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