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