CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWExpressionValidator.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FWExpressionValidator
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Fri Aug 22 20:42:51 EDT 2008
11 // $Id: FWExpressionValidator.cc,v 1.6 2009/05/05 08:39:25 elmer Exp $
12 //
13 
14 // system include files
15 #include <ctype.h>
16 #include <algorithm>
17 
18 #include "Reflex/Member.h"
19 #include "Reflex/Base.h"
20 #include <cstring>
21 
22 // user include files
25 
26 //
27 // constants, enums and typedefs
28 //
29 typedef std::vector<boost::shared_ptr<fireworks::OptionNode> > Options;
30 
31 namespace fireworks {
32  template< class T>
34  bool operator()(const T& iLHS,
35  const T& iRHS) const
36  {
37  return *iLHS < *iRHS;
38  }
39  };
40 
41  template< class T>
43  bool operator()(const T& iLHS,
44  const T& iRHS) const
45  {
46  return iLHS->description() == iRHS->description();
47  }
48  };
49 
50 
51  class OptionNode {
52 public:
53  OptionNode(const ROOT::Reflex::Member& );
54  OptionNode(const std::string& iDescription,
55  unsigned long iSubstitutionEnd,
56  const ROOT::Reflex::Type& iType);
57 
58  const std::string& description() const {
59  return m_description;
60  }
61  unsigned long substitutionEnd() const {
62  return m_endOfName;
63  }
64  const std::vector<boost::shared_ptr<OptionNode> >& options() const {
65  if(m_hasSubOptions && m_subOptions.empty()) {
67  std::sort(m_subOptions.begin(),m_subOptions.end(),
69  std::vector<boost::shared_ptr<OptionNode> >::iterator it=
70  std::unique(m_subOptions.begin(),m_subOptions.end(),
72  m_subOptions.erase(it, m_subOptions.end());
73 
74  m_hasSubOptions = !m_subOptions.empty();
75  }
76  return m_subOptions;
77  }
78 
79  bool operator<(const OptionNode& iRHS) const {
80  return m_description.substr(0,m_endOfName) < iRHS.m_description.substr(0,iRHS.m_endOfName);
81  }
82 
83  static void fillOptionForType( const ROOT::Reflex::Type&,
84  std::vector<boost::shared_ptr<OptionNode> >& );
85 private:
87  mutable std::string m_description;
89  mutable std::vector<boost::shared_ptr<OptionNode> > m_subOptions;
90  mutable bool m_hasSubOptions;
91  static bool typeHasOptions(const ROOT::Reflex::Type& iType);
92  };
93 
94  OptionNode::OptionNode(const std::string& iDescription,
95  unsigned long iSubstitutionEnd,
96  const ROOT::Reflex::Type& iType) :
97  m_type(iType),
98  m_description(iDescription),
99  m_endOfName(iSubstitutionEnd),
100  m_hasSubOptions(typeHasOptions(iType) )
101  {
102  }
103 
104  namespace {
105  std::string descriptionFromMember(const ROOT::Reflex::Member& iMember)
106  {
107  std::string typeString = iMember.TypeOf().Name();
108  std::string::size_type index = typeString.find_first_of("(");
109  if(index == std::string::npos) {
110  return iMember.Name()+":"+typeString;
111  } else {
112  return iMember.Name()+typeString.substr(index,std::string::npos)+":"+
113  typeString.substr(0,index);
114  }
115  }
116  }
117 
118  OptionNode::OptionNode(const ROOT::Reflex::Member& iMember) :
119  m_type(reco::returnType(iMember)),
120  m_description(descriptionFromMember(iMember)),
121  m_endOfName(iMember.Name().size()),
122  m_hasSubOptions(typeHasOptions(m_type))
123  {
124  }
125 
126 
128  std::vector<boost::shared_ptr<OptionNode> >& oOptions)
129  {
130  ROOT::Reflex::Type type = iType;
131  if(type.IsPointer()) {
132  type = type.ToType();
133  }
134  // first look in base scope
135  oOptions.reserve(oOptions.size()+type.FunctionMemberSize());
136  for(ROOT::Reflex::Member_Iterator m = type.FunctionMember_Begin(); m != type.FunctionMember_End(); ++m ) {
137  if(!m->TypeOf().IsConst() ||
138  m->IsConstructor() ||
139  m->IsDestructor() ||
140  m->IsOperator() ||
141  !m->IsPublic() ||
142  m->Name().substr(0,2)=="__") {continue;}
143  oOptions.push_back(boost::shared_ptr<OptionNode>(new OptionNode(*m)));
144  }
145 
146  for(ROOT::Reflex::Base_Iterator b = type.Base_Begin(); b != type.Base_End(); ++b) {
147  fillOptionForType(b->ToType(),oOptions);
148  }
149  }
150 
152  return iType.IsClass();
153  }
154 
155 }
156 
157 //
158 // static data member definitions
159 //
160 
161 //
162 // constructors and destructor
163 //
164 #define FUN1(_fun_) \
165  m_builtins.push_back(boost::shared_ptr<OptionNode>( new OptionNode( # _fun_ "(float):float", strlen( # _fun_ )+1,s_float)))
166 
167 #define FUN2(_fun_) \
168  m_builtins.push_back(boost::shared_ptr<OptionNode>( new OptionNode( # _fun_ "(float,float):float", strlen( # _fun_ )+1,s_float)))
169 
171 {
172  using fireworks::OptionNode;
173  static const ROOT::Reflex::Type s_float(ROOT::Reflex::Type::ByTypeInfo(typeid(float)));
174  FUN1(abs);
175  FUN1(acos);
176  FUN1(asin);
177  FUN1(atan);
178  FUN1(cos);
179  FUN1(cosh);
180  FUN1(exp);
181  FUN1(log);
182  FUN1(log10);
183  FUN1(sin);
184  FUN1(sinh);
185  FUN1(sqrt);
186  FUN1(tan);
187  FUN1(tanh);
188  FUN2(atan2);
189  FUN2(chi2prob);
190  FUN2(pow);
191  FUN2(min);
192  FUN2(max);
193  std::sort(m_builtins.begin(),m_builtins.end(),
195 
196 }
197 
198 // FWExpressionValidator::FWExpressionValidator(const FWExpressionValidator& rhs)
199 // {
200 // // do actual copying here;
201 // }
202 
204 {
205 }
206 
207 //
208 // assignment operators
209 //
210 // const FWExpressionValidator& FWExpressionValidator::operator=(const FWExpressionValidator& rhs)
211 // {
212 // //An exception safe implementation is
213 // FWExpressionValidator temp(rhs);
214 // swap(rhs);
215 //
216 // return *this;
217 // }
218 
219 //
220 // member functions
221 //
222 void
224 {
225  using fireworks::OptionNode;
226  m_type=iType;
227  m_options.clear();
229  OptionNode::fillOptionForType(iType, m_options);
230  std::sort(m_options.begin(),m_options.end(),
232  std::vector<boost::shared_ptr<OptionNode> >::iterator it=
233  std::unique(m_options.begin(),m_options.end(),
235  m_options.erase(it, m_options.end());
236 }
237 
238 //
239 // const member functions
240 //
241 namespace {
242  void dummyDelete(void*) {
243  }
244 
245  void findTypeDelimiters(const char*& ioBegin,
246  const char* iEnd,
247  std::vector<const char*>& oDelimeters)
248  {
249  oDelimeters.clear();
250  if(ioBegin==iEnd) { return; }
251  const char* it = iEnd-1;
252  const char* itEnd = ioBegin-1;
253  for(; it != itEnd; --it) {
254  if(isalnum(*it)) { continue;}
255  bool shouldStop=false;
256  switch(*it) {
257  case '_': break;
258  case '.':
259  oDelimeters.push_back(it);
260  break;
261  default:
262  shouldStop=true;
263  }
264  if(shouldStop) { break;}
265  }
266  ioBegin = it+1;
267  std::reverse(oDelimeters.begin(),oDelimeters.end());
268  }
269 }
270 
271 void
272 FWExpressionValidator::fillOptions(const char* iBegin, const char* iEnd,
273  std::vector<std::pair<boost::shared_ptr<std::string>, std::string> >& oOptions) const
274 {
275  using fireworks::OptionNode;
276  oOptions.clear();
277  std::vector<const char*> delimeters;
278  findTypeDelimiters(iBegin, iEnd, delimeters);
279  //must find correct OptionNode
280  const Options* nodes = &m_options;
281  const char* begin = iBegin;
282  for(std::vector<const char*>::iterator it = delimeters.begin(), itEnd = delimeters.end();
283  it != itEnd; ++it) {
284  OptionNode temp(std::string(begin,*it),
285  *it-begin,
287 
288  boost::shared_ptr<OptionNode> comp(&temp, dummyDelete);
289  Options::const_iterator itFind =std::lower_bound(nodes->begin(),
290  nodes->end(),
291  comp,
293 
294  if(itFind == nodes->end() || *comp < *(*itFind) ) {
295  //no match so we have an error
296  return;
297  }
298  nodes = &((*itFind)->options());
299  begin = (*it)+1;
300  }
301 
302  //only use add items which begin with the part of the member we are trying to match
303  std::string part(begin,iEnd);
304  unsigned int part_size = part.size();
305  for(Options::const_iterator it = nodes->begin(), itEnd = nodes->end();
306  it != itEnd;
307  ++it) {
308  if(part == (*it)->description().substr(0,part_size) ) {
309  oOptions.push_back(std::make_pair(boost::shared_ptr<std::string>(const_cast<std::string*>(&((*it)->description())), dummyDelete),
310  (*it)->description().substr(part_size,(*it)->substitutionEnd()-part_size)));
311  }
312  }
313 }
314 
315 //
316 // static member functions
317 //
type
Definition: HCALResponse.h:22
#define FUN1(_fun_)
std::string::size_type m_endOfName
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
bool operator<(const OptionNode &iRHS) const
#define abs(x)
Definition: mlp_lapack.h:159
#define min(a, b)
Definition: mlp_lapack.h:161
Exp< T >::type exp(const T &t)
Definition: Exp.h:22
std::vector< boost::shared_ptr< fireworks::OptionNode > > m_options
uint16_t size_type
Type returnType(const Member &mem)
Definition: returnType.cc:9
static bool typeHasOptions(const ROOT::Reflex::Type &iType)
#define FUN2(_fun_)
virtual void fillOptions(const char *iBegin, const char *iEnd, std::vector< std::pair< boost::shared_ptr< std::string >, std::string > > &oOptions) const
const T & max(const T &a, const T &b)
const std::string & description() const
T sqrt(T t)
Definition: SSEVec.h:28
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
unsigned long substitutionEnd() const
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
const std::vector< boost::shared_ptr< OptionNode > > & options() const
std::vector< boost::shared_ptr< OptionNode > > m_subOptions
bool operator()(const T &iLHS, const T &iRHS) const
Log< T >::type log(const T &t)
Definition: Log.h:22
part
Definition: HCALResponse.h:21
OptionNode(const ROOT::Reflex::Member &)
double b
Definition: hdecay.h:120
#define begin
Definition: vmac.h:31
std::vector< boost::shared_ptr< fireworks::OptionNode > > Options
static void fillOptionForType(const ROOT::Reflex::Type &, std::vector< boost::shared_ptr< OptionNode > > &)
std::vector< boost::shared_ptr< fireworks::OptionNode > > m_builtins
bool operator()(const T &iLHS, const T &iRHS) const
long double T
ROOT::Reflex::Type m_type
void setType(const ROOT::Reflex::Type &)
tuple size
Write out results.
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40