CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
format_type_name.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWLite
4 // Class : format_type_name
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author:
10 // Created: Thu Dec 3 16:52:50 CST 2009
11 //
12 
13 // system include files
14 #include <string>
15 #include "boost/static_assert.hpp"
16 
17 // user include files
19 
20 //
21 // constants, enums and typedefs
22 //
23 
24 //
25 // static data member definitions
26 //
27 
28 static const std::string s_symbolDemangled[] = {"::",
29  "_" ,
30  "," ,
31  " " ,
32  "&" ,
33  "*" ,
34  "<" ,
35  ">"
36  };
37 static const std::string s_symbolMangled[] = {"_1",
38  "_2",
39  "_3",
40  "_4",
41  "_7",
42  "_8",
43  "_9",
44  "_0"
45  };
46 static const unsigned int s_symbolToMangledSize = sizeof(s_symbolDemangled)/sizeof(std::string);
47 
48 namespace fwlite {
49 
50  void staticAssert() {
51  BOOST_STATIC_ASSERT(sizeof(s_symbolMangled) == sizeof(s_symbolDemangled));
52  }
53 
55  std::string format_type_to_mangled(const std::string& iType) {
56  std::string returnValue;
57  returnValue.append(static_cast<std::string::size_type>(iType.size()*2),' ');
58  std::string::size_type fromIndex=0;
59  std::string::size_type toIndex=0;
60  size_t sIndex=0;
61  for(;fromIndex<iType.size();++fromIndex) {
62  bool foundMatch = false;
63  for(sIndex=0;sIndex<s_symbolToMangledSize;) {
64  const std::string& symbol = s_symbolDemangled[sIndex];
65  if(iType.substr(fromIndex,symbol.size())==symbol) {
66  foundMatch = true;
67  break;
68  }
69  ++sIndex;
70  }
71  if(!foundMatch) {
72  returnValue[toIndex]=iType[fromIndex];
73  ++toIndex;
74  } else {
75  const std::string& mangled=s_symbolMangled[sIndex];
76  returnValue.replace(toIndex,mangled.size(),mangled);
77  toIndex += mangled.size();
78  fromIndex += s_symbolDemangled[sIndex].size()-1;
79  }
80  }
81  returnValue.resize(toIndex);
82  return returnValue;
83  }
84 
86  std::string unformat_mangled_to_type(const std::string& iMangled) {
87  std::string returnValue;
88  returnValue.append(static_cast<std::string::size_type>(iMangled.size()*2),' ');
89  std::string::size_type fromIndex=0;
90  std::string::size_type toIndex=0;
91  size_t sIndex=0;
92  for(;fromIndex<iMangled.size();++fromIndex) {
93  bool foundMatch = false;
94  for(sIndex=0;sIndex<s_symbolToMangledSize;) {
95  const std::string& mangled = s_symbolMangled[sIndex];
96  if(iMangled.substr(fromIndex,mangled.size())==mangled) {
97  foundMatch = true;
98  break;
99  }
100  ++sIndex;
101  }
102  if(!foundMatch) {
103  returnValue[toIndex]=iMangled[fromIndex];
104  ++toIndex;
105  } else {
106  const std::string& symbol=s_symbolDemangled[sIndex];
107  returnValue.replace(toIndex,symbol.size(),symbol);
108  toIndex += symbol.size();
109  fromIndex += s_symbolMangled[sIndex].size()-1;
110  }
111  }
112  returnValue.resize(toIndex);
113  return returnValue;
114  }
115 
116 }
void staticAssert()
uint16_t size_type
std::string unformat_mangled_to_type(const std::string &)
given a mangled name return the C++ class name
std::string format_type_to_mangled(const std::string &)
given a C++ class name returned a mangled name
static const std::string s_symbolDemangled[]
static const std::string s_symbolMangled[]
static const unsigned int s_symbolToMangledSize