libDwm-0.6.1
DwmTypeName.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.1/include/DwmTypeName.hh 9017 $
3 // @(#) $Id: DwmTypeName.hh 9017 2017-04-11 17:51:09Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2006-2007
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. The names of the authors and copyright holders may not be used to
18 // endorse or promote products derived from this software without
19 // specific prior written permission.
20 //
21 // IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
22 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
23 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
24 // EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
25 // DAMAGE.
26 //
27 // THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
28 // DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
29 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
30 // REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
31 // IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
33 // OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
34 // TRADEMARK OR OTHER RIGHTS.
35 //===========================================================================
36 
37 //---------------------------------------------------------------------------
42 //---------------------------------------------------------------------------
43 
44 #ifndef _DWMTYPENAME_HH_
45 #define _DWMTYPENAME_HH_
46 
47 #include <cxxabi.h>
48 
49 /*
50 extern "C" {
52  char *__cxa_demangle(const char *, char *, size_t *, int *);
53 }
54 */
55 
56 #include <cstdlib>
57 #include <string>
58 #include <typeinfo>
59 
60 using namespace abi;
61 
62 namespace Dwm {
63 
64  //--------------------------------------------------------------------------
68  //--------------------------------------------------------------------------
69  std::string BuiltinTypeName(const std::type_info & typeInfo);
70 
71  //--------------------------------------------------------------------------
73  //--------------------------------------------------------------------------
74  template <typename T>
75  std::string BuiltinTypeName()
76  {
77  return(BuiltinTypeName(typeid(T)));
78  }
79 
80  //------------------------------------------------------------------------
82  //------------------------------------------------------------------------
83  template <typename T>
84  std::string TypeName(const T & x)
85  {
86  static const std::string rcsid = "@(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.1/include/DwmTypeName.hh 9017 $";
87 
88  std::string rc;
89  int demangleStatus = -1;
90  char *demangledName =
91  abi::__cxa_demangle(typeid(x).name(), 0, 0, &demangleStatus);
92  if (demangledName) {
93 // if (demangleStatus == 0) {
94  rc = demangledName;
95  if (rc == "unsigned") {
96  rc += " int";
97  }
98 // }
99  free(demangledName);
100  }
101  else {
102  // __cxa_demangle() returns NULL for built-in types, with a -2 status
103  if (demangleStatus == -2) {
104  rc = BuiltinTypeName<T>();
105  }
106  }
107 
108  return(rc);
109  }
110 
111  //------------------------------------------------------------------------
113  //------------------------------------------------------------------------
114  template <typename T>
115  std::string TypeNameNoNamespace(const T & x)
116  {
117  std::string rc;
118  int demangleStatus;
119  char *demangledName =
120  __cxa_demangle(typeid(x).name(), 0, 0, &demangleStatus);
121  if (demangledName) {
122  if (demangleStatus == 0) {
123  rc = demangledName;
124  if (rc == "unsigned") {
125  rc += " int";
126  }
127  }
128  free(demangledName);
129  }
130  else {
131  // __cxa_demangle() returns NULL for built-in types, with a -2 status
132  if (demangleStatus == -2) {
133  rc = BuiltinTypeName<T>();
134  }
135  }
136 
137  std::string::size_type idx = rc.find_last_of("::");
138  if (idx != rc.npos) {
139  rc = rc.substr(idx + 1);
140  }
141 
142  return(rc);
143  }
144 
145  //--------------------------------------------------------------------------
147  //--------------------------------------------------------------------------
148  template <typename T>
149  std::string TypeName()
150  {
151  std::string rc;
152  int demangleStatus;
153  char *demangledName =
154  __cxa_demangle(typeid(T).name(), 0, 0, &demangleStatus);
155  if (demangledName) {
156  if (demangleStatus == 0) {
157  rc = demangledName;
158  }
159  free(demangledName);
160  }
161  else {
162  // __cxa_demangle() returns NULL for built-in types, with a -2 status
163  if (demangleStatus == -2) {
164  rc = BuiltinTypeName<T>();
165  }
166  }
167 
168  return(rc);
169  }
170 
171 } // namespace Dwm
172 
173 #endif // _DWMTYPENAME_HH_
174 
175 //---------------------------- emacs settings -----------------------------
176 // Local Variables:
177 // mode: C++/la
178 // tab-width: 2
179 // indent-tabs-mode: nil
180 // c-basic-offset: 2
181 // End:
182 //-------------------------------------------------------------------------
std::string BuiltinTypeName(const std::type_info &typeInfo)
Returns a string holding the demangled name for the given typeInfo.
std::string TypeName(const T &x)
Returns the name of type T.
Definition: DwmTypeName.hh:84
Definition: DwmBZ2IO.hh:67
std::string TypeNameNoNamespace(const T &x)
Returns the name of type T, sans its namespace.
Definition: DwmTypeName.hh:115