CMS 3D CMS Logo

FriendlyName.cc
Go to the documentation of this file.
1 /*
2  * friendlyName.cpp
3  * CMSSW
4  *
5  * Created by Chris Jones on 2/24/06.
6  *
7  */
8 #include <string>
9 #include <regex>
10 #include <iostream>
11 #include <cassert>
12 #include "tbb/concurrent_unordered_map.h"
13 
14 //NOTE: This should probably be rewritten so that we break the class name into a tree where the template arguments are the node. On the way down the tree
15 // we look for '<' or ',' and on the way up (caused by finding a '>') we can apply the transformation to the output string based on the class name for the
16 // templated class. Up front we'd register a class name to a transformation function (which would probably take a std::vector<std::string> which holds
17 // the results of the node transformations)
18 
19 namespace edm {
20  namespace friendlyname {
21  static std::regex const reBeginSpace("^ +");
22  static std::regex const reEndSpace(" +$");
23  static std::regex const reAllSpaces(" +");
24  static std::regex const reColons("::");
25  static std::regex const reComma(",");
26  static std::regex const reTemplateArgs("[^<]*<(.*)>$");
27  static std::regex const reTemplateClass("([^<>,]+<[^<>]*>)");
28  static std::regex const rePointer("\\*");
29  static std::regex const reArray("\\[\\]");
30  static std::string const emptyString("");
31 
33  return std::regex_replace(iIn,reColons,emptyString);
34 
35  }
36 
38  return std::regex_replace(std::regex_replace(iIn,reBeginSpace,emptyString),
40  }
41 
43  return std::regex_replace(iIn, reAllSpaces,emptyString);
44  }
45  static std::regex const reWrapper("edm::Wrapper<(.*)>");
46  static std::regex const reString("std::basic_string<char>");
47  static std::regex const reString2("std::string");
48  static std::regex const reString3("std::basic_string<char,std::char_traits<char> >");
49  //The c++11 abi for gcc internally uses a different namespace for standard classes
50  static std::regex const reCXX11("std::__cxx11::");
51  static std::regex const reSorted("edm::SortedCollection<(.*), *edm::StrictWeakOrdering<\\1 *> >");
52  static std::regex const reclangabi("std::__1::");
53  static std::regex const reULongLong("ULong64_t");
54  static std::regex const reLongLong("Long64_t");
55  static std::regex const reUnsigned("unsigned ");
56  static std::regex const reLong("long ");
57  static std::regex const reVector("std::vector");
58  static std::regex const reSharedPtr("std::shared_ptr");
59  static std::regex const reUniquePtr("std::unique_ptr");
60  static std::regex const reAIKR(", *edm::helper::AssociationIdenticalKeyReference"); //this is a default so can replaced with empty
61  //force first argument to also be the argument to edm::ClonePolicy so that if OwnVector is within
62  // a template it will not eat all the remaining '>'s
63  static std::regex const reOwnVector("edm::OwnVector<(.*), *edm::ClonePolicy<\\1 *> >");
64 
65  //NOTE: the '?' means make the smallest match. This may lead to problems where the template arguments themselves have commas
66  // but we are using it in the cases where edm::AssociationMap appears multiple times in template arguments
67  static std::regex const reOneToOne("edm::AssociationMap< *edm::OneToOne<(.*?),(.*?), *u[a-z]*> >");
68  static std::regex const reOneToMany("edm::AssociationMap< *edm::OneToMany<(.*?),(.*?), *u[a-z]*> >");
69  static std::regex const reOneToValue("edm::AssociationMap< *edm::OneToValue<(.*?),(.*?), *u[a-z]*> >");
70  static std::regex const reOneToManyWithQuality("edm::AssociationMap<edm::OneToManyWithQuality<(.*?), *(.*?), *(.*?), *u[a-z]*> >");
71  static std::regex const reToVector("edm::AssociationVector<(.*), *(.*), *edm::Ref.*,.*>");
72  //NOTE: if the item within a clone policy is a template, this substitution will probably fail
73  static std::regex const reToRangeMap("edm::RangeMap< *(.*), *(.*), *edm::ClonePolicy<([^>]*)> >");
74  //NOTE: If container is a template with one argument which is its 'type' then can simplify name
75  static std::regex const reToRefs1("edm::RefVector< *(.*)< *(.*) *>, *\\2 *, *edm::refhelper::FindUsingAdvance< *\\1< *\\2 *> *, *\\2 *> *>");
76  static std::regex const reToRefs2("edm::RefVector< *(.*) *, *(.*) *, *edm::refhelper::FindUsingAdvance< *\\1, *\\2 *> *>");
77  static std::regex const reToRefsAssoc("edm::RefVector< *Association(.*) *, *edm::helper(.*), *Association(.*)::Find>");
78 
79 
81  using std::regex_replace;
82  using std::regex;
83  std::string name = regex_replace(iIn, reWrapper, "$1");
84  name = regex_replace(name,rePointer,"ptr");
85  name = regex_replace(name,reArray,"As");
86  name = regex_replace(name,reAIKR,"");
87  name = regex_replace(name,reclangabi,"std::");
88  name = regex_replace(name,reCXX11,"std::");
89  name = regex_replace(name,reString,"String");
90  name = regex_replace(name,reString2,"String");
91  name = regex_replace(name,reString3,"String");
92  name = regex_replace(name,reSorted,"sSorted<$1>");
93  name = regex_replace(name,reULongLong,"ull");
94  name = regex_replace(name,reLongLong,"ll");
95  name = regex_replace(name,reUnsigned,"u");
96  name = regex_replace(name,reLong,"l");
97  name = regex_replace(name,reVector,"s");
98  name = regex_replace(name,reSharedPtr,"SharedPtr");
99  name = regex_replace(name,reUniquePtr,"UniquePtr");
100  name = regex_replace(name,reOwnVector,"sOwned<$1>");
101  name = regex_replace(name,reToVector,"AssociationVector<$1,To,$2>");
102  name = regex_replace(name,reOneToOne,"Association<$1,ToOne,$2>");
103  name = regex_replace(name,reOneToMany,"Association<$1,ToMany,$2>");
104  name = regex_replace(name,reOneToValue,"Association<$1,ToValue,$2>");
105  name = regex_replace(name,reOneToManyWithQuality,"Association<$1,ToMany,$2,WithQuantity,$3>");
106  name = regex_replace(name,reToRangeMap,"RangeMap<$1,$2>");
107  name = regex_replace(name,reToRefs1,"Refs<$1<$2>>");
108  name = regex_replace(name,reToRefs2,"Refs<$1,$2>");
109  name = regex_replace(name,reToRefsAssoc,"Refs<Association$1>");
110  //std::cout <<"standardRenames '"<<name<<"'"<<std::endl;
111  return name;
112  }
113 
116  using namespace std;
117  std::string result = removeExtraSpaces(iFullName);
118 
119  smatch theMatch;
120  if(regex_match(result,theMatch,reTemplateArgs)) {
121  //std::cout <<"found match \""<<theMatch.str(1) <<"\"" <<std::endl;
122  //static regex const templateClosing(">$");
123  //std::string aMatch = regex_replace(theMatch.str(1),templateClosing,"");
124  std::string aMatch = theMatch.str(1);
125  std::string theSub = handleTemplateArguments(aMatch);
126  regex const eMatch(std::string("(^[^<]*)<")+aMatch+">");
127  result = regex_replace(result,eMatch,theSub+"$1");
128  }
129  return result;
130  }
131 
133  using namespace std;
135  bool shouldStop = false;
136  while(!shouldStop) {
137  if(std::string::npos != result.find_first_of("<")) {
138  smatch theMatch;
139  if(regex_search(result,theMatch,reTemplateClass)) {
140  std::string templateClass = theMatch.str(1);
141  std::string friendlierName = removeAllSpaces(subFriendlyName(templateClass));
142 
143  //std::cout <<" t: "<<templateClass <<" f:"<<friendlierName<<std::endl;
144  result = regex_replace(result, regex(templateClass),friendlierName);
145  } else {
146  //static regex const eComma(",");
147  //result = regex_replace(result,eComma,"");
148  std::cout <<" no template match for \""<<result<<"\""<<std::endl;
149  assert(nullptr =="failed to find a match for template class");
150  }
151  } else {
152  shouldStop=true;
153  }
154  }
155  result = regex_replace(result,reComma,"");
156  return result;
157  }
159  typedef tbb::concurrent_unordered_map<std::string, std::string> Map;
160  static Map s_fillToFriendlyName;
161  auto itFound = s_fillToFriendlyName.find(iFullName);
162  if(s_fillToFriendlyName.end()==itFound) {
163  itFound = s_fillToFriendlyName.insert(Map::value_type(iFullName, handleNamespaces(subFriendlyName(standardRenames(iFullName))))).first;
164  }
165  return itFound->second;
166  }
167  }
168 } // namespace edm
169 
static std::regex const reToRefs1("edm::RefVector< *(.*)< *(.*) *>, *\\2 *, *edm::refhelper::FindUsingAdvance< *\\1< *\\2 *> *, *\\2 *> *>")
static std::regex const reEndSpace(" +$")
static std::regex const reToRangeMap("edm::RangeMap< *(.*), *(.*), *edm::ClonePolicy<([^>]*)> >")
static std::regex const reCXX11("std::__cxx11::")
static std::regex const reAIKR(", *edm::helper::AssociationIdenticalKeyReference")
static std::regex const reString3("std::basic_string<char,std::char_traits<char> >")
static std::regex const reUnsigned("unsigned ")
std::string removeAllSpaces(std::string const &iIn)
Definition: FriendlyName.cc:42
std::string subFriendlyName(std::string const &iFullName)
static std::regex const reSharedPtr("std::shared_ptr")
static std::regex const rePointer("\\*")
static std::regex const reOneToManyWithQuality("edm::AssociationMap<edm::OneToManyWithQuality<(.*?), *(.*?), *(.*?), *u[a-z]*> >")
static std::regex const reToVector("edm::AssociationVector<(.*), *(.*), *edm::Ref.*,.*>")
static std::regex const reLong("long ")
static std::regex const reOneToMany("edm::AssociationMap< *edm::OneToMany<(.*?),(.*?), *u[a-z]*> >")
static std::regex const reOwnVector("edm::OwnVector<(.*), *edm::ClonePolicy<\\1 *> >")
static std::regex const reSorted("edm::SortedCollection<(.*), *edm::StrictWeakOrdering<\\1 *> >")
static std::regex const reLongLong("Long64_t")
static std::regex const reComma(",")
std::string friendlyName(std::string const &iFullName)
static std::regex const reOneToOne("edm::AssociationMap< *edm::OneToOne<(.*?),(.*?), *u[a-z]*> >")
std::string handleNamespaces(std::string const &iIn)
Definition: FriendlyName.cc:32
static std::regex const reWrapper("edm::Wrapper<(.*)>")
static std::regex const reArray("\\[\\]")
static std::regex const reTemplateArgs("[^<]*<(.*)>$")
static std::regex const reToRefsAssoc("edm::RefVector< *Association(.*) *, *edm::helper(.*), *Association(.*)::Find>")
static std::regex const reULongLong("ULong64_t")
static std::regex const reVector("std::vector")
std::string standardRenames(std::string const &iIn)
Definition: FriendlyName.cc:80
static std::regex const reUniquePtr("std::unique_ptr")
static std::regex const reColons("::")
std::string handleTemplateArguments(std::string const &)
static std::regex const reTemplateClass("([^<>,]+<[^<>]*>)")
static std::string const emptyString("")
HLT enums.
static std::regex const reBeginSpace("^ +")
static std::regex const reclangabi("std::__1::")
static std::regex const reString2("std::string")
static std::regex const reString("std::basic_string<char>")
static std::regex const reAllSpaces(" +")
static std::regex const reOneToValue("edm::AssociationMap< *edm::OneToValue<(.*?),(.*?), *u[a-z]*> >")
static std::regex const reToRefs2("edm::RefVector< *(.*) *, *(.*) *, *edm::refhelper::FindUsingAdvance< *\\1, *\\2 *> *>")
std::string removeExtraSpaces(std::string const &iIn)
Definition: FriendlyName.cc:37