CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MethodInvoker.h
Go to the documentation of this file.
1 #ifndef CommonTools_Utils_MethodInvoker_h
2 #define CommonTools_Utils_MethodInvoker_h
3 
10 
11 #include <map>
12 #include <vector>
13 
14 #include "tbb/concurrent_unordered_map.h"
15 
16 namespace edm {
17  struct TypeIDHasher {
18  size_t operator()(TypeID const& tid) const { return std::hash<std::string>{}(std::string(tid.name())); }
19  };
20 } // namespace edm
21 
22 namespace reco {
23  namespace parser {
24 
25  class MethodInvoker {
26  private: // Private Data Members
29  std::vector<AnyMethodArgument> ints_; // already fixed to the correct type
30  std::vector<void*> args_;
31 
34 
35  private: // Private Function Members
36  void setArgs();
37 
38  public: // Public Function Members
40  const std::vector<AnyMethodArgument>& ints = std::vector<AnyMethodArgument>());
41  explicit MethodInvoker(const edm::MemberWithDict&);
44 
45  edm::FunctionWithDict const method() const { return method_; }
46  edm::MemberWithDict const member() const { return member_; }
47  bool isFunction() const { return isFunction_; }
48  std::string methodName() const;
50 
57  };
58 
63  struct SingleInvoker {
64  private: // Private Data Members
66  std::vector<MethodInvoker> invokers_;
70  bool isRefGet_;
71 
72  public:
73  SingleInvoker(const SingleInvoker&) = delete;
74  SingleInvoker& operator=(const SingleInvoker&) = delete;
75 
76  SingleInvoker(const edm::TypeWithDict&, const std::string& name, const std::vector<AnyMethodArgument>& args);
78 
86  std::pair<edm::ObjectWithDict, bool> invoke(const edm::ObjectWithDict& o,
87  std::vector<edm::ObjectWithDict>& v) const;
88 
90  double retToDouble(const edm::ObjectWithDict&) const;
91 
92  void throwFailedConversion(const edm::ObjectWithDict&) const;
93  };
94 
96  struct LazyInvoker {
97  typedef std::shared_ptr<SingleInvoker> SingleInvokerPtr;
98  typedef tbb::concurrent_unordered_map<edm::TypeID, SingleInvokerPtr, edm::TypeIDHasher> InvokerMap;
99 
100  private: // Private Data Members
102  std::vector<AnyMethodArgument> argsBeforeFixups_;
103  // the shared ptr is only to make the code exception safe
104  // otherwise I think it could leak if the constructor of
105  // SingleInvoker throws an exception (which can happen)
107 
108  private: // Private Function Members
109  const SingleInvoker& invoker(const edm::TypeWithDict&) const;
110 
111  public: // Public Function Members
112  explicit LazyInvoker(const std::string& name, const std::vector<AnyMethodArgument>& args);
113  ~LazyInvoker();
114 
121  edm::ObjectWithDict invoke(const edm::ObjectWithDict& o, std::vector<edm::ObjectWithDict>& v) const;
122 
124  double invokeLast(const edm::ObjectWithDict& o, std::vector<edm::ObjectWithDict>& v) const;
125  };
126 
127  } // namespace parser
128 } // namespace reco
129 
130 #endif // CommonTools_Utils_MethodInvoker_h
edm::ObjectWithDict invoke(const edm::ObjectWithDict &obj, edm::ObjectWithDict &retstore) const
SingleInvoker(const SingleInvoker &)=delete
method::TypeCode retType_
Definition: MethodInvoker.h:65
bool isRefGet_
true if this invoker just pops out a ref and returns (ref.get(), false)
Definition: MethodInvoker.h:70
tbb::concurrent_unordered_map< edm::TypeID, SingleInvokerPtr, edm::TypeIDHasher > InvokerMap
Definition: MethodInvoker.h:98
std::pair< edm::ObjectWithDict, bool > invoke(const edm::ObjectWithDict &o, std::vector< edm::ObjectWithDict > &v) const
MethodInvoker(const edm::FunctionWithDict &method, const std::vector< AnyMethodArgument > &ints=std::vector< AnyMethodArgument >())
std::string returnTypeName() const
double invokeLast(const edm::ObjectWithDict &o, std::vector< edm::ObjectWithDict > &v) const
invoke and coerce result to double
edm::MemberWithDict member_
Definition: MethodInvoker.h:28
std::vector< AnyMethodArgument > ints_
Definition: MethodInvoker.h:29
size_t operator()(TypeID const &tid) const
Definition: MethodInvoker.h:18
Keeps different SingleInvokers for each dynamic type of the objects passed to invoke() ...
Definition: MethodInvoker.h:96
std::shared_ptr< SingleInvoker > SingleInvokerPtr
Definition: MethodInvoker.h:97
edm::MemberWithDict const member() const
Definition: MethodInvoker.h:46
edm::FunctionWithDict method_
Definition: MethodInvoker.h:27
edm::ObjectWithDict storage_
Definition: MethodInvoker.h:67
edm::TypeWithDict retTypeFinal_
Definition: MethodInvoker.h:33
edm::ObjectWithDict invoke(const edm::ObjectWithDict &o, std::vector< edm::ObjectWithDict > &v) const
MethodInvoker & operator=(const MethodInvoker &)
void throwFailedConversion(const edm::ObjectWithDict &) const
edm::FunctionWithDict const method() const
Definition: MethodInvoker.h:45
const SingleInvoker & invoker(const edm::TypeWithDict &) const
std::string methodName() const
std::vector< void * > args_
Definition: MethodInvoker.h:30
std::vector< AnyMethodArgument > argsBeforeFixups_
double retToDouble(const edm::ObjectWithDict &) const
convert the output of invoke to a double, if possible
std::vector< MethodInvoker > invokers_
Definition: MethodInvoker.h:66
const char * name() const
Definition: TypeIDBase.h:44
LazyInvoker(const std::string &name, const std::vector< AnyMethodArgument > &args)
SingleInvoker & operator=(const SingleInvoker &)=delete