• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdevelop API Reference
  • KDE Home
  • Contact Us
 

kdevplatform/language/duchain

  • sources
  • kfour-appscomplete
  • kdevelop
  • kdevplatform
  • language
  • duchain
importers.cpp
Go to the documentation of this file.
1 /* This file is part of KDevelop
2  Copyright 2008 David Nolden <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17  */
18 
19 #include "importers.h"
20 
21 #include "declarationid.h"
22 #include "duchainpointer.h"
23 #include "serialization/itemrepository.h"
24 #include "topducontext.h"
25 
26 namespace KDevelop {
27 DEFINE_LIST_MEMBER_HASH(ImportersItem, importers, IndexedDUContext)
28 
29 class ImportersItem
30 {
31 public:
32  ImportersItem()
33  {
34  initializeAppendedLists();
35  }
36  ImportersItem(const ImportersItem& rhs, bool dynamic = true) : declaration(rhs.declaration)
37  {
38  initializeAppendedLists(dynamic);
39  copyListsFrom(rhs);
40  }
41 
42  ~ImportersItem()
43  {
44  freeAppendedLists();
45  }
46 
47  ImportersItem& operator=(const ImportersItem& rhs) = delete;
48 
49  unsigned int hash() const
50  {
51  //We only compare the declaration. This allows us implementing a map, although the item-repository
52  //originally represents a set.
53  return declaration.hash();
54  }
55 
56  unsigned int itemSize() const
57  {
58  return dynamicSize();
59  }
60 
61  uint classSize() const
62  {
63  return sizeof(ImportersItem);
64  }
65 
66  DeclarationId declaration;
67 
68  START_APPENDED_LISTS(ImportersItem);
69  APPENDED_LIST_FIRST(ImportersItem, IndexedDUContext, importers);
70  END_APPENDED_LISTS(ImportersItem, importers);
71 };
72 
73 class ImportersRequestItem
74 {
75 public:
76 
77  ImportersRequestItem(const ImportersItem& item) : m_item(item)
78  {
79  }
80  enum {
81  AverageSize = 30 //This should be the approximate average size of an Item
82  };
83 
84  unsigned int hash() const
85  {
86  return m_item.hash();
87  }
88 
89  uint itemSize() const
90  {
91  return m_item.itemSize();
92  }
93 
94  void createItem(ImportersItem* item) const
95  {
96  new (item) ImportersItem(m_item, false);
97  }
98 
99  static void destroy(ImportersItem* item, KDevelop::AbstractItemRepository&)
100  {
101  item->~ImportersItem();
102  }
103 
104  static bool persistent(const ImportersItem* /*item*/)
105  {
106  return true;
107  }
108 
109  bool equals(const ImportersItem* item) const
110  {
111  return m_item.declaration == item->declaration;
112  }
113 
114  const ImportersItem& m_item;
115 };
116 
117 class ImportersPrivate
118 {
119 public:
120 
121  ImportersPrivate() : m_importers(QStringLiteral("Importer Map"))
122  {
123  }
124  //Maps declaration-ids to Importers
125  // mutable as things like findIndex are not const
126  mutable ItemRepository<ImportersItem, ImportersRequestItem> m_importers;
127 };
128 
129 Importers::Importers()
130  : d_ptr(new ImportersPrivate())
131 {
132 }
133 
134 Importers::~Importers() = default;
135 
136 void Importers::addImporter(const DeclarationId& id, const IndexedDUContext& use)
137 {
138  Q_D(Importers);
139 
140  ImportersItem item;
141  item.declaration = id;
142  item.importersList().append(use);
143  ImportersRequestItem request(item);
144 
145  uint index = d->m_importers.findIndex(item);
146 
147  if (index) {
148  //Check whether the item is already in the mapped list, else copy the list into the new created item
149  const ImportersItem* oldItem = d->m_importers.itemFromIndex(index);
150  for (unsigned int a = 0; a < oldItem->importersSize(); ++a) {
151  if (oldItem->importers()[a] == use)
152  return; //Already there
153  item.importersList().append(oldItem->importers()[a]);
154  }
155 
156  d->m_importers.deleteItem(index);
157  }
158 
159  //This inserts the changed item
160  d->m_importers.index(request);
161 }
162 
163 void Importers::removeImporter(const DeclarationId& id, const IndexedDUContext& use)
164 {
165  Q_D(Importers);
166 
167  ImportersItem item;
168  item.declaration = id;
169  ImportersRequestItem request(item);
170 
171  uint index = d->m_importers.findIndex(item);
172 
173  if (index) {
174  //Check whether the item is already in the mapped list, else copy the list into the new created item
175  const ImportersItem* oldItem = d->m_importers.itemFromIndex(index);
176  for (unsigned int a = 0; a < oldItem->importersSize(); ++a)
177  if (!(oldItem->importers()[a] == use))
178  item.importersList().append(oldItem->importers()[a]);
179 
180  d->m_importers.deleteItem(index);
181  Q_ASSERT(d->m_importers.findIndex(item) == 0);
182 
183  //This inserts the changed item
184  if (item.importersSize() != 0)
185  d->m_importers.index(request);
186  }
187 }
188 
189 KDevVarLengthArray<IndexedDUContext> Importers::importers(const DeclarationId& id) const
190 {
191  Q_D(const Importers);
192 
193  KDevVarLengthArray<IndexedDUContext> ret;
194 
195  ImportersItem item;
196  item.declaration = id;
197  ImportersRequestItem request(item);
198 
199  uint index = d->m_importers.findIndex(item);
200 
201  if (index) {
202  const ImportersItem* repositoryItem = d->m_importers.itemFromIndex(index);
203  FOREACH_FUNCTION(const IndexedDUContext &decl, repositoryItem->importers)
204  ret.append(decl);
205  }
206 
207  return ret;
208 }
209 
210 Importers& Importers::self()
211 {
212  static Importers globalImporters;
213  return globalImporters;
214 }
215 }
KDevelop::m_importers
KDEVPLATFORMLANGUAGE_EXPORT m_importers
Definition: ducontextdata.h:56
duchainpointer.h
DEFINE_LIST_MEMBER_HASH
#define DEFINE_LIST_MEMBER_HASH(container, member, type)
Definition: appendedlist.h:218
APPENDED_LIST_FIRST
#define APPENDED_LIST_FIRST(container, type, name)
Definition: appendedlist.h:321
topducontext.h
FOREACH_FUNCTION
#define FOREACH_FUNCTION(item, container)
Foreach macro that takes a container and a function-name, and will iterate through the vector returne...
Definition: appendedlist.h:213
KDevelop::Importers::importers
KDevVarLengthArray< IndexedDUContext > importers(const DeclarationId &id) const
Gets the top-contexts of all users assigned to the declaration-id.
Definition: importers.cpp:189
KDevVarLengthArray
KDevelop::Importers::self
static Importers & self()
Definition: importers.cpp:210
KDevelop::Importers::removeImporter
void removeImporter(const DeclarationId &id, const IndexedDUContext &use)
Removes the given top-context from the list of uses.
Definition: importers.cpp:163
KDevelop::DeclarationId
Allows clearly identifying a Declaration.
Definition: declarationid.h:50
declarationid.h
END_APPENDED_LISTS
#define END_APPENDED_LISTS(container, predecessor)
Definition: appendedlist.h:358
KDevelop::Importers::~Importers
~Importers()
Destructor.
KDevelop::Importers
Global mapping of Declaration-Ids to contexts that import the associated context, protected through D...
Definition: importers.h:41
importers.h
KDevelop
Definition: abstractfunctiondeclaration.cpp:27
KDevelop::Importers::Importers
Importers()
Constructor.
Definition: importers.cpp:129
KDevelop::Importers::addImporter
void addImporter(const DeclarationId &id, const IndexedDUContext &use)
Adds a top-context to the users-list of the given id.
Definition: importers.cpp:136
START_APPENDED_LISTS
#define START_APPENDED_LISTS(container)
use this if the class does not have a base class that also uses appended lists
Definition: appendedlist.h:250
KDevelop::IndexedDUContext
Represents a context only by its global indices.
Definition: indexedducontext.h:35
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Apr 10 2021 23:30:23 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdevplatform/language/duchain

Skip menu "kdevplatform/language/duchain"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdevelop API Reference

Skip menu "kdevelop API Reference"
  • kdevplatform
  •   debugger
  •   documentation
  •   interfaces
  •   language
  •     assistant
  •     backgroundparser
  •     checks
  •     classmodel
  •     codecompletion
  •     codegen
  •     duchain
  •     editor
  •     highlighting
  •     interfaces
  •     util
  •   outputview
  •   project
  •   serialization
  •   shell
  •   sublime
  •   tests
  •   util
  •   vcs

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal