Medical Imaging Interaction Toolkit  2018.4.99-6a3ea89d
Medical Imaging Interaction Toolkit
berryReflection.cpp
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 #include "berryReflection.h"
14 
15 #include "berryObject.h"
16 
17 #include <QStringList>
18 
19 // Better name demangling for gcc
20 #if __GNUC__ > 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ > 0 )
21 #define GCC_USEDEMANGLE
22 #endif
23 
24 #ifdef GCC_USEDEMANGLE
25 #include <cstdlib>
26 #include <cxxabi.h>
27 #endif
28 
29 #if defined(_WIN32) && defined(NDEBUG)
30 // exported from VC CRT
31 extern "C"
32 char * __unDName(char * outputString, const char * name, int maxStringLength,
33  void * (* pAlloc )(size_t), void (* pFree )(void *),
34  unsigned short disableFlags);
35 #endif
36 
37 namespace berry {
38 
39 namespace Reflection {
40 
41 QString DemangleName(const char* mangledName)
42 {
43  QString name(mangledName);
44 #ifdef GCC_USEDEMANGLE
45  int status;
46  char* unmangled = abi::__cxa_demangle(mangledName, nullptr, nullptr, &status);
47 
48  if(status == 0)
49  {
50  name = QString(unmangled);
51  free(unmangled);
52  }
53 #elif defined(_WIN32) && defined(NDEBUG)
54  char * const unmangled = __unDName(0, mangledName, 0, malloc, free, 0x2800);
55  if (unmangled)
56  {
57  QString unmangledName(unmangled);
58  name = unmangledName.split(' ', QString::SkipEmptyParts).back();
59  free(unmangled);
60  }
61 #else
62  name = name.split(' ', QString::SkipEmptyParts).back();
63 #endif
64  return name;
65 }
66 
67 #ifdef GetClassName
68 // clash with WinUser.h definition
69 #undef GetClassName
70 #endif
71 
72 QString GetClassName(const Object* obj)
73 {
74  return DemangleName(typeid(*const_cast<Object*>(obj)).name());
75 }
76 
77 TypeInfo::Concept::~Concept(){}
78 
79 template<>
80 struct TypeInfo::Model<EmptyType> : Concept {
81  QString GetName() const override { return QString(); }
82  QList<TypeInfo> GetSuperclasses() const override { return QList<TypeInfo>(); }
83 };
84 
86  : m_Self(std::make_shared<Model<EmptyType> >())
87 {
88 }
89 
90 bool TypeInfo::operator==(const TypeInfo& other) const
91 {
92  return this->GetName() == other.GetName();
93 }
94 
95 QString TypeInfo::GetName() const
96 {
97  return m_Self->GetName();
98 }
99 
100 QList<TypeInfo> TypeInfo::GetSuperclasses() const
101 {
102  return m_Self->GetSuperclasses();
103 }
104 
105 }
106 
107 }
QString GetClassName(const Object *obj)
Light weight base class for most BlueBerry classes.
Definition: berryObject.h:72
STL namespace.
QString DemangleName(const char *mangledName)
QList< TypeInfo > GetSuperclasses() const
bool operator==(const TypeInfo &other) const