test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 std::string const emptyString("");
28 
30  return boost::regex_replace(iIn,reColons,emptyString,boost::format_perl);
31 
32  }
33 
35  return boost::regex_replace(boost::regex_replace(iIn,reBeginSpace,emptyString),
37  }
38 
40  return boost::regex_replace(iIn, reAllSpaces,emptyString);
41  }
42  static boost::regex const reWrapper("edm::Wrapper<(.*)>");
43  static boost::regex const reString("std::basic_string<char>");
44  static boost::regex const reSorted("edm::SortedCollection<(.*), *edm::StrictWeakOrdering<\\1 *> >");
45  static boost::regex const reUnsigned("unsigned ");
46  static boost::regex const reLong("long ");
47  static boost::regex const reVector("std::vector");
48  static boost::regex const reAIKR(", *edm::helper::AssociationIdenticalKeyReference"); //this is a default so can replaced with empty
49  //force first argument to also be the argument to edm::ClonePolicy so that if OwnVector is within
50  // a template it will not eat all the remaining '>'s
51  static boost::regex const reOwnVector("edm::OwnVector<(.*), *edm::ClonePolicy<\\1 *> >");
52 
53  //NOTE: the '?' means make the smallest match. This may lead to problems where the template arguments themselves have commas
54  // but we are using it in the cases where edm::AssociationMap appears multiple times in template arguments
55  static boost::regex const reOneToOne("edm::AssociationMap< *edm::OneToOne<(.*?),(.*?), *u[a-z]*> >");
56  static boost::regex const reOneToMany("edm::AssociationMap< *edm::OneToMany<(.*?),(.*?), *u[a-z]*> >");
57  static boost::regex const reOneToValue("edm::AssociationMap< *edm::OneToValue<(.*?),(.*?), *u[a-z]*> >");
58  static boost::regex const reOneToManyWithQuality("edm::AssociationMap<edm::OneToManyWithQuality<(.*?), *(.*?), *(.*?), *u[a-z]*> >");
59  static boost::regex const reToVector("edm::AssociationVector<(.*), *(.*), *edm::Ref.*,.*>");
60  //NOTE: if the item within a clone policy is a template, this substitution will probably fail
61  static boost::regex const reToRangeMap("edm::RangeMap< *(.*), *(.*), *edm::ClonePolicy<([^>]*)> >");
62  //NOTE: If container is a template with one argument which is its 'type' then can simplify name
63  static boost::regex const reToRefs1("edm::RefVector< *(.*)< *(.*) *>, *\\2 *, *edm::refhelper::FindUsingAdvance< *\\1< *\\2 *> *, *\\2 *> *>");
64  static boost::regex const reToRefs2("edm::RefVector< *(.*) *, *(.*) *, *edm::refhelper::FindUsingAdvance< *\\1, *\\2 *> *>");
65  static boost::regex const reToRefsAssoc("edm::RefVector< *Association(.*) *, *edm::helper(.*), *Association(.*)::Find>");
66 
68  using boost::regex_replace;
69  using boost::regex;
70  std::string name = regex_replace(iIn, reWrapper, "$1");
71  name = regex_replace(name,reAIKR,"");
72  name = regex_replace(name,reString,"String");
73  name = regex_replace(name,reSorted,"sSorted<$1>");
74  name = regex_replace(name,reUnsigned,"u");
75  name = regex_replace(name,reLong,"l");
76  name = regex_replace(name,reVector,"s");
77  name = regex_replace(name,reOwnVector,"sOwned<$1>");
78  name = regex_replace(name,reToVector,"AssociationVector<$1,To,$2>");
79  name = regex_replace(name,reOneToOne,"Association<$1,ToOne,$2>");
80  name = regex_replace(name,reOneToMany,"Association<$1,ToMany,$2>");
81  name = regex_replace(name,reOneToValue,"Association<$1,ToValue,$2>");
82  name = regex_replace(name,reOneToManyWithQuality,"Association<$1,ToMany,$2,WithQuantity,$3>");
83  name = regex_replace(name,reToRangeMap,"RangeMap<$1,$2>");
84  name = regex_replace(name,reToRefs1,"Refs<$1<$2>>");
85  name = regex_replace(name,reToRefs2,"Refs<$1,$2>");
86  name = regex_replace(name,reToRefsAssoc,"Refs<Association$1>");
87  //std::cout <<"standardRenames '"<<name<<"'"<<std::endl;
88  return name;
89  }
90 
93  using namespace boost;
95 
96  smatch theMatch;
97  if(regex_match(result,theMatch,reTemplateArgs)) {
98  //std::cout <<"found match \""<<theMatch.str(1) <<"\"" <<std::endl;
99  //static regex const templateClosing(">$");
100  //std::string aMatch = regex_replace(theMatch.str(1),templateClosing,"");
101  std::string aMatch = theMatch.str(1);
102  std::string theSub = handleTemplateArguments(aMatch);
103  regex const eMatch(std::string("(^[^<]*)<")+aMatch+">");
104  result = regex_replace(result,eMatch,theSub+"$1");
105  }
106  return result;
107  }
108 
110  using namespace boost;
112  bool shouldStop = false;
113  while(!shouldStop) {
114  if(std::string::npos != result.find_first_of("<")) {
115  smatch theMatch;
116  if(regex_search(result,theMatch,reTemplateClass)) {
117  std::string templateClass = theMatch.str(1);
118  std::string friendlierName = removeAllSpaces(subFriendlyName(templateClass));
119 
120  //std::cout <<" t: "<<templateClass <<" f:"<<friendlierName<<std::endl;
121  result = regex_replace(result, regex(templateClass),friendlierName);
122  } else {
123  //static regex const eComma(",");
124  //result = regex_replace(result,eComma,"");
125  std::cout <<" no template match for \""<<result<<"\""<<std::endl;
126  assert(0 =="failed to find a match for template class");
127  }
128  } else {
129  shouldStop=true;
130  }
131  }
132  result = regex_replace(result,reComma,"");
133  return result;
134  }
136  typedef tbb::concurrent_unordered_map<std::string, std::string> Map;
137  static Map s_fillToFriendlyName;
138  auto itFound = s_fillToFriendlyName.find(iFullName);
139  if(s_fillToFriendlyName.end()==itFound) {
140  itFound = s_fillToFriendlyName.insert(Map::value_type(iFullName, handleNamespaces(subFriendlyName(standardRenames(iFullName))))).first;
141  }
142  return itFound->second;
143  }
144  }
145 } // namespace edm
146 
static boost::regex const reOneToValue("edm::AssociationMap< *edm::OneToValue<(.*?),(.*?), *u[a-z]*> >")
static boost::regex const reTemplateArgs("[^<]*<(.*)>$")
static boost::regex const reOwnVector("edm::OwnVector<(.*), *edm::ClonePolicy<\\1 *> >")
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:39
std::string subFriendlyName(std::string const &iFullName)
Definition: FriendlyName.cc:92
static boost::regex const reTemplateClass("([^<>,]+<[^<>]*>)")
static boost::regex const reOneToMany("edm::AssociationMap< *edm::OneToMany<(.*?),(.*?), *u[a-z]*> >")
static boost::regex const reOneToOne("edm::AssociationMap< *edm::OneToOne<(.*?),(.*?), *u[a-z]*> >")
static boost::regex const reVector("std::vector")
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)
tuple result
Definition: query.py:137
static boost::regex const reToRefs1("edm::RefVector< *(.*)< *(.*) *>, *\\2 *, *edm::refhelper::FindUsingAdvance< *\\1< *\\2 *> *, *\\2 *> *>")
std::string handleNamespaces(std::string const &iIn)
Definition: FriendlyName.cc:29
Container::value_type value_type
static boost::regex const reAIKR(", *edm::helper::AssociationIdenticalKeyReference")
static boost::regex const reBeginSpace("^ +")
std::string standardRenames(std::string const &iIn)
Definition: FriendlyName.cc:67
std::string handleTemplateArguments(std::string const &)
static boost::regex const reComma(",")
static boost::regex const reColons("::")
static std::string const emptyString("")
static boost::regex const reString("std::basic_string<char>")
static boost::regex const reEndSpace(" +$")
static boost::regex const reWrapper("edm::Wrapper<(.*)>")
tuple cout
Definition: gather_cfg.py:121
std::string removeExtraSpaces(std::string const &iIn)
Definition: FriendlyName.cc:34
static boost::regex const reToRefsAssoc("edm::RefVector< *Association(.*) *, *edm::helper(.*), *Association(.*)::Find>")