TdZdd  1.1
A top-down/breadth-first decision diagram manipulation framework
demangle.hpp
1 /*
2  * TdZdd: a Top-down/Breadth-first Decision Diagram Manipulation Framework
3  * by Hiroaki Iwashita <iwashita@erato.ist.hokudai.ac.jp>
4  * Copyright (c) 2014 ERATO MINATO Project
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #pragma once
26 
27 #include <cstdlib>
28 #include <string>
29 #include <typeinfo>
30 
31 #ifdef __GNUC__
32 #include <cxxabi.h>
33 #endif
34 
35 namespace tdzdd {
36 
37 inline std::string demangle(char const* name) {
38 #ifdef __GNUC__
39  char* dName = abi::__cxa_demangle(name, 0, 0, 0);
40  if (dName == 0) return name;
41  char const* p = dName;
42 #else
43  char const* p = name;
44 #endif
45 
46  std::string s;
47 
48  for (char c = *p++; c; c = *p++) {
49  s += c;
50  if (!isalnum(c)) {
51  while (std::isspace(*p)) {
52  ++p;
53  }
54  }
55  }
56 
57 #ifdef __GNUC__
58  free(dName);
59 #endif
60  return s;
61 }
62 
63 inline std::string demangleTypename(char const* name) {
64  std::string s = demangle(name);
65  size_t i = 0;
66  size_t j = 0;
67 
68  while (j + 1 < s.size()) {
69  if (std::isalnum(s[j])) {
70  ++j;
71  }
72  else if (s[j] == ':' && s[j + 1] == ':') {
73  s = s.replace(i, j + 2 - i, "");
74  j = i;
75  }
76  else if (s[j] == '(') { // (anonymous namespace)
77  size_t k = j + 1;
78  while (k < s.size() && s[k++] != ')');
79  s = s.replace(j, k - j, "");
80  }
81  else {
82  i = ++j;
83  }
84  }
85 
86  return s;
87 }
88 
89 template<typename T>
90 std::string typenameof() {
91  return demangleTypename(typeid(T).name());
92 }
93 
94 template<typename T>
95 std::string typenameof(T const& obj) {
96  return demangleTypename(typeid(obj).name());
97 }
98 
99 } // namespace tdzdd