fw4spl
Demangler.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include <string>
8 #include <vector>
9 #include <typeinfo>
10 
11 #ifndef _WIN32
12 #include <cxxabi.h>
13 #include <cstdlib>
14 #endif
15 
16 #include "fwCore/Demangler.hpp"
17 
18 namespace fwCore
19 {
20 
21 #define COLONS std::string("::")
22 #define LT std::string("<")
23 
24 //------------------------------------------------------------------------------
25 
26 Demangler::Demangler(const std::type_info& t) :
27  m_name(t.name())
28 {
29 }
30 
31 //------------------------------------------------------------------------------
32 
33 Demangler::Demangler(const std::string& s) :
34  m_name(s)
35 {
36 }
37 
38 //------------------------------------------------------------------------------
39 
41 {
42 }
43 
44 //------------------------------------------------------------------------------
45 
46 std::string Demangler::getLeafClassname() const
47 {
48  std::string demangled(this->demangle());
49 
50  const size_t lt_pos = demangled.find(LT);
51  size_t colons_pos = demangled.rfind(COLONS, lt_pos);
52 
53  colons_pos = (colons_pos == std::string::npos) ? 0 : colons_pos+COLONS.size();
54  return demangled.replace(0, colons_pos, "");
55 }
56 
57 //------------------------------------------------------------------------------
58 
59 std::string Demangler::getClassname() const
60 {
61  const std::string demangled(this->demangle());
62  return COLONS + demangled;
63 }
64 
65 //------------------------------------------------------------------------------
66 
67 std::string Demangler::demangle() const
68 {
69  const char* mangled = m_name.c_str();
70 #ifndef _WIN32
71  char* c_demangled = abi::__cxa_demangle( mangled, 0, 0, 0);
72  std::string res;
73  if (c_demangled)
74  {
75  res = c_demangled;
76  free(c_demangled);
77  return res;
78  }
79  else
80  {
81  res = mangled;
82  }
83  return res;
84 #else
85  static std::vector<std::string> keywords;
86  typedef std::vector<std::string>::iterator keyword_iterator;
87  if ( keywords.empty() )
88  {
89  keywords.push_back("__cdecl");
90  keywords.push_back("class ");
91  keywords.push_back("enum ");
92  keywords.push_back("struct ");
93  keywords.push_back("union ");
94  }
95  std::string res(mangled);
96  for (keyword_iterator iter = keywords.begin(); iter != keywords.end(); ++iter )
97  {
98  while (res.find(*iter) != std::string::npos)
99  {
100  res = res.replace(res.find(*iter), iter->size(), "");
101  }
102  while (res.find(" *") != std::string::npos)
103  {
104  res = res.replace(res.find(" *"), 2, "*");
105  }
106  while (res.find(" &") != std::string::npos)
107  {
108  res = res.replace(res.find(" &"), 2, "&");
109  }
110  }
111  return res;
112 #endif
113 }
114 
115 //------------------------------------------------------------------------------
116 
117 #undef COLONS
118 #undef LT
119 
120 } //namespace fwCore
FWCORE_API std::string getClassname() const
Returns "::some::long::namespace::Object" from "::some::long::namespace::Object". ...
Definition: Demangler.cpp:59
This namespace fwCore provides common foundations for FW4SPL.
Definition: BaseObject.hpp:16
virtual FWCORE_API ~Demangler()
Destructor.
Definition: Demangler.cpp:40
FWCORE_API Demangler(const std::type_info &t)
Constructor from a typeinfo object.
Definition: Demangler.cpp:26
FWCORE_API std::string getLeafClassname() const
Returns "Object" from "::some::long::namespace::Object".
Definition: Demangler.cpp:46
std::string demangle() const
Process the name to demangle and return the same string whatever the OS is.
Definition: Demangler.cpp:67
const std::string m_name
Store the name to demangle.
Definition: Demangler.hpp:80