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

kdevelop/kdevplatform/language/duchain

  • extragear
  • kdevelop
  • 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 copy(const DUChainBaseData& from, DUChainBaseData& to, bool constant) const = 0;
38  virtual DUChainBaseData* cloneData(const DUChainBaseData& data) const = 0;
39  virtual uint dynamicSize(const DUChainBaseData& data) const = 0;
40 
41  virtual ~DUChainBaseFactory()
42  {
43  }
44 };
45 
47 template <class T, class Data>
48 class DUChainItemFactory
49  : public DUChainBaseFactory
50 {
51 public:
52  DUChainBase* create(DUChainBaseData* data) const override
53  {
54  return new T(*static_cast<Data*>(data));
55  }
56 
57  void copy(const DUChainBaseData& from, DUChainBaseData& to, bool constant) const override
58  {
59  Q_ASSERT(from.classId == T::Identity);
60 
61  bool& isConstant = DUChainBaseData::shouldCreateConstantData();
62  const bool previousConstant = isConstant;
63  if (previousConstant != constant) {
64  isConstant = constant;
65  }
66 
67  new (&to) Data(static_cast<const Data&>(from)); //Call the copy constructor to initialize the target
68 
69  if (previousConstant != constant) {
70  isConstant = previousConstant;
71  }
72  }
73 
74  void callDestructor(DUChainBaseData* data) const override
75  {
76  Q_ASSERT(data->classId == T::Identity);
77  static_cast<Data*>(data)->~Data();
78  }
79 
80  void freeDynamicData(DUChainBaseData* data) const override
81  {
82  Q_ASSERT(data->classId == T::Identity);
83  static_cast<Data*>(data)->freeDynamicData();
84  }
85 
86  uint dynamicSize(const DUChainBaseData& data) const override
87  {
88  Q_ASSERT(data.classId == T::Identity);
89  return static_cast<const Data&>(data).dynamicSize();
90  }
91 
92  DUChainBaseData* cloneData(const DUChainBaseData& data) const override
93  {
94  Q_ASSERT(data.classId == T::Identity);
95  return new Data(static_cast<const Data&>(data));
96  }
97 };
98 
105 class KDEVPLATFORMLANGUAGE_EXPORT DUChainItemSystem
106 {
107 public:
111  template <class T, class Data>
112  void registerTypeClass()
113  {
114  if (m_factories.size() <= T::Identity) {
115  m_factories.resize(T::Identity + 1);
116  m_dataClassSizes.resize(T::Identity + 1);
117  }
118 
119  Q_ASSERT_X(!m_factories[T::Identity], Q_FUNC_INFO, "This identity is already registered");
120  m_factories[T::Identity] = new DUChainItemFactory<T, Data>();
121  m_dataClassSizes[T::Identity] = sizeof(Data);
122  }
123 
127  template <class T, class Data>
128  void unregisterTypeClass()
129  {
130  Q_ASSERT(m_factories.size() > T::Identity);
131  Q_ASSERT(m_factories[T::Identity]);
132  delete m_factories[T::Identity];
133  m_factories[T::Identity] = nullptr;
134  m_dataClassSizes[T::Identity] = 0;
135  }
136 
141  DUChainBase* create(DUChainBaseData* data) const;
142 
144  DUChainBaseData* cloneData(const DUChainBaseData& data) const;
145 
150  void copy(const DUChainBaseData& from, DUChainBaseData& to, bool constant) const;
151 
153  uint dynamicSize(const DUChainBaseData& data) const;
154 
157  uint dataClassSize(const DUChainBaseData& data) const;
158 
161  void callDestructor(DUChainBaseData* data) const;
162 
165  void freeDynamicData(DUChainBaseData* data) const;
166 
168  static DUChainItemSystem& self();
169 
170 private:
171  Q_DISABLE_COPY(DUChainItemSystem)
172  DUChainItemSystem() = default;
173  ~DUChainItemSystem();
174 
175  QVector<DUChainBaseFactory*> m_factories;
176  QVector<uint> m_dataClassSizes;
177 };
178 
179 template <typename T>
180 struct DUChainType {};
181 
183 #define DUCHAIN_DECLARE_TYPE(Type) \
184  namespace KDevelop { \
185  template <> struct DUChainType<Type> { \
186  static void registerType(); \
187  static void unregisterType(); \
188  }; \
189  }
190 #define DUCHAIN_DEFINE_TYPE_WITH_DATA(Type, Data) \
192  void KDevelop::DUChainType<Type>::registerType() { DUChainItemSystem::self().registerTypeClass<Type, Data>(); } \
193  void KDevelop::DUChainType<Type>::unregisterType() { DUChainItemSystem::self().unregisterTypeClass<Type, Data>(); }
194 #define DUCHAIN_DEFINE_TYPE(Type) \
195  DUCHAIN_DEFINE_TYPE_WITH_DATA(Type, Type ## Data)
196 
198 template <typename T>
199 void duchainRegisterType() { DUChainType<T>::registerType(); }
201 template <typename T>
202 void duchainUnregisterType() { DUChainType<T>::unregisterType(); }
203 
207 template <class T, class Data>
208 struct DUChainItemRegistrator
209 {
210  DUChainItemRegistrator()
211  {
212  DUChainItemSystem::self().registerTypeClass<T, Data>();
213  }
214  ~DUChainItemRegistrator()
215  {
216  DUChainItemSystem::self().unregisterTypeClass<T, Data>();
217  }
218 private:
219  Q_DISABLE_COPY(DUChainItemRegistrator)
220 };
221 
225 #define REGISTER_DUCHAIN_ITEM(Class) KDevelop::DUChainItemRegistrator<Class, Class ## Data> register ## Class
226 #define REGISTER_DUCHAIN_ITEM_WITH_DATA(Class, Data) KDevelop::DUChainItemRegistrator<Class, Data> register ## Class
227 }
228 
229 #endif
KDevelop::DUChainType
Definition: duchainregister.h:180
KDevelop::DUChainItemSystem
A class which registers data types and creates factories for them.
Definition: duchainregister.h:105
KDevelop::duchainUnregisterType
void duchainUnregisterType()
Unregister T to DUChainItemSystem.
Definition: duchainregister.h:202
KDevelop::DUChainItemSystem::self
static DUChainItemSystem & self()
Access the static DUChainItemSystem instance.
Definition: duchainregister.cpp:86
KDevelop::DUChainItemFactory::copy
void copy(const DUChainBaseData &from, DUChainBaseData &to, bool constant) const override
Definition: duchainregister.h:57
KDevelop::DUChainItemRegistrator::DUChainItemRegistrator
DUChainItemRegistrator()
Definition: duchainregister.h:210
KDevelop::DUChainItemFactory
Never use this directly, use the REGISTER_DUCHAIN_ITEM macro instead.
Definition: duchainregister.h:48
KDevelop::DUChainBaseData::shouldCreateConstantData
static bool & shouldCreateConstantData()
Used to decide whether a constructed item should create constant data.
Definition: duchainbase.cpp:228
KDevelop::DUChainBase
Base class for definition-use chain objects.
Definition: duchainbase.h:131
KDevelop::DUChainBaseFactory
This class is purely internal and doesn't need to be documented.
Definition: duchainregister.h:31
KDevelop::DUChainBaseData::classId
quint16 classId
Definition: duchainbase.h:84
KDevelop::DUChainBaseFactory::~DUChainBaseFactory
virtual ~DUChainBaseFactory()
Definition: duchainregister.h:41
KDevelop::DUChainItemFactory::cloneData
DUChainBaseData * cloneData(const DUChainBaseData &data) const override
Definition: duchainregister.h:92
KDevelop::DUChainItemFactory::create
DUChainBase * create(DUChainBaseData *data) const override
Definition: duchainregister.h:52
KDevelop::DUChainItemRegistrator::~DUChainItemRegistrator
~DUChainItemRegistrator()
Definition: duchainregister.h:214
QVector
KDevelop::DUChainBaseData
Definition: duchainbase.h:59
KDevelop::DUChainItemSystem::unregisterTypeClass
void unregisterTypeClass()
Unregister an DUChainBase subclass.
Definition: duchainregister.h:128
KDevelop::duchainRegisterType
void duchainRegisterType()
Register T to DUChainItemSystem.
Definition: duchainregister.h:199
duchainbase.h
KDevelop::DUChainItemFactory::freeDynamicData
void freeDynamicData(DUChainBaseData *data) const override
Definition: duchainregister.h:80
KDevelop::DUChainItemSystem::registerTypeClass
void registerTypeClass()
Register a new DUChainBase subclass.
Definition: duchainregister.h:112
KDevelop::DUChainItemFactory::dynamicSize
uint dynamicSize(const DUChainBaseData &data) const override
Definition: duchainregister.h:86
KDevelop::DUChainItemRegistrator
Helper class to register an DUChainBase subclass.
Definition: duchainregister.h:208
KDevelop::DUChainItemFactory::callDestructor
void callDestructor(DUChainBaseData *data) const override
Definition: duchainregister.h:74
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Thu Dec 12 2019 03:33:28 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdevelop/kdevplatform/language/duchain

Skip menu "kdevelop/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