CMS 3D CMS Logo

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