CMS 3D CMS Logo

DDLMap.cc
Go to the documentation of this file.
4 
5 // Boost parser, spirit, for parsing the std::vector elements.
6 #include "boost/spirit/home/classic/core/non_terminal/grammar.hpp"
7 #include "boost/spirit/include/classic.hpp"
8 
9 #include <cstddef>
10 #include <utility>
11 
12 class DDCompactView;
13 
14 class MapPair {
15 public:
16  MapPair(DDLMap* iMap) : map_{iMap} {};
17  void operator()(char const* str, char const* end) const;
18 
19 private:
21 };
22 
23 class MapMakeName {
24 public:
25  MapMakeName(DDLMap* iMap) : map_{iMap} {};
26  void operator()(char const* str, char const* end) const;
27 
28 private:
30 };
31 
33 public:
34  MapMakeDouble(DDLMap* iMap) : map_{iMap} {};
35  void operator()(char const* str, char const* end) const;
36 
37 private:
39 };
40 
41 class Mapper : public boost::spirit::classic::grammar<Mapper> {
42 public:
43  Mapper(DDLMap* iMap) : map_{iMap} {};
44  template <typename ScannerT>
45  struct definition;
46 
47  MapPair mapPair() const { return MapPair(map_); }
48  MapMakeName mapMakeName() const { return MapMakeName(map_); }
50 
51 private:
53 };
54 
55 namespace boost {
56  namespace spirit {
57  namespace classic {}
58  } // namespace spirit
59 } // namespace boost
60 
61 using namespace boost::spirit::classic;
62 
64 
65 template <typename ScannerT>
66 struct Mapper::definition {
67  definition(Mapper const& self) {
68  mapSet = ppair[self.mapPair()] >> *((',' >> ppair)[self.mapPair()]);
69 
70  ppair = name >> ch_p('=') >> value;
71 
72  name = (alpha_p >> *alnum_p)[self.mapMakeName()];
73 
74  value = (+(anychar_p - ','))[self.mapMakeDouble()];
75  }
76 
77  rule<ScannerT> mapSet, ppair, name, value;
78 
79  rule<ScannerT> const& start() const { return mapSet; }
80 };
81 
82 void MapPair::operator()(char const* str, char const* end) const { map_->do_pair(str, end); }
83 
84 void MapMakeName::operator()(char const* str, char const* end) const { map_->do_makeName(str, end); }
85 
86 void MapMakeDouble::operator()(char const* str, char const* end) const { map_->do_makeDouble(str, end); }
87 
89  pName = "";
90  pMap.clear();
91  //pMapMap.clear(); only the DDLAlgorithm is allowed to clear this guy!
92  pDouble = 0.0;
93  pNameSpace = nmspace;
94 }
95 
96 void DDLMap::processElement(const std::string& name, const std::string& nmspace, DDCompactView& cpv) {
97  std::string tTextToParse = getText();
99  std::string tName = atts.find("name")->second;
100 
101  if (tTextToParse.empty()) {
102  errorOut("No std::string to parse!");
103  }
104 
105  // NOT IMPLEMENTED YET
106  if (atts.find("type") != atts.end() && atts.find("type")->second == "string") {
107  errorOut("Map of type std::string is not supported yet.");
108  }
109 
110  Mapper mapGrammar{this};
111 
112  pMap.clear();
113 
114  parse_info<> info = boost::spirit::classic::parse(tTextToParse.c_str(), mapGrammar >> end_p, space_p);
115  if (!info.full) {
116  errorOut("Does not conform to name=value, name=value... etc. of ddl Map element.");
117  }
118 
119  if (parent() == "Algorithm" || parent() == "SpecPar") {
120  pMapMap[tName] = pMap;
121  } else if (parent() == "ConstantsSection" || parent() == "DDDefinition") {
122  dd_map_type tMap;
123  for (std::map<std::string, double>::const_iterator it = pMap.begin(); it != pMap.end(); ++it) {
124  tMap[it->first] = it->second;
125  }
126  DDMap m(getDDName(pNameSpace), std::make_unique<dd_map_type>(tMap));
127  // clear the map of maps, because in these elements we only have ONE at a time.
128  pMapMap.clear();
129  }
130 
131  std::string nEntries = atts.find("nEntries")->second;
132  if (pMap.size() != size_t(myRegistry_->evaluator().eval(pNameSpace, nEntries))) {
133  errorOut("Number of entries found in Map text does not match number in attribute nEntries.");
134  }
135  clear();
136 }
137 
138 void DDLMap::do_pair(char const* str, char const* end) { pMap[pName] = pDouble; }
139 
140 void DDLMap::do_makeName(char const* str, char const* end) { pName = std::string(str, end); }
141 
142 void DDLMap::do_makeDouble(char const* str, char const* end) {
143  std::string ts(str, end);
145 }
146 
147 void DDLMap::errorOut(const char* str) {
148  std::string msg("\nDDLMap: Failed to parse the following: \n");
149  msg += std::string(str);
150  msg += "\n as a Map element (comma separated list of name=value).";
151  throwError(msg);
152 }
153 
MapPair(DDLMap *iMap)
Definition: DDLMap.cc:16
vector< string > parse(string line, const string &delimiter)
static const TGPicture * info(bool iBackgroundIsBlack)
DDLElementRegistry * myRegistry_
Definition: DDXMLElement.h:173
DDLMap(DDLElementRegistry *myreg)
Definition: DDLMap.cc:63
Definition: CLHEP.h:16
Mapper(DDLMap *iMap)
Definition: DDLMap.cc:43
std::string pNameSpace
Definition: DDLMap.h:51
std::string pName
Definition: DDLMap.h:50
ReadMapType< std::map< std::string, double > > & getMapOfMaps(void)
Definition: DDLMap.cc:154
void processElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv) override
Processing the element.
Definition: DDLMap.cc:96
MapMakeDouble mapMakeDouble() const
Definition: DDLMap.cc:49
void operator()(char const *str, char const *end) const
Definition: DDLMap.cc:84
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
DDLMap * map_
Definition: DDLMap.cc:29
a named constant corresponding to the DDL-XML tag <Constant> and <ConstantsVector> ...
Definition: DDMap.h:22
std::map< std::string, std::string > DDXMLAttribute
Definition: DDXMLElement.h:45
virtual std::vector< DDXMLAttribute >::const_iterator end(void)
rule< ScannerT > mapSet
Definition: DDLMap.cc:77
MapMakeName(DDLMap *iMap)
Definition: DDLMap.cc:25
rule< ScannerT > name
Definition: DDLMap.cc:77
ClhepEvaluator & evaluator()
MapPair mapPair() const
Definition: DDLMap.cc:47
virtual const DDXMLAttribute & getAttributeSet(size_t aIndex=0) const
Get a "row" of attributes, i.e. one attribute set.
Definition: DDXMLElement.cc:54
definition(Mapper const &self)
Definition: DDLMap.cc:67
void do_makeName(char const *str, char const *end)
Definition: DDLMap.cc:140
void operator()(char const *str, char const *end) const
Definition: DDLMap.cc:86
void preProcessElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv) override
Called by loadAttributes AFTER attributes are loaded.
Definition: DDLMap.cc:88
rule< ScannerT > value
Definition: DDLMap.cc:77
Definition: value.py:1
MapMakeDouble(DDLMap *iMap)
Definition: DDLMap.cc:34
Definition: DDLMap.cc:41
rule< ScannerT > ppair
Definition: DDLMap.cc:77
void do_pair(char const *str, char const *end)
Definition: DDLMap.cc:138
ReadMapType< std::map< std::string, double > > pMapMap
Definition: DDLMap.h:48
DDLMap handles Map container.
Definition: DDLMap.h:33
This is a base class for processing XML elements in the DDD.
Definition: DDXMLElement.h:48
tuple msg
Definition: mps_check.py:285
DDLMap * map_
Definition: DDLMap.cc:52
double pDouble
Definition: DDLMap.h:49
void errorOut(const char *str)
Definition: DDLMap.cc:147
void operator()(char const *str, char const *end) const
Definition: DDLMap.cc:82
const std::string & parent(void) const
access to parent element name
double eval(const std::string &ns, const std::string &expr)
The main class for processing parsed elements.
virtual void clear(void)
clear this element&#39;s contents.
Definition: DDXMLElement.cc:40
DDLMap * map_
Definition: DDLMap.cc:38
void throwError(const std::string &keyMessage) const
format std::string for throw an error.
void do_makeDouble(char const *str, char const *end)
Definition: DDLMap.cc:142
#define str(s)
MapMakeName mapMakeName() const
Definition: DDLMap.cc:48
virtual const DDName getDDName(const std::string &defaultNS, const std::string &attname=std::string("name"), size_t aIndex=0)
Definition: DDXMLElement.cc:56
rule< ScannerT > const & start() const
Definition: DDLMap.cc:79
dd_map_type pMap
Definition: DDLMap.h:47
DDLMap * map_
Definition: DDLMap.cc:20
const std::string getText(size_t tindex=0) const
retrieve the text blob.