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  void operator() (char const* str, char const* end) const
25  {
26  ddlVector_->do_makeDouble(str, end);
27  }
28 
30  ddlVector_ = dynamic_cast < DDLVector* > (DDLGlobalRegistry::instance().getElement("Vector"));
31  }
32 
34 };
35 
37 {
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 
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 
77 {}
78 
79 void
81 {
82  pVector.clear();
83  pStrVector.clear();
84  pNameSpace = nmspace;
85 }
86 
87 void
89 {
90  DCOUT_V('P', "DDLVector::processElement started");
91 
93  bool isNumVec((atts.find("type") == atts.end()
94  || atts.find("type")->second == "numeric")
95  ? true : false);
96  bool isStringVec((!isNumVec && atts.find("type") != atts.end()
97  && atts.find("type")->second == "string")
98  ? true : false);
99  std::string tTextToParse = getText();
100  // cout << "tTextToParse is |"<< tTextToParse << "|" << endl;
101  if (tTextToParse.size() == 0) {
102  errorOut(" EMPTY STRING ");
103  }
104 
105  if (isNumVec) {//(atts.find("type") == atts.end() || atts.find("type")->second == "numeric") {
106  if (!parse_numbers(tTextToParse.c_str())) {
107  errorOut(tTextToParse.c_str());
108  }
109  }
110  else if (isStringVec) { //(atts.find("type")->second == "string") {
111  if (!parse_strings(tTextToParse.c_str())) {
112  errorOut(tTextToParse.c_str());
113  }
114  }
115  else {
116  errorOut("Unexpected std::vector type. Only \"numeric\" and \"string\" are allowed.");
117  }
118 
119 
120  if (parent() == "Algorithm" || parent() == "SpecPar")
121  {
122  if (isNumVec) { //(atts.find("type") != atts.end() || atts.find("type")->second == "numeric") {
123  // std::cout << "adding to pVecMap name= " << atts.find("name")->second << std::endl;
124  // for (std::vector<double>::const_iterator it = pVector.begin(); it != pVector.end(); ++it)
125  // std::cout << *it << "\t" << std::endl;
126  pVecMap[atts.find("name")->second] = pVector;
127  // std::cout << "size: " << pVecMap.size() << std::endl;
128  }
129  else if (isStringVec) { //(atts.find("type")->second == "string") {
130  pStrVecMap[atts.find("name")->second] = pStrVector;
131  // cout << "it is a string, name is: " << atts.find("name")->second << endl;
132  }
133  size_t expNEntries = 0;
134  if (atts.find("nEntries") != atts.end()) {
135  std::string nEntries = atts.find("nEntries")->second;
136  expNEntries = size_t (ExprEvalSingleton::instance().eval(pNameSpace, nEntries));
137  }
138  if ( (isNumVec && pVector.size() != expNEntries)
139  || (isStringVec && pStrVector.size() != expNEntries) )
140  {
141  std::string msg ("Number of entries found in Vector text does not match number in attribute nEntries.");
142  msg += "\n\tnEntries = " + atts.find("nEntries")->second;
143  msg += "\n------------------text---------\n";
144  msg += tTextToParse;
145  msg += "\n------------------text---------\n";
146  errorOut(msg.c_str());
147  }
148  }
149  else if (parent() == "ConstantsSection" || parent() == "DDDefinition")
150  {
151  if (atts.find("type") == atts.end() || atts.find("type")->second == "numeric") {
152  DDVector v(getDDName(nmspace), new std::vector<double>(pVector));
153  }
154  else {
155  DDStrVector v(getDDName(nmspace), new std::vector<std::string>(pStrVector));
156  }
157  }
158  clear();
159  DCOUT_V('P', "DDLVector::processElement completed");
160 }
161 
164 {
165  return pVecMap;
166 }
167 
170 {
171  return pStrVecMap;
172 }
173 
174 void
175 DDLVector::do_makeDouble( char const* str, char const* end )
176 {
177  std::string ts(str, end);
178  double td = ExprEvalSingleton::instance().eval(pNameSpace, ts);
179  pVector.push_back(td);
180 }
181 
182 void
183 DDLVector::do_makeString( char const* str, char const* end )
184 {
185  std::string ts(str, end);
186  pStrVector.push_back(ts);
187 }
188 
189 void
190 DDLVector::errorOut( const char* str ) const
191 {
192  std::string e("Failed to parse the following: \n");
193  e+= std::string(str);
194  e+="\n as a Vector element (comma separated list).";
195  throwError (e);
196 }
197 
198 void
200 {
202  pVecMap.clear();
203  pStrVecMap.clear();
204 }
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:72
ReadMapType< std::vector< std::string > > & getMapOfStrVectors()
Definition: DDLVector.cc:169
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:17
void processElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv)
Processing the element.
Definition: DDLVector.cc:88
type of data representation of DDCompactView
Definition: DDCompactView.h:77
void do_makeDouble(char const *str, char const *end)
Definition: DDLVector.cc:175
std::map< std::string, std::string > DDXMLAttribute
Definition: DDXMLElement.h:55
ReadMapType< std::vector< double > > & getMapOfVectors()
Definition: DDLVector.cc:163
void do_makeString(char const *str, char const *end)
Definition: DDLVector.cc:183
void errorOut(const char *str) const
Definition: DDLVector.cc:190
DDLVector * ddlVector_
Definition: DDLVector.cc:33
std::string pNameSpace
Definition: DDLVector.h:58
static value_type & instance()
bool parse_strings(char const *str) const
Definition: DDLVector.cc:62
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
void preProcessElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv)
Called by loadAttributes AFTER attributes are loaded.
Definition: DDLVector.cc:80
void clearall()
Definition: DDLVector.cc:199
#define end
Definition: vmac.h:38
a named constant corresponding to the DDL-XML tag &lt;Constant&gt; and &lt;ConstantsVector&gt; ...
Definition: DDVector.h:17
DDLVector * ddlVector_
Definition: DDLVector.cc:47
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:51
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
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:76
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.