CMS 3D CMS Logo

DDLVector.cc
Go to the documentation of this file.
2 
3 #include <stddef.h>
4 #include <map>
5 #include <utility>
6 
12 #include "boost/spirit/include/classic.hpp"
13 
14 class DDCompactView;
15 
16 namespace boost { namespace spirit { namespace classic { } } } using namespace boost::spirit::classic;
17 
18 using namespace boost::spirit;
19 
21 {
22 public:
23  void operator() (char const* str, char const* end) const
24  {
25  ddlVector_->do_makeDouble(str, end);
26  }
27 
29  ddlVector_ = dynamic_cast < DDLVector* > (DDLGlobalRegistry::instance().getElement("Vector"));
30  }
31 private:
33 };
34 
36 {
37 public:
38  void operator() (char const* str, char const* end) const
39  {
40  ddlVector_->do_makeString(str, end);
41  }
42 
44  ddlVector_ = dynamic_cast < DDLVector* > (DDLGlobalRegistry::instance().getElement("Vector"));
45  }
46 private:
48 };
49 
50 bool
51 DDLVector::parse_numbers(char const* str) const
52 {
53  static VectorMakeDouble makeDouble;
54  return parse(str,
55  ((+(anychar_p - ','))[makeDouble]
56  >> *(',' >> (+(anychar_p - ','))[makeDouble]))
57  >> end_p
58  , space_p).full;
59 }
60 
61 bool
62 DDLVector::parse_strings(char const* str) const
63 {
64  static VectorMakeString makeString;
65  return parse(str,
66  ((+(anychar_p - ','))[makeString]
67  >> *(',' >> (+(anychar_p - ','))[makeString]))
68  >> end_p
69  , space_p).full;
70 }
71 
73  : DDXMLElement( myreg )
74 {}
75 
76 void
78 {
79  pVector.clear();
80  pStrVector.clear();
81  pNameSpace = nmspace;
82 }
83 
84 void
86 {
88  bool isNumVec((atts.find("type") == atts.end()
89  || atts.find("type")->second == "numeric")
90  ? true : false);
91  bool isStringVec((!isNumVec && atts.find("type") != atts.end()
92  && atts.find("type")->second == "string")
93  ? true : false);
94  std::string tTextToParse = getText();
95  // cout << "tTextToParse is |"<< tTextToParse << "|" << endl;
96  if (tTextToParse.size() == 0) {
97  errorOut(" EMPTY STRING ");
98  }
99 
100  if (isNumVec) {//(atts.find("type") == atts.end() || atts.find("type")->second == "numeric") {
101  if (!parse_numbers(tTextToParse.c_str())) {
102  errorOut(tTextToParse.c_str());
103  }
104  }
105  else if (isStringVec) { //(atts.find("type")->second == "string") {
106  if (!parse_strings(tTextToParse.c_str())) {
107  errorOut(tTextToParse.c_str());
108  }
109  }
110  else {
111  errorOut("Unexpected std::vector type. Only \"numeric\" and \"string\" are allowed.");
112  }
113 
114 
115  if (parent() == "Algorithm" || parent() == "SpecPar")
116  {
117  if (isNumVec) { //(atts.find("type") != atts.end() || atts.find("type")->second == "numeric") {
118  // std::cout << "adding to pVecMap name= " << atts.find("name")->second << std::endl;
119  // for (std::vector<double>::const_iterator it = pVector.begin(); it != pVector.end(); ++it)
120  // std::cout << *it << "\t" << std::endl;
121  pVecMap[atts.find("name")->second] = pVector;
122  // std::cout << "size: " << pVecMap.size() << std::endl;
123  }
124  else if (isStringVec) { //(atts.find("type")->second == "string") {
125  pStrVecMap[atts.find("name")->second] = pStrVector;
126  // cout << "it is a string, name is: " << atts.find("name")->second << endl;
127  }
128  size_t expNEntries = 0;
129  if (atts.find("nEntries") != atts.end()) {
130  std::string nEntries = atts.find("nEntries")->second;
131  expNEntries = size_t (myRegistry_->evaluator().eval(pNameSpace, nEntries));
132  }
133  if ( (isNumVec && pVector.size() != expNEntries)
134  || (isStringVec && pStrVector.size() != expNEntries) )
135  {
136  std::string msg ("Number of entries found in Vector text does not match number in attribute nEntries.");
137  msg += "\n\tnEntries = " + atts.find("nEntries")->second;
138  msg += "\n------------------text---------\n";
139  msg += tTextToParse;
140  msg += "\n------------------text---------\n";
141  errorOut(msg.c_str());
142  }
143  }
144  else if (parent() == "ConstantsSection" || parent() == "DDDefinition")
145  {
146  if (atts.find("type") == atts.end() || atts.find("type")->second == "numeric") {
147  DDVector v(getDDName(nmspace), new std::vector<double>(pVector));
148  }
149  else {
150  DDStrVector v(getDDName(nmspace), new std::vector<std::string>(pStrVector));
151  }
152  }
153  clear();
154 }
155 
158 {
159  return pVecMap;
160 }
161 
164 {
165  return pStrVecMap;
166 }
167 
168 void
169 DDLVector::do_makeDouble( char const* str, char const* end )
170 {
171  std::string ts(str, end);
172  double td = myRegistry_->evaluator().eval(pNameSpace, ts);
173  pVector.push_back(td);
174 }
175 
176 void
177 DDLVector::do_makeString( char const* str, char const* end )
178 {
179  std::string ts(str, end);
180  pStrVector.push_back(ts);
181 }
182 
183 void
184 DDLVector::errorOut( const char* str ) const
185 {
186  std::string e("Failed to parse the following: \n");
187  e+= std::string(str);
188  e+="\n as a Vector element (comma separated list).";
189  throwError (e);
190 }
191 
192 void
194 {
196  pVecMap.clear();
197  pStrVecMap.clear();
198 }
DDLVector * ddlVector_
Definition: DDLVector.cc:47
DDLElementRegistry * myRegistry_
Definition: DDXMLElement.h:172
Definition: CLHEP.h:16
std::vector< double > pVector
Definition: DDLVector.h:49
void preProcessElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv) override
Called by loadAttributes AFTER attributes are loaded.
Definition: DDLVector.cc:77
DDLVector handles Rotation and ReflectionRotation elements.
Definition: DDLVector.h:31
DDLVector(DDLElementRegistry *myreg)
Definition: DDLVector.cc:72
DDLVector * ddlVector_
Definition: DDLVector.cc:32
ReadMapType< std::vector< std::string > > & getMapOfStrVectors()
Definition: DDLVector.cc:163
virtual const DDXMLAttribute & getAttributeSet(size_t aIndex=0) const
Get a "row" of attributes, i.e. one attribute set.
Definition: DDXMLElement.cc:73
void throwError(const std::string &keyMessage) const
format std::string for throw an error.
const std::string & parent(void) const
access to parent element name
a named constant corresponding to the DDL-XML tag <Constant> and <ConstantsStrVector> ...
Definition: DDStrVector.h:17
type of data representation of DDCompactView
Definition: DDCompactView.h:90
void do_makeDouble(char const *str, char const *end)
Definition: DDLVector.cc:169
std::map< std::string, std::string > DDXMLAttribute
Definition: DDXMLElement.h:45
ReadMapType< std::vector< double > > & getMapOfVectors()
Definition: DDLVector.cc:157
void do_makeString(char const *str, char const *end)
Definition: DDLVector.cc:177
virtual std::vector< DDXMLAttribute >::const_iterator end(void)
void errorOut(const char *str) const
Definition: DDLVector.cc:184
std::string pNameSpace
Definition: DDLVector.h:53
static value_type & instance()
bool parse_strings(char const *str) const
Definition: DDLVector.cc:62
a std::map<std::string,YourType> that offers a const operator[key]; if key is not stored in the std::...
Definition: DDReadMapType.h:13
ClhepEvaluator & evaluator()
void clearall()
Definition: DDLVector.cc:193
#define end
Definition: vmac.h:37
a named constant corresponding to the DDL-XML tag <Constant> and <ConstantsVector> ...
Definition: DDVector.h:17
std::vector< std::string > pStrVector
Definition: DDLVector.h:50
ReadMapType< std::vector< double > > pVecMap
Definition: DDLVector.h:51
def parse(path, config)
Definition: dumpparser.py:15
bool parse_numbers(char const *str) const
Definition: DDLVector.cc:51
This is a base class for processing XML elements in the DDD.
Definition: DDXMLElement.h:48
ReadMapType< std::vector< std::string > > pStrVecMap
Definition: DDLVector.h:52
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:55
void processElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv) override
Processing the element.
Definition: DDLVector.cc:85
virtual const DDName getDDName(const std::string &defaultNS, const std::string &attname=std::string("name"), size_t aIndex=0)
Definition: DDXMLElement.cc:80
const std::string getText(size_t tindex=0) const
retrieve the text blob.