File: importers.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (204 lines) | stat: -rw-r--r-- 5,206 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
    SPDX-FileCopyrightText: 2008 David Nolden <david.nolden.kdevelop@art-master.de>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include "importers.h"

#include "declarationid.h"
#include "duchainpointer.h"
#include "serialization/itemrepository.h"
#include "topducontext.h"

namespace KDevelop {
DEFINE_LIST_MEMBER_HASH(ImportersItem, importers, IndexedDUContext)

class ImportersItem
{
public:
    ImportersItem()
    {
        initializeAppendedLists();
    }
    ImportersItem(const ImportersItem& rhs, bool dynamic = true) : declaration(rhs.declaration)
    {
        initializeAppendedLists(dynamic);
        copyListsFrom(rhs);
    }

    ~ImportersItem()
    {
        freeAppendedLists();
    }

    ImportersItem& operator=(const ImportersItem& rhs) = delete;

    unsigned int hash() const
    {
        //We only compare the declaration. This allows us implementing a map, although the item-repository
        //originally represents a set.
        return declaration.hash();
    }

    unsigned int itemSize() const
    {
        return dynamicSize();
    }

    uint classSize() const
    {
        return sizeof(ImportersItem);
    }

    DeclarationId declaration;

    START_APPENDED_LISTS(ImportersItem);
    APPENDED_LIST_FIRST(ImportersItem, IndexedDUContext, importers);
    END_APPENDED_LISTS(ImportersItem, importers);
};

class ImportersRequestItem
{
public:

    ImportersRequestItem(const ImportersItem& item) : m_item(item)
    {
    }
    enum {
        AverageSize = 30 //This should be the approximate average size of an Item
    };

    unsigned int hash() const
    {
        return m_item.hash();
    }

    uint itemSize() const
    {
        return m_item.itemSize();
    }

    void createItem(ImportersItem* item) const
    {
        new (item) ImportersItem(m_item, false);
    }

    static void destroy(ImportersItem* item, KDevelop::AbstractItemRepository&)
    {
        item->~ImportersItem();
    }

    static bool persistent(const ImportersItem* /*item*/)
    {
        return true;
    }

    bool equals(const ImportersItem* item) const
    {
        return m_item.declaration == item->declaration;
    }

    const ImportersItem& m_item;
};

// Maps declaration-ids to Importers
using ImportersRepo = ItemRepository<ImportersItem, ImportersRequestItem>;

template<>
class ItemRepositoryFor<Importers>
{
    friend struct LockedItemRepository;
    static ImportersRepo& repo()
    {
        static QMutex mutex;
        static ImportersRepo repo { QStringLiteral("Importer Map"), &mutex };
        return repo;
    }
};

Importers::Importers()
{
    LockedItemRepository::initialize<Importers>();
}

void Importers::addImporter(const DeclarationId& id, const IndexedDUContext& use)
{
    ImportersItem item;
    item.declaration = id;
    item.importersList().append(use);
    ImportersRequestItem request(item);

    LockedItemRepository::write<Importers>([&](ImportersRepo& repo) {
        uint index = repo.findIndex(item);

        if (index) {
            // Check whether the item is already in the mapped list, else copy the list into the new created item
            const ImportersItem* oldItem = repo.itemFromIndex(index);
            for (unsigned int a = 0; a < oldItem->importersSize(); ++a) {
                if (oldItem->importers()[a] == use)
                    return; // Already there
                item.importersList().append(oldItem->importers()[a]);
            }

            repo.deleteItem(index);
        }

        // This inserts the changed item
        repo.index(request);
    });
}

void Importers::removeImporter(const DeclarationId& id, const IndexedDUContext& use)
{
    ImportersItem item;
    item.declaration = id;
    ImportersRequestItem request(item);

    LockedItemRepository::write<Importers>([&](ImportersRepo& repo) {
        uint index = repo.findIndex(item);

        if (index) {
            // Check whether the item is already in the mapped list, else copy the list into the new created item
            const ImportersItem* oldItem = repo.itemFromIndex(index);
            for (unsigned int a = 0; a < oldItem->importersSize(); ++a)
                if (!(oldItem->importers()[a] == use))
                    item.importersList().append(oldItem->importers()[a]);

            repo.deleteItem(index);
            Q_ASSERT(repo.findIndex(item) == 0);

            // This inserts the changed item
            if (item.importersSize() != 0)
                repo.index(request);
        }
    });
}

KDevVarLengthArray<IndexedDUContext> Importers::importers(const DeclarationId& id) const
{
    KDevVarLengthArray<IndexedDUContext> ret;

    ImportersItem item;
    item.declaration = id;
    ImportersRequestItem request(item);

    LockedItemRepository::read<Importers>([&](const ImportersRepo& repo) {
        uint index = repo.findIndex(item);

        if (index) {
            const ImportersItem* repositoryItem = repo.itemFromIndex(index);
            FOREACH_FUNCTION(const IndexedDUContext& decl, repositoryItem->importers)
            ret.append(decl);
        }
    });

    return ret;
}

Importers& Importers::self()
{
    static Importers globalImporters;
    return globalImporters;
}
}