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 private:
20 };
21 
22 class MapMakeName {
23 public:
24  MapMakeName(DDLMap* iMap):map_{iMap} { };
25  void operator()(char const* str, char const* end) const;
26 private:
28 };
29 
31 public:
32  MapMakeDouble(DDLMap* iMap): map_{iMap} { };
33  void operator()(char const* str, char const* end) const;
34 private:
36 };
37 
38 class Mapper : public boost::spirit::classic::grammar<Mapper> {
39 public:
40  Mapper(DDLMap* iMap):map_{iMap} { };
41  template <typename ScannerT> struct definition;
42 
43  MapPair mapPair() const { return MapPair(map_); }
44  MapMakeName mapMakeName() const { return MapMakeName(map_); }
46 
47 private:
49 };
50 
51 
52 namespace boost { namespace spirit { namespace classic {} } }
53 
54 using namespace boost::spirit::classic;
55 
57  : DDXMLElement( myreg )
58 {}
59 
60 template <typename ScannerT> struct Mapper::definition
61 {
62  definition(Mapper const& self)
63  {
64  mapSet
65  = ppair[self.mapPair()]
66  >> *((',' >> ppair)[self.mapPair()])
67  ;
68 
69  ppair
70  = name
71  >> ch_p('=') >> value
72  ;
73 
74  name
75  = (alpha_p >> *alnum_p)[self.mapMakeName()]
76  ;
77 
78  value
79  = (+(anychar_p - ','))[self.mapMakeDouble()]
80  ;
81  }
82 
83  rule<ScannerT> mapSet, ppair, name, value;
84 
85  rule<ScannerT> const&
86  start() const { return mapSet; }
87 };
88 
89 void
90 MapPair::operator() (char const* str, char const* end) const
91 {
92  map_->do_pair(str, end);
93 }
94 
95 void
96 MapMakeName::operator() (char const* str, char const* end) const
97 {
98  map_->do_makeName(str, end);
99 }
100 
101 void
102 MapMakeDouble::operator() (char const* str, char const* end)const
103 {
104  map_->do_makeDouble(str, end);
105 }
106 
107 void
109 {
110  pName = "";
111  pMap.clear();
112  //pMapMap.clear(); only the DDLAlgorithm is allowed to clear this guy!
113  pDouble = 0.0;
114  pNameSpace = nmspace;
115 }
116 
117 void
119 {
120  std::string tTextToParse = getText();
122  std::string tName = atts.find("name")->second;
123 
124  if (tTextToParse.empty())
125  {
126  errorOut("No std::string to parse!");
127  }
128 
129  // NOT IMPLEMENTED YET
130  if (atts.find("type") != atts.end() && atts.find("type")->second == "string")
131  {
132  errorOut("Map of type std::string is not supported yet.");
133  }
134 
135  Mapper mapGrammar{this};
136 
137  pMap.clear();
138 
139  parse_info<> info = boost::spirit::classic::parse(tTextToParse.c_str(), mapGrammar >> end_p, space_p);
140  if (!info.full)
141  {
142  errorOut("Does not conform to name=value, name=value... etc. of ddl Map element.");
143  }
144 
145  if (parent() == "Algorithm" || parent() == "SpecPar")
146  {
147  pMapMap[tName] = pMap;
148  }
149  else if (parent() == "ConstantsSection" || parent() == "DDDefinition")
150  {
151  dd_map_type tMap;
152  for (std::map<std::string, double>::const_iterator it = pMap.begin(); it != pMap.end(); ++it)
153  {
154  tMap[it->first] = it->second;
155  }
156  DDMap m ( getDDName(pNameSpace) , std::make_unique<dd_map_type>( tMap ));
157  // clear the map of maps, because in these elements we only have ONE at a time.
158  pMapMap.clear();
159  }
160 
161  std::string nEntries = atts.find("nEntries")->second;
162  if (pMap.size() !=
163  size_t(myRegistry_->evaluator().eval(pNameSpace, nEntries)))
164  {
165  errorOut("Number of entries found in Map text does not match number in attribute nEntries.");
166  }
167  clear();
168 }
169 
170 void
171 DDLMap::do_pair( char const* str, char const* end )
172 {
173  pMap[pName] = pDouble;
174 }
175 
176 void
177 DDLMap::do_makeName( char const* str, char const* end )
178 {
179  pName = std::string(str, end);
180 }
181 
182 void
183 DDLMap::do_makeDouble( char const* str, char const* end )
184 {
185  std::string ts(str, end);
187 }
188 
189 void
190 DDLMap::errorOut( const char* str )
191 {
192  std::string msg("\nDDLMap: Failed to parse the following: \n");
193  msg+= std::string(str);
194  msg+="\n as a Map element (comma separated list of name=value).";
195  throwError(msg);
196 }
197 
200 {
201  return pMapMap;
202 }
MapPair(DDLMap *iMap)
Definition: DDLMap.cc:16
Map map_
ReadMapType< std::map< std::string, double > > pMapMap
Definition: DDLMap.h:50
MapMakeName mapMakeName() const
Definition: DDLMap.cc:44
static const TGPicture * info(bool iBackgroundIsBlack)
void operator()(char const *str, char const *end) const
Definition: DDLMap.cc:96
DDLElementRegistry * myRegistry_
Definition: DDXMLElement.h:172
DDLMap(DDLElementRegistry *myreg)
Definition: DDLMap.cc:56
Definition: CLHEP.h:16
Mapper(DDLMap *iMap)
Definition: DDLMap.cc:40
virtual const DDXMLAttribute & getAttributeSet(size_t aIndex=0) const
Get a "row" of attributes, i.e. one attribute set.
Definition: DDXMLElement.cc:72
std::string pNameSpace
Definition: DDLMap.h:53
std::string pName
Definition: DDLMap.h:52
void throwError(const std::string &keyMessage) const
format std::string for throw an error.
ReadMapType< std::map< std::string, double > > & getMapOfMaps(void)
Definition: DDLMap.cc:199
const std::string & parent(void) const
access to parent element name
void processElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv) override
Processing the element.
Definition: DDLMap.cc:118
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:80
DDLMap * map_
Definition: DDLMap.cc:27
MapMakeDouble mapMakeDouble() const
Definition: DDLMap.cc:45
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)
MapPair mapPair() const
Definition: DDLMap.cc:43
MapMakeName(DDLMap *iMap)
Definition: DDLMap.cc:24
ClhepEvaluator & evaluator()
definition(Mapper const &self)
Definition: DDLMap.cc:62
void do_makeName(char const *str, char const *end)
Definition: DDLMap.cc:177
void operator()(char const *str, char const *end) const
Definition: DDLMap.cc:102
void preProcessElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv) override
Called by loadAttributes AFTER attributes are loaded.
Definition: DDLMap.cc:108
rule< ScannerT > value
Definition: DDLMap.cc:83
#define end
Definition: vmac.h:39
MapMakeDouble(DDLMap *iMap)
Definition: DDLMap.cc:32
Definition: DDLMap.cc:38
void do_pair(char const *str, char const *end)
Definition: DDLMap.cc:171
DDLMap handles Map container.
Definition: DDLMap.h:33
def parse(path, config)
Definition: dumpparser.py:13
This is a base class for processing XML elements in the DDD.
Definition: DDXMLElement.h:48
tuple msg
Definition: mps_check.py:278
DDLMap * map_
Definition: DDLMap.cc:48
double pDouble
Definition: DDLMap.h:51
void errorOut(const char *str)
Definition: DDLMap.cc:190
rule< ScannerT > const & start() const
Definition: DDLMap.cc:86
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:54
DDLMap * map_
Definition: DDLMap.cc:35
void do_makeDouble(char const *str, char const *end)
Definition: DDLMap.cc:183
#define str(s)
virtual const DDName getDDName(const std::string &defaultNS, const std::string &attname=std::string("name"), size_t aIndex=0)
Definition: DDXMLElement.cc:79
void operator()(char const *str, char const *end) const
Definition: DDLMap.cc:90
dd_map_type pMap
Definition: DDLMap.h:49
const std::string getText(size_t tindex=0) const
retrieve the text blob.
DDLMap * map_
Definition: DDLMap.cc:19