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 //
12 
13 // system include files
14 #include <ctype.h>
15 #include <algorithm>
16 
19 #include <cstring>
20 
21 // user include files
24 
25 //
26 // constants, enums and typedefs
27 //
28 typedef std::vector<boost::shared_ptr<fireworks::OptionNode> > Options;
29 
30 namespace fireworks {
31  template< class T>
33  bool operator()(const T& iLHS,
34  const T& iRHS) const
35  {
36  return *iLHS < *iRHS;
37  }
38  };
39 
40  template< class T>
42  bool operator()(const T& iLHS,
43  const T& iRHS) const
44  {
45  return iLHS->description() == iRHS->description();
46  }
47  };
48 
49 
50  class OptionNode {
51 public:
53  OptionNode(const std::string& iDescription,
54  unsigned long iSubstitutionEnd,
55  const edm::TypeWithDict& iType);
56 
57  const std::string& description() const {
58  return m_description;
59  }
60  unsigned long substitutionEnd() const {
61  return m_endOfName;
62  }
63  const std::vector<boost::shared_ptr<OptionNode> >& options() const {
64  if(m_hasSubOptions && m_subOptions.empty()) {
66  std::sort(m_subOptions.begin(),m_subOptions.end(),
68  std::vector<boost::shared_ptr<OptionNode> >::iterator it=
69  std::unique(m_subOptions.begin(),m_subOptions.end(),
71  m_subOptions.erase(it, m_subOptions.end());
72 
73  m_hasSubOptions = !m_subOptions.empty();
74  }
75  return m_subOptions;
76  }
77 
78  bool operator<(const OptionNode& iRHS) const {
79  return m_description.substr(0,m_endOfName) < iRHS.m_description.substr(0,iRHS.m_endOfName);
80  }
81 
82  static void fillOptionForType( const edm::TypeWithDict&,
83  std::vector<boost::shared_ptr<OptionNode> >& );
84 private:
88  mutable std::vector<boost::shared_ptr<OptionNode> > m_subOptions;
89  mutable bool m_hasSubOptions;
90  static bool typeHasOptions(const edm::TypeWithDict& iType);
91  };
92 
93  OptionNode::OptionNode(const std::string& iDescription,
94  unsigned long iSubstitutionEnd,
95  const edm::TypeWithDict& iType) :
96  m_type(iType),
97  m_description(iDescription),
98  m_endOfName(iSubstitutionEnd),
99  m_hasSubOptions(typeHasOptions(iType) )
100  {
101  }
102 
103  namespace {
104  std::string descriptionFromMember(const edm::FunctionWithDict& iMember)
105  {
106  std::string typeString = iMember.typeName();
107  std::string::size_type index = typeString.find_first_of("(");
108  if(index == std::string::npos) {
109  return iMember.name()+":"+typeString;
110  } else {
111  return iMember.name()+typeString.substr(index,std::string::npos)+":"+
112  typeString.substr(0,index);
113  }
114  }
115  }
116 
118  m_type(reco::returnType(iMember)),
119  m_description(descriptionFromMember(iMember)),
120  m_endOfName(iMember.name().size()),
121  m_hasSubOptions(typeHasOptions(m_type))
122  {
123  }
124 
125 
127  std::vector<boost::shared_ptr<OptionNode> >& oOptions)
128  {
129  edm::TypeWithDict type = iType;
130  if(type.isPointer()) {
131  type = type.toType(); // for Pointers, I get the real type this way
132  }
133  // first look in base scope
134  edm::TypeFunctionMembers functions(type);
135  oOptions.reserve(oOptions.size()+functions.size());
136  for(auto const& function : functions) {
137  edm::FunctionWithDict m(function);
138  if(!m.isConst() ||
139  m.isConstructor() ||
140  m.isDestructor() ||
141  m.isOperator() ||
142  !m.isPublic() ||
143  m.name().substr(0,2)=="__") {continue;}
144  oOptions.push_back(boost::shared_ptr<OptionNode>(new OptionNode(m)));
145  }
146 
147  edm::TypeBases bases(type);
148  for(auto const& base : bases) {
149  fillOptionForType(edm::BaseWithDict(base).typeOf(),oOptions);
150  }
151  }
152 
154  return iType.isClass();
155  }
156 
157 }
158 
159 //
160 // static data member definitions
161 //
162 
163 //
164 // constructors and destructor
165 //
166 #define FUN1(_fun_) \
167  m_builtins.push_back(boost::shared_ptr<OptionNode>( new OptionNode( # _fun_ "(float):float", strlen( # _fun_ )+1,s_float)))
168 
169 #define FUN2(_fun_) \
170  m_builtins.push_back(boost::shared_ptr<OptionNode>( new OptionNode( # _fun_ "(float,float):float", strlen( # _fun_ )+1,s_float)))
171 
173 {
174  using fireworks::OptionNode;
175  static const edm::TypeWithDict s_float(typeid(float));
176  FUN1(abs);
177  FUN1(acos);
178  FUN1(asin);
179  FUN1(atan);
180  FUN1(cos);
181  FUN1(cosh);
182  FUN1(exp);
183  FUN1(log);
184  FUN1(log10);
185  FUN1(sin);
186  FUN1(sinh);
187  FUN1(sqrt);
188  FUN1(tan);
189  FUN1(tanh);
190  FUN2(atan2);
191  FUN2(chi2prob);
192  FUN2(pow);
193  FUN2(min);
194  FUN2(max);
195  std::sort(m_builtins.begin(),m_builtins.end(),
197 
198 }
199 
200 // FWExpressionValidator::FWExpressionValidator(const FWExpressionValidator& rhs)
201 // {
202 // // do actual copying here;
203 // }
204 
206 {
207 }
208 
209 //
210 // assignment operators
211 //
212 // const FWExpressionValidator& FWExpressionValidator::operator=(const FWExpressionValidator& rhs)
213 // {
214 // //An exception safe implementation is
215 // FWExpressionValidator temp(rhs);
216 // swap(rhs);
217 //
218 // return *this;
219 // }
220 
221 //
222 // member functions
223 //
224 void
226 {
227  using fireworks::OptionNode;
228  m_type=iType;
229  m_options.clear();
231  OptionNode::fillOptionForType(iType, m_options);
232  std::sort(m_options.begin(),m_options.end(),
234  std::vector<boost::shared_ptr<OptionNode> >::iterator it=
235  std::unique(m_options.begin(),m_options.end(),
237  m_options.erase(it, m_options.end());
238 }
239 
240 //
241 // const member functions
242 //
243 namespace {
244  void dummyDelete(void*) {
245  }
246 
247  void findTypeDelimiters(const char*& ioBegin,
248  const char* iEnd,
249  std::vector<const char*>& oDelimeters)
250  {
251  oDelimeters.clear();
252  if(ioBegin==iEnd) { return; }
253  const char* it = iEnd-1;
254  const char* itEnd = ioBegin-1;
255  for(; it != itEnd; --it) {
256  if(isalnum(*it)) { continue;}
257  bool shouldStop=false;
258  switch(*it) {
259  case '_': break;
260  case '.':
261  oDelimeters.push_back(it);
262  break;
263  default:
264  shouldStop=true;
265  }
266  if(shouldStop) { break;}
267  }
268  ioBegin = it+1;
269  std::reverse(oDelimeters.begin(),oDelimeters.end());
270  }
271 }
272 
273 void
274 FWExpressionValidator::fillOptions(const char* iBegin, const char* iEnd,
275  std::vector<std::pair<boost::shared_ptr<std::string>, std::string> >& oOptions) const
276 {
277  using fireworks::OptionNode;
278  oOptions.clear();
279  std::vector<const char*> delimeters;
280  findTypeDelimiters(iBegin, iEnd, delimeters);
281  //must find correct OptionNode
282  const Options* nodes = &m_options;
283  const char* begin = iBegin;
284  for(std::vector<const char*>::iterator it = delimeters.begin(), itEnd = delimeters.end();
285  it != itEnd; ++it) {
286  OptionNode temp(std::string(begin,*it),
287  *it-begin,
289 
290  boost::shared_ptr<OptionNode> comp(&temp, dummyDelete);
291  Options::const_iterator itFind =std::lower_bound(nodes->begin(),
292  nodes->end(),
293  comp,
295 
296  if(itFind == nodes->end() || *comp < *(*itFind) ) {
297  //no match so we have an error
298  return;
299  }
300  nodes = &((*itFind)->options());
301  begin = (*it)+1;
302  }
303 
304  //only use add items which begin with the part of the member we are trying to match
305  std::string part(begin,iEnd);
306  unsigned int part_size = part.size();
307  for(Options::const_iterator it = nodes->begin(), itEnd = nodes->end();
308  it != itEnd;
309  ++it) {
310  if(part == (*it)->description().substr(0,part_size) ) {
311  oOptions.push_back(std::make_pair(boost::shared_ptr<std::string>(const_cast<std::string*>(&((*it)->description())), dummyDelete),
312  (*it)->description().substr(part_size,(*it)->substitutionEnd()-part_size)));
313  }
314  }
315 }
316 
317 //
318 // static member functions
319 //
tuple base
Main Program
Definition: newFWLiteAna.py:91
type
Definition: HCALResponse.h:21
OptionNode(const edm::FunctionWithDict &)
void setType(const edm::TypeWithDict &)
#define FUN1(_fun_)
std::string::size_type m_endOfName
bool isConstructor() const
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
bool operator<(const OptionNode &iRHS) const
std::string name() const
std::vector< boost::shared_ptr< fireworks::OptionNode > > m_options
uint16_t size_type
TypeWithDict toType() const
#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
std::string typeName() const
const std::string & description() const
T sqrt(T t)
Definition: SSEVec.h:18
edm::TypeWithDict returnType(const edm::FunctionWithDict &func)
Definition: returnType.cc:16
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
unsigned long substitutionEnd() const
bool isClass() const
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
T min(T a, T b)
Definition: MathUtil.h:58
const std::vector< boost::shared_ptr< OptionNode > > & options() const
std::vector< boost::shared_ptr< OptionNode > > m_subOptions
static bool typeHasOptions(const edm::TypeWithDict &iType)
bool operator()(const T &iLHS, const T &iRHS) const
static void fillOptionForType(const edm::TypeWithDict &, std::vector< boost::shared_ptr< OptionNode > > &)
part
Definition: HCALResponse.h:20
#define begin
Definition: vmac.h:30
std::vector< boost::shared_ptr< fireworks::OptionNode > > Options
std::vector< boost::shared_ptr< fireworks::OptionNode > > m_builtins
bool isDestructor() const
bool operator()(const T &iLHS, const T &iRHS) const
bool isPointer() const
long double T
tuple size
Write out results.
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40