• 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
duchainregister.h
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 #ifndef KDEVPLATFORM_DUCHAINREGISTER_H
20 #define KDEVPLATFORM_DUCHAINREGISTER_H
21 
22 #include "duchainbase.h"
23 
24 namespace KDevelop {
25 class DUChainBase;
26 class DUChainBaseData;
27 
31 class KDEVPLATFORMLANGUAGE_EXPORT DUChainBaseFactory
32 {
33 public:
34  virtual DUChainBase* create(DUChainBaseData* data) const = 0;
35  virtual void callDestructor(DUChainBaseData* data) const = 0;
36  virtual void freeDynamicData(DUChainBaseData* data) const = 0;
37  virtual void deleteDynamicData(DUChainBaseData* data) const = 0;
38  virtual void copy(const DUChainBaseData& from, DUChainBaseData& to, bool constant) const = 0;
39  virtual DUChainBaseData* cloneData(const DUChainBaseData& data) const = 0;
40  virtual uint dynamicSize(const DUChainBaseData& data) const = 0;
41 
42  virtual ~DUChainBaseFactory()
43  {
44  }
45 };
46 
48 template <class T, class Data>
49 class DUChainItemFactory
50  : public DUChainBaseFactory
51 {
52 public:
53  DUChainBase* create(DUChainBaseData* data) const override
54  {
55  return new T(*static_cast<Data*>(data));
56  }
57 
58  void copy(const DUChainBaseData& from, DUChainBaseData& to, bool constant) const override
59  {
60  Q_ASSERT(from.classId == T::Identity);
61 
62  bool& isConstant = DUChainBaseData::shouldCreateConstantData();
63  const bool previousConstant = isConstant;
64  if (previousConstant != constant) {
65  isConstant = constant;
66  }
67 
68  new (&to) Data(static_cast<const Data&>(from)); //Call the copy constructor to initialize the target
69 
70  if (previousConstant != constant) {
71  isConstant = previousConstant;
72  }
73  }
74 
75  void callDestructor(DUChainBaseData* data) const override
76  {
77  Q_ASSERT(data->classId == T::Identity);
78  static_cast<Data*>(data)->~Data();
79  }
80 
81  void freeDynamicData(DUChainBaseData* data) const override
82  {
83  Q_ASSERT(data->classId == T::Identity);
84  static_cast<Data*>(data)->freeDynamicData();
85  }
86 
87  void deleteDynamicData(DUChainBaseData* data) const override
88  {
89  Q_ASSERT(data->classId == T::Identity);
90  delete static_cast<Data*>(data);
91  }
92 
93  uint dynamicSize(const DUChainBaseData& data) const override
94  {
95  Q_ASSERT(data.classId == T::Identity);
96  return static_cast<const Data&>(data).dynamicSize();
97  }
98 
99  DUChainBaseData* cloneData(const DUChainBaseData& data) const override
100  {
101  Q_ASSERT(data.classId == T::Identity);
102  return new Data(static_cast<const Data&>(data));
103  }
104 };
105 
112 class KDEVPLATFORMLANGUAGE_EXPORT DUChainItemSystem
113 {
114 public:
118  template <class T, class Data>
119  void registerTypeClass()
120  {
121  if (m_factories.size() <= T::Identity) {
122  m_factories.resize(T::Identity + 1);
123  m_dataClassSizes.resize(T::Identity + 1);
124  }
125 
126  Q_ASSERT_X(!m_factories[T::Identity], Q_FUNC_INFO, "This identity is already registered");
127  m_factories[T::Identity] = new DUChainItemFactory<T, Data>();
128  m_dataClassSizes[T::Identity] = sizeof(Data);
129  }
130 
134  template <class T, class Data>
135  void unregisterTypeClass()
136  {
137  Q_ASSERT(m_factories.size() > T::Identity);
138  Q_ASSERT(m_factories[T::Identity]);
139  delete m_factories[T::Identity];
140  m_factories[T::Identity] = nullptr;
141  m_dataClassSizes[T::Identity] = 0;
142  }
143 
148  DUChainBase* create(DUChainBaseData* data) const;
149 
151  DUChainBaseData* cloneData(const DUChainBaseData& data) const;
152 
157  void copy(const DUChainBaseData& from, DUChainBaseData& to, bool constant) const;
158 
160  uint dynamicSize(const DUChainBaseData& data) const;
161 
164  uint dataClassSize(const DUChainBaseData& data) const;
165 
168  void callDestructor(DUChainBaseData* data) const;
169 
172  void freeDynamicData(DUChainBaseData* data) const;
173 
175  void deleteDynamicData(DUChainBaseData* data) const;
176 
178  static DUChainItemSystem& self();
179 
180 private:
181  Q_DISABLE_COPY(DUChainItemSystem)
182  DUChainItemSystem() = default;
183  ~DUChainItemSystem();
184 
185  QVector<DUChainBaseFactory*> m_factories;
186  QVector<uint> m_dataClassSizes;
187 };
188 
189 template <typename T>
190 struct DUChainType {};
191 
193 #define DUCHAIN_DECLARE_TYPE(Type) \
194  namespace KDevelop { \
195  template <> struct DUChainType<Type> { \
196  static void registerType(); \
197  static void unregisterType(); \
198  }; \
199  }
200 #define DUCHAIN_DEFINE_TYPE_WITH_DATA(Type, Data) \
202  void KDevelop::DUChainType<Type>::registerType() { DUChainItemSystem::self().registerTypeClass<Type, Data>(); } \
203  void KDevelop::DUChainType<Type>::unregisterType() { DUChainItemSystem::self().unregisterTypeClass<Type, Data>(); }
204 #define DUCHAIN_DEFINE_TYPE(Type) \
205  DUCHAIN_DEFINE_TYPE_WITH_DATA(Type, Type ## Data)
206 
208 template <typename T>
209 void duchainRegisterType() { DUChainType<T>::registerType(); }
211 template <typename T>
212 void duchainUnregisterType() { DUChainType<T>::unregisterType(); }
213 
217 template <class T, class Data>
218 struct DUChainItemRegistrator
219 {
220  DUChainItemRegistrator()
221  {
222  DUChainItemSystem::self().registerTypeClass<T, Data>();
223  }
224  ~DUChainItemRegistrator()
225  {
226  DUChainItemSystem::self().unregisterTypeClass<T, Data>();
227  }
228 private:
229  Q_DISABLE_COPY(DUChainItemRegistrator)
230 };
231 
235 #define REGISTER_DUCHAIN_ITEM(Class) KDevelop::DUChainItemRegistrator<Class, Class ## Data> register ## Class
236 #define REGISTER_DUCHAIN_ITEM_WITH_DATA(Class, Data) KDevelop::DUChainItemRegistrator<Class, Data> register ## Class
237 }
238 
239 #endif
KDevelop::duchainRegisterType
void duchainRegisterType()
Register T to DUChainItemSystem.
Definition: duchainregister.h:209
KDevelop::DUChainItemRegistrator::DUChainItemRegistrator
DUChainItemRegistrator()
Definition: duchainregister.h:220
KDevelop::duchainUnregisterType
void duchainUnregisterType()
Unregister T to DUChainItemSystem.
Definition: duchainregister.h:212
KDevelop::DUChainBase
Base class for definition-use chain objects.
Definition: duchainbase.h:131
KDevelop::DUChainItemFactory::copy
void copy(const DUChainBaseData &from, DUChainBaseData &to, bool constant) const override
Definition: duchainregister.h:58
KDevelop::DUChainItemSystem
A class which registers data types and creates factories for them.
Definition: duchainregister.h:112
KDevelop::DUChainItemFactory::cloneData
DUChainBaseData * cloneData(const DUChainBaseData &data) const override
Definition: duchainregister.h:99
KDevelop::DUChainItemSystem::unregisterTypeClass
void unregisterTypeClass()
Unregister an DUChainBase subclass.
Definition: duchainregister.h:135
KDevelop::DUChainItemFactory::create
DUChainBase * create(DUChainBaseData *data) const override
Definition: duchainregister.h:53
KDevelop::DUChainBaseData::shouldCreateConstantData
static bool & shouldCreateConstantData()
Used to decide whether a constructed item should create constant data.
Definition: duchainbase.cpp:231
KDevelop::DUChainItemRegistrator::~DUChainItemRegistrator
~DUChainItemRegistrator()
Definition: duchainregister.h:224
KDevelop::DUChainItemFactory::callDestructor
void callDestructor(DUChainBaseData *data) const override
Definition: duchainregister.h:75
KDevelop::DUChainItemRegistrator
Helper class to register an DUChainBase subclass.
Definition: duchainregister.h:218
KDevelop::DUChainBaseData::classId
quint16 classId
Definition: duchainbase.h:84
KDevelop::DUChainItemFactory::freeDynamicData
void freeDynamicData(DUChainBaseData *data) const override
Definition: duchainregister.h:81
KDevelop::DUChainType
Definition: duchainregister.h:190
KDevelop::DUChainBaseFactory
This class is purely internal and doesn't need to be documented.
Definition: duchainregister.h:31
KDevelop::DUChainItemFactory::deleteDynamicData
void deleteDynamicData(DUChainBaseData *data) const override
Definition: duchainregister.h:87
KDevelop::DUChainItemSystem::registerTypeClass
void registerTypeClass()
Register a new DUChainBase subclass.
Definition: duchainregister.h:119
KDevelop
Definition: abstractfunctiondeclaration.cpp:27
KDevelop::DUChainBaseFactory::~DUChainBaseFactory
virtual ~DUChainBaseFactory()
Definition: duchainregister.h:42
KDevelop::DUChainItemFactory
Never use this directly, use the REGISTER_DUCHAIN_ITEM macro instead.
Definition: duchainregister.h:49
QVector
KDevelop::DUChainBaseData
Definition: duchainbase.h:59
KDevelop::DUChainItemFactory::dynamicSize
uint dynamicSize(const DUChainBaseData &data) const override
Definition: duchainregister.h:93
KDevelop::DUChainItemSystem::self
static DUChainItemSystem & self()
Access the static DUChainItemSystem instance.
Definition: duchainregister.cpp:93
duchainbase.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Mar 3 2021 00:37:28 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