Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
berryReflection.h
Go to the documentation of this file.
1 /*===================================================================
2 
3 BlueBerry Platform
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef BERRYREFLECTION_H
18 #define BERRYREFLECTION_H
19 
21 
22 #include <QString>
23 #include <QList>
24 
25 #include <iostream>
26 #include <memory>
27 
28 #ifdef GetClassName
29 // clash with WinUser.h definition
30 #undef GetClassName
31 #endif
32 
33 namespace berry {
34 
35 class Object;
36 
37 namespace Reflection {
38 
39 org_blueberry_core_runtime_EXPORT QString DemangleName(const char* typeName);
40 
41 org_blueberry_core_runtime_EXPORT QString GetClassName(const Object* obj);
42 
43 template<typename T>
44 QString GetClassName()
45 {
46  return DemangleName(typeid(T).name());
47 }
48 
49 class TypeInfo;
50 
51 template<typename T>
52 QList<TypeInfo> GetSuperclasses();
53 
54 struct EmptyType {};
55 
56 template<
57  typename T0=EmptyType,
58  typename T1=EmptyType,
59  typename T2=EmptyType,
60  typename T3=EmptyType,
61  typename T4=EmptyType,
62  typename T5=EmptyType,
63  typename T6=EmptyType,
64  typename T7=EmptyType,
65  typename T8=EmptyType,
66  typename T9=EmptyType
67 > struct TypeList;
68 
69 template<
70  typename T0,
71  typename T1,
72  typename T2,
73  typename T3,
74  typename T4,
75  typename T5,
76  typename T6,
77  typename T7,
78  typename T8,
79  typename T9
80 > struct TypeList
81 {
82  typedef T0 head;
84  enum
85  {
87  };
88 };
89 
90 template<>
93 {
94  enum
95  {
96  length = 0
97  };
98 };
99 
100 // MapReduce
101 template<
102  typename TL,
103  template <typename> class Map,
104  template <typename> class Reduce
105 >
106 struct MapReduce
107 {
108  typedef typename Map<char>::type map_type;
109  typedef typename Reduce<map_type>::result type;
110 
111  static type& value(type& result)
112  {
113  return MapReduce<typename TL::tail, Map, Reduce>::value(Reduce<map_type>::value(result, Map<typename TL::head>::value()));
114  }
115 
116  static type value()
117  {
118  type result;
119  return MapReduce<typename TL::tail, Map, Reduce>::value(Reduce<map_type>::value(result, Map<typename TL::head>::value()));
120  }
121 };
122 
123 template<
124  template <typename> class Map,
125  template <typename> class Reduce
126 >
127 struct MapReduce<TypeList<>, Map, Reduce>
128 {
129  typedef typename Map<char>::type map_type;
130  typedef typename Reduce<map_type>::result type;
131 
132  static type value()
133  {
134  return type();
135  }
136 
137  static type& value(type& result)
138  {
139  return result;
140  }
141 };
142 
144 {
145 public:
146 
147  TypeInfo();
148 
149  template<typename T>
150  explicit TypeInfo(T* /*dummy*/)
151  : m_Self(std::make_shared<Model<T> >())
152  {}
153 
154  bool operator==(const TypeInfo& other) const;
155 
156  template<typename T>
157  static TypeInfo New()
158  { T* dummy = nullptr; return TypeInfo(dummy); }
159 
160  QString GetName() const;
161  QList<TypeInfo> GetSuperclasses() const;
162 
163 
164 private:
165 
166  struct org_blueberry_core_runtime_EXPORT Concept {
167  virtual ~Concept();
168  virtual QString GetName() const = 0;
169  virtual QList<TypeInfo> GetSuperclasses() const = 0;
170  };
171 
172  template<typename T>
173  struct Model : Concept {
174  QString GetName() const override
175  { return GetClassName<T>(); }
176 
177  QList<TypeInfo> GetSuperclasses() const override;
178  };
179 
180  std::shared_ptr<const Concept> m_Self;
181 };
182 
183 template<typename T>
185 {
186  typedef TypeInfo type;
187  static type value()
188  {
189  return TypeInfo::New<T>();
190  }
191 };
192 
193 template<typename T>
195 {
196  typedef T type;
197  typedef QList<T> result;
198  static result& value(result& list, const type& item)
199  {
200  list.push_back(item);
201  return list;
202  }
203 };
204 
205 template<typename T>
207 {
208  typedef char Small;
209  struct Big { char dummy[2]; };
210 
211  template<typename U> static Small Test(typename U::SuperclassTypes*);
212  template<typename U> static Big Test(...);
213 public:
214  enum { value = sizeof(Test<T>(nullptr)) == sizeof(Small) };
215 };
216 
217 template<typename T, bool>
219 
220 template<typename T>
221 struct GetSuperclassTypeList<T, true> { typedef typename T::SuperclassTypes value; };
222 
223 template<typename T>
224 QList<TypeInfo> GetSuperclasses()
225 {
227 }
228 
229 template<typename T>
230 QList<TypeInfo> TypeInfo::Model<T>::GetSuperclasses() const
231 {
232  return ::berry::Reflection::GetSuperclasses<T>();
233 }
234 
235 } // end namespace Reflection
236 
237 } // end namespace berry
238 
239 #endif // BERRYREFLECTION_H
240 
QString GetClassName(const Object *obj)
Reduce< map_type >::result type
STL namespace.
QString DemangleName(const char *mangledName)
MITKCORE_EXPORT bool operator==(const InteractionEvent &a, const InteractionEvent &b)
#define org_blueberry_core_runtime_EXPORT
TypeList< T1, T2, T3, T4, T5, T6, T7, T8, T9 > tail
static type & value(type &result)
static result & value(result &list, const type &item)
QList< TypeInfo > GetSuperclasses()
static std::string GetName(std::string fileName, std::string suffix)