CMS 3D CMS Logo

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 <boost/utility.hpp>
12 
13 #include <map>
14 #include <vector>
15 
16 #include "tbb/concurrent_unordered_map.h"
17 
18 namespace edm {
19  struct TypeIDHasher {
20  size_t operator()(TypeID const& tid) const {
21  tbb::tbb_hash<std::string> hasher;
22  return hasher(std::string(tid.name()));
23  }
24  };
25 } // namespace edm
26 
27 namespace reco {
28  namespace parser {
29 
30  class MethodInvoker {
31  private: // Private Data Members
34  std::vector<AnyMethodArgument> ints_; // already fixed to the correct type
35  std::vector<void*> args_;
36 
39 
40  private: // Private Function Members
41  void setArgs();
42 
43  public: // Public Function Members
45  const std::vector<AnyMethodArgument>& ints = std::vector<AnyMethodArgument>());
46  explicit MethodInvoker(const edm::MemberWithDict&);
48  MethodInvoker& operator=(const MethodInvoker&);
49 
50  edm::FunctionWithDict const method() const { return method_; }
51  edm::MemberWithDict const member() const { return member_; }
52  bool isFunction() const { return isFunction_; }
53  std::string methodName() const;
54  std::string returnTypeName() const;
55 
61  edm::ObjectWithDict invoke(const edm::ObjectWithDict& obj, edm::ObjectWithDict& retstore) const;
62  };
63 
68  struct SingleInvoker : boost::noncopyable {
69  private: // Private Data Members
71  std::vector<MethodInvoker> invokers_;
75  bool isRefGet_;
76 
77  public:
78  SingleInvoker(const edm::TypeWithDict&, const std::string& name, const std::vector<AnyMethodArgument>& args);
79  ~SingleInvoker();
80 
88  std::pair<edm::ObjectWithDict, bool> invoke(const edm::ObjectWithDict& o,
89  std::vector<edm::ObjectWithDict>& v) const;
90 
92  double retToDouble(const edm::ObjectWithDict&) const;
93 
94  void throwFailedConversion(const edm::ObjectWithDict&) const;
95  };
96 
98  struct LazyInvoker {
99  typedef std::shared_ptr<SingleInvoker> SingleInvokerPtr;
100  typedef tbb::concurrent_unordered_map<edm::TypeID, SingleInvokerPtr, edm::TypeIDHasher> InvokerMap;
101 
102  private: // Private Data Members
104  std::vector<AnyMethodArgument> argsBeforeFixups_;
105  // the shared ptr is only to make the code exception safe
106  // otherwise I think it could leak if the constructor of
107  // SingleInvoker throws an exception (which can happen)
108  mutable InvokerMap invokers_;
109 
110  private: // Private Function Members
111  const SingleInvoker& invoker(const edm::TypeWithDict&) const;
112 
113  public: // Public Function Members
114  explicit LazyInvoker(const std::string& name, const std::vector<AnyMethodArgument>& args);
115  ~LazyInvoker();
116 
123  edm::ObjectWithDict invoke(const edm::ObjectWithDict& o, std::vector<edm::ObjectWithDict>& v) const;
124 
126  double invokeLast(const edm::ObjectWithDict& o, std::vector<edm::ObjectWithDict>& v) const;
127  };
128 
129  } // namespace parser
130 } // namespace reco
131 
132 #endif // CommonTools_Utils_MethodInvoker_h
method::TypeCode retType_
Definition: MethodInvoker.h:70
bool isRefGet_
true if this invoker just pops out a ref and returns (ref.get(), false)
Definition: MethodInvoker.h:75
tbb::concurrent_unordered_map< edm::TypeID, SingleInvokerPtr, edm::TypeIDHasher > InvokerMap
edm::MemberWithDict member_
Definition: MethodInvoker.h:33
std::vector< AnyMethodArgument > ints_
Definition: MethodInvoker.h:34
size_t operator()(TypeID const &tid) const
Definition: MethodInvoker.h:20
Keeps different SingleInvokers for each dynamic type of the objects passed to invoke() ...
Definition: MethodInvoker.h:98
std::shared_ptr< SingleInvoker > SingleInvokerPtr
Definition: MethodInvoker.h:99
edm::MemberWithDict const member() const
Definition: MethodInvoker.h:51
edm::FunctionWithDict method_
Definition: MethodInvoker.h:32
edm::ObjectWithDict storage_
Definition: MethodInvoker.h:72
edm::TypeWithDict retTypeFinal_
Definition: MethodInvoker.h:38
edm::FunctionWithDict const method() const
Definition: MethodInvoker.h:50
fixed size matrix
std::vector< void * > args_
Definition: MethodInvoker.h:35
HLT enums.
std::vector< AnyMethodArgument > argsBeforeFixups_
std::vector< MethodInvoker > invokers_
Definition: MethodInvoker.h:71
const char * name() const
Definition: TypeIDBase.h:44