Medical Imaging Interaction Toolkit  2018.4.99-b7f3afaa
Medical Imaging Interaction Toolkit
berryReflection.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef BERRYREFLECTION_H
14 #define BERRYREFLECTION_H
15 
17 
18 #include <QString>
19 #include <QList>
20 
21 #include <iostream>
22 #include <memory>
23 
24 #ifdef GetClassName
25 // clash with WinUser.h definition
26 #undef GetClassName
27 #endif
28 
29 namespace berry {
30 
31 class Object;
32 
33 namespace Reflection {
34 
35 org_blueberry_core_runtime_EXPORT QString DemangleName(const char* typeName);
36 
37 org_blueberry_core_runtime_EXPORT QString GetClassName(const Object* obj);
38 
39 template<typename T>
40 QString GetClassName()
41 {
42  return DemangleName(typeid(T).name());
43 }
44 
45 class TypeInfo;
46 
47 template<typename T>
48 QList<TypeInfo> GetSuperclasses();
49 
50 struct EmptyType {};
51 
52 template<
53  typename T0=EmptyType,
54  typename T1=EmptyType,
55  typename T2=EmptyType,
56  typename T3=EmptyType,
57  typename T4=EmptyType,
58  typename T5=EmptyType,
59  typename T6=EmptyType,
60  typename T7=EmptyType,
61  typename T8=EmptyType,
62  typename T9=EmptyType
63 > struct TypeList;
64 
65 template<
66  typename T0,
67  typename T1,
68  typename T2,
69  typename T3,
70  typename T4,
71  typename T5,
72  typename T6,
73  typename T7,
74  typename T8,
75  typename T9
76 > struct TypeList
77 {
78  typedef T0 head;
80  enum
81  {
82  length = tail::length+1
83  };
84 };
85 
86 template<>
89 {
90  enum
91  {
92  length = 0
93  };
94 };
95 
96 // MapReduce
97 template<
98  typename TL,
99  template <typename> class Map,
100  template <typename> class Reduce
101 >
102 struct MapReduce
103 {
104  typedef typename Map<char>::type map_type;
105  typedef typename Reduce<map_type>::result type;
106 
107  static type& value(type& result)
108  {
109  return MapReduce<typename TL::tail, Map, Reduce>::value(Reduce<map_type>::value(result, Map<typename TL::head>::value()));
110  }
111 
112  static type value()
113  {
114  type result;
115  return MapReduce<typename TL::tail, Map, Reduce>::value(Reduce<map_type>::value(result, Map<typename TL::head>::value()));
116  }
117 };
118 
119 template<
120  template <typename> class Map,
121  template <typename> class Reduce
122 >
123 struct MapReduce<TypeList<>, Map, Reduce>
124 {
125  typedef typename Map<char>::type map_type;
126  typedef typename Reduce<map_type>::result type;
127 
128  static type value()
129  {
130  return type();
131  }
132 
133  static type& value(type& result)
134  {
135  return result;
136  }
137 };
138 
140 {
141 public:
142 
143  TypeInfo();
144 
145  template<typename T>
146  explicit TypeInfo(T* /*dummy*/)
147  : m_Self(std::make_shared<Model<T> >())
148  {}
149 
150  bool operator==(const TypeInfo& other) const;
151 
152  template<typename T>
153  static TypeInfo New()
154  { T* dummy = nullptr; return TypeInfo(dummy); }
155 
156  QString GetName() const;
157  QList<TypeInfo> GetSuperclasses() const;
158 
159 
160 private:
161 
162  struct org_blueberry_core_runtime_EXPORT Concept {
163  virtual ~Concept();
164  virtual QString GetName() const = 0;
165  virtual QList<TypeInfo> GetSuperclasses() const = 0;
166  };
167 
168  template<typename T>
169  struct Model : Concept {
170  QString GetName() const override
171  { return GetClassName<T>(); }
172 
173  QList<TypeInfo> GetSuperclasses() const override;
174  };
175 
176  std::shared_ptr<const Concept> m_Self;
177 };
178 
179 template<typename T>
181 {
182  typedef TypeInfo type;
183  static type value()
184  {
185  return TypeInfo::New<T>();
186  }
187 };
188 
189 template<typename T>
191 {
192  typedef T type;
193  typedef QList<T> result;
194  static result& value(result& list, const type& item)
195  {
196  list.push_back(item);
197  return list;
198  }
199 };
200 
201 template<typename T>
203 {
204  typedef char Small;
205  struct Big { char dummy[2]; };
206 
207  template<typename U> static Small Test(typename U::SuperclassTypes*);
208  template<typename U> static Big Test(...);
209 public:
210  enum { value = sizeof(Test<T>(nullptr)) == sizeof(Small) };
211 };
212 
213 template<typename T, bool>
215 
216 template<typename T>
217 struct GetSuperclassTypeList<T, true> { typedef typename T::SuperclassTypes value; };
218 
219 template<typename T>
220 QList<TypeInfo> GetSuperclasses()
221 {
223 }
224 
225 template<typename T>
226 QList<TypeInfo> TypeInfo::Model<T>::GetSuperclasses() const
227 {
228  return ::berry::Reflection::GetSuperclasses<T>();
229 }
230 
231 } // end namespace Reflection
232 
233 } // end namespace berry
234 
235 #endif // BERRYREFLECTION_H
236 
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)