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.12 2013/02/10 22:12:04 wmtan Exp $
12 //
13 
14 // system include files
15 #include <ctype.h>
16 #include <algorithm>
17 
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:
54  OptionNode(const std::string& iDescription,
55  unsigned long iSubstitutionEnd,
56  const edm::TypeWithDict& 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 edm::TypeWithDict&,
84  std::vector<boost::shared_ptr<OptionNode> >& );
85 private:
89  mutable std::vector<boost::shared_ptr<OptionNode> > m_subOptions;
90  mutable bool m_hasSubOptions;
91  static bool typeHasOptions(const edm::TypeWithDict& iType);
92  };
93 
94  OptionNode::OptionNode(const std::string& iDescription,
95  unsigned long iSubstitutionEnd,
96  const edm::TypeWithDict& 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 edm::FunctionWithDict& iMember)
106  {
107  std::string typeString = iMember.typeName();
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 
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  edm::TypeWithDict type = iType;
131  if(type.isPointer()) {
132  type = type.toType(); // for Pointers, I get the real type this way
133  }
134  // first look in base scope
135  edm::TypeFunctionMembers functions(type);
136  oOptions.reserve(oOptions.size()+functions.size());
137  for(auto const& function : functions) {
138  edm::FunctionWithDict m(function);
139  if(!m.isConst() ||
140  m.isConstructor() ||
141  m.isDestructor() ||
142  m.isOperator() ||
143  !m.isPublic() ||
144  m.name().substr(0,2)=="__") {continue;}
145  oOptions.push_back(boost::shared_ptr<OptionNode>(new OptionNode(m)));
146  }
147 
148  edm::TypeBases bases(type);
149  for(auto const& base : bases) {
150  fillOptionForType(edm::BaseWithDict(base).typeOf(),oOptions);
151  }
152  }
153 
155  return iType.isClass();
156  }
157 
158 }
159 
160 //
161 // static data member definitions
162 //
163 
164 //
165 // constructors and destructor
166 //
167 #define FUN1(_fun_) \
168  m_builtins.push_back(boost::shared_ptr<OptionNode>( new OptionNode( # _fun_ "(float):float", strlen( # _fun_ )+1,s_float)))
169 
170 #define FUN2(_fun_) \
171  m_builtins.push_back(boost::shared_ptr<OptionNode>( new OptionNode( # _fun_ "(float,float):float", strlen( # _fun_ )+1,s_float)))
172 
174 {
175  using fireworks::OptionNode;
176  static const edm::TypeWithDict s_float(typeid(float));
177  FUN1(abs);
178  FUN1(acos);
179  FUN1(asin);
180  FUN1(atan);
181  FUN1(cos);
182  FUN1(cosh);
183  FUN1(exp);
184  FUN1(log);
185  FUN1(log10);
186  FUN1(sin);
187  FUN1(sinh);
188  FUN1(sqrt);
189  FUN1(tan);
190  FUN1(tanh);
191  FUN2(atan2);
192  FUN2(chi2prob);
193  FUN2(pow);
194  FUN2(min);
195  FUN2(max);
196  std::sort(m_builtins.begin(),m_builtins.end(),
198 
199 }
200 
201 // FWExpressionValidator::FWExpressionValidator(const FWExpressionValidator& rhs)
202 // {
203 // // do actual copying here;
204 // }
205 
207 {
208 }
209 
210 //
211 // assignment operators
212 //
213 // const FWExpressionValidator& FWExpressionValidator::operator=(const FWExpressionValidator& rhs)
214 // {
215 // //An exception safe implementation is
216 // FWExpressionValidator temp(rhs);
217 // swap(rhs);
218 //
219 // return *this;
220 // }
221 
222 //
223 // member functions
224 //
225 void
227 {
228  using fireworks::OptionNode;
229  m_type=iType;
230  m_options.clear();
232  OptionNode::fillOptionForType(iType, m_options);
233  std::sort(m_options.begin(),m_options.end(),
235  std::vector<boost::shared_ptr<OptionNode> >::iterator it=
236  std::unique(m_options.begin(),m_options.end(),
238  m_options.erase(it, m_options.end());
239 }
240 
241 //
242 // const member functions
243 //
244 namespace {
245  void dummyDelete(void*) {
246  }
247 
248  void findTypeDelimiters(const char*& ioBegin,
249  const char* iEnd,
250  std::vector<const char*>& oDelimeters)
251  {
252  oDelimeters.clear();
253  if(ioBegin==iEnd) { return; }
254  const char* it = iEnd-1;
255  const char* itEnd = ioBegin-1;
256  for(; it != itEnd; --it) {
257  if(isalnum(*it)) { continue;}
258  bool shouldStop=false;
259  switch(*it) {
260  case '_': break;
261  case '.':
262  oDelimeters.push_back(it);
263  break;
264  default:
265  shouldStop=true;
266  }
267  if(shouldStop) { break;}
268  }
269  ioBegin = it+1;
270  std::reverse(oDelimeters.begin(),oDelimeters.end());
271  }
272 }
273 
274 void
275 FWExpressionValidator::fillOptions(const char* iBegin, const char* iEnd,
276  std::vector<std::pair<boost::shared_ptr<std::string>, std::string> >& oOptions) const
277 {
278  using fireworks::OptionNode;
279  oOptions.clear();
280  std::vector<const char*> delimeters;
281  findTypeDelimiters(iBegin, iEnd, delimeters);
282  //must find correct OptionNode
283  const Options* nodes = &m_options;
284  const char* begin = iBegin;
285  for(std::vector<const char*>::iterator it = delimeters.begin(), itEnd = delimeters.end();
286  it != itEnd; ++it) {
287  OptionNode temp(std::string(begin,*it),
288  *it-begin,
290 
291  boost::shared_ptr<OptionNode> comp(&temp, dummyDelete);
292  Options::const_iterator itFind =std::lower_bound(nodes->begin(),
293  nodes->end(),
294  comp,
296 
297  if(itFind == nodes->end() || *comp < *(*itFind) ) {
298  //no match so we have an error
299  return;
300  }
301  nodes = &((*itFind)->options());
302  begin = (*it)+1;
303  }
304 
305  //only use add items which begin with the part of the member we are trying to match
306  std::string part(begin,iEnd);
307  unsigned int part_size = part.size();
308  for(Options::const_iterator it = nodes->begin(), itEnd = nodes->end();
309  it != itEnd;
310  ++it) {
311  if(part == (*it)->description().substr(0,part_size) ) {
312  oOptions.push_back(std::make_pair(boost::shared_ptr<std::string>(const_cast<std::string*>(&((*it)->description())), dummyDelete),
313  (*it)->description().substr(part_size,(*it)->substitutionEnd()-part_size)));
314  }
315  }
316 }
317 
318 //
319 // static member functions
320 //
tuple base
Main Program
Definition: newFWLiteAna.py:92
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
#define abs(x)
Definition: mlp_lapack.h:159
#define min(a, b)
Definition: mlp_lapack.h:161
std::string name() const
edm::TypeWithDict returnType(const edm::FunctionWithDict &mem)
Definition: returnType.cc:8
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 T & max(const T &a, const T &b)
const std::string & description() const
T sqrt(T t)
Definition: SSEVec.h:48
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
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:31
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