Medical Imaging Interaction Toolkit  2024.06.00
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 
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  {
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 
berry::Reflection::TypeInfo::TypeInfo
TypeInfo(T *)
Definition: berryReflection.h:146
org_blueberry_core_runtime_Export.h
berry::Reflection::MapToTypeInfo::type
TypeInfo type
Definition: berryReflection.h:182
berry::Reflection::DemangleName
org_blueberry_core_runtime_EXPORT QString DemangleName(const char *typeName)
berry::Reflection::GetClassName
org_blueberry_core_runtime_EXPORT QString GetClassName(const Object *obj)
berry::Reflection::ReduceToList::type
T type
Definition: berryReflection.h:192
berry::Reflection::TypeList::tail
TypeList< T1, T2, T3, T4, T5, T6, T7, T8, T9 > tail
Definition: berryReflection.h:79
berry::Reflection::MapReduce::type
Reduce< map_type >::result type
Definition: berryReflection.h:105
berry::Reflection::TypeList::length
@ length
Definition: berryReflection.h:82
berry::Reflection::ReduceToList
Definition: berryReflection.h:190
berry::Reflection::TypeInfo::New
static TypeInfo New()
Definition: berryReflection.h:153
berry::Reflection::MapReduce::map_type
Map< char >::type map_type
Definition: berryReflection.h:104
berry::Reflection::MapToTypeInfo::value
static type value()
Definition: berryReflection.h:183
berry::Object
Light weight base class for most BlueBerry classes.
Definition: berryObject.h:72
berry::Reflection::MapReduce::value
static type value()
Definition: berryReflection.h:112
berry::Reflection::MapReduce< TypeList<>, Map, Reduce >::value
static type & value(type &result)
Definition: berryReflection.h:133
berry::Reflection::HasTypeSuperclass
Definition: berryReflection.h:202
org_blueberry_core_runtime_EXPORT
#define org_blueberry_core_runtime_EXPORT
Definition: org_blueberry_core_runtime_Export.h:26
berry::Reflection::MapReduce::value
static type & value(type &result)
Definition: berryReflection.h:107
berry::Reflection::TypeInfo
Definition: berryReflection.h:139
berry::Reflection::MapReduce
Definition: berryReflection.h:102
berry::Reflection::TypeList::head
T0 head
Definition: berryReflection.h:78
berry::Reflection::GetSuperclassTypeList::value
TypeList value
Definition: berryReflection.h:214
mitk::operator==
MITKCORE_EXPORT bool operator==(const InteractionEvent &a, const InteractionEvent &b)
berry::Reflection::MapReduce< TypeList<>, Map, Reduce >::map_type
Map< char >::type map_type
Definition: berryReflection.h:125
berry::Reflection::HasTypeSuperclass::value
@ value
Definition: berryReflection.h:210
berry::Reflection::TypeList
Definition: berryReflection.h:63
berry::Reflection::GetSuperclasses
QList< TypeInfo > GetSuperclasses()
Definition: berryReflection.h:220
berry::Reflection::ReduceToList::result
QList< T > result
Definition: berryReflection.h:193
berry::Reflection::GetSuperclassTypeList
Definition: berryReflection.h:214
berry::Reflection::EmptyType
Definition: berryReflection.h:50
berry::Reflection::MapToTypeInfo
Definition: berryReflection.h:180
berry::Reflection::ReduceToList::value
static result & value(result &list, const type &item)
Definition: berryReflection.h:194
berry::Reflection::MapReduce< TypeList<>, Map, Reduce >::value
static type value()
Definition: berryReflection.h:128
berry::Reflection::MapReduce< TypeList<>, Map, Reduce >::type
Reduce< map_type >::result type
Definition: berryReflection.h:126
berry
Definition: QmitkPropertyItemModel.h:24
berry::Reflection::GetSuperclassTypeList< T, true >::value
T::SuperclassTypes value
Definition: berryReflection.h:217