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 <map>
12 #include <vector>
13 
14 #include "oneapi/tbb/concurrent_unordered_map.h"
15 #include "oneapi/tbb/concurrent_queue.h"
16 
17 namespace edm {
18  struct TypeIDHasher {
19  size_t operator()(TypeID const& tid) const { return std::hash<std::string>{}(std::string(tid.name())); }
20  };
21 } // namespace edm
22 
23 namespace reco {
24  namespace parser {
25 
26  class MethodInvoker {
27  private: // Private Data Members
30  std::vector<AnyMethodArgument> ints_; // already fixed to the correct type
31  std::vector<void*> args_;
32 
35 
36  private: // Private Function Members
37  void setArgs();
38 
39  public: // Public Function Members
41  const std::vector<AnyMethodArgument>& ints = std::vector<AnyMethodArgument>());
42  explicit MethodInvoker(const edm::MemberWithDict&);
45 
46  edm::FunctionWithDict const method() const { return method_; }
47  edm::MemberWithDict const member() const { return member_; }
48  bool isFunction() const { return isFunction_; }
49  std::string methodName() const;
51 
58  };
59 
60  struct SingleInvoker;
61 
62  //Handles temporary object storage
63  struct StorageManager {
64  private:
68 
69  public:
70  StorageManager(edm::ObjectWithDict const& iObj, SingleInvoker const* iInvoker, bool iNeedsDestructor_)
71  : object_(iObj), invoker_(iInvoker), needsDestructor_(iNeedsDestructor_) {}
72 
73  StorageManager(const StorageManager&) = delete;
76  iOther.needsDestructor_ = false;
77  iOther.invoker_ = nullptr;
78  }
79  StorageManager& operator=(const StorageManager&) = delete;
81 
83  };
88  struct SingleInvoker {
89  private: // Private Data Members
91  std::vector<MethodInvoker> invokers_;
92  mutable oneapi::tbb::concurrent_queue<edm::ObjectWithDict> storage_;
95  bool isRefGet_;
96 
98 
99  edm::ObjectWithDict createStorage(bool& needsDestructor) const;
100 
101  public:
102  SingleInvoker(const SingleInvoker&) = delete;
103  SingleInvoker& operator=(const SingleInvoker&) = delete;
104 
105  SingleInvoker(const edm::TypeWithDict&, const std::string& name, const std::vector<AnyMethodArgument>& args);
106  ~SingleInvoker();
107 
115  std::pair<edm::ObjectWithDict, bool> invoke(const edm::ObjectWithDict& o, std::vector<StorageManager>& v) const;
116 
118  double retToDouble(const edm::ObjectWithDict&) const;
119 
120  void throwFailedConversion(const edm::ObjectWithDict&) const;
121 
122  void returnStorage(edm::ObjectWithDict&&) const;
123  };
124 
126  struct LazyInvoker {
127  typedef std::shared_ptr<SingleInvoker> SingleInvokerPtr;
128  typedef tbb::concurrent_unordered_map<edm::TypeID, SingleInvokerPtr, edm::TypeIDHasher> InvokerMap;
129 
130  private: // Private Data Members
132  std::vector<AnyMethodArgument> argsBeforeFixups_;
133  // the shared ptr is only to make the code exception safe
134  // otherwise I think it could leak if the constructor of
135  // SingleInvoker throws an exception (which can happen)
137 
138  private: // Private Function Members
139  const SingleInvoker& invoker(const edm::TypeWithDict&) const;
140 
141  public: // Public Function Members
142  explicit LazyInvoker(const std::string& name, const std::vector<AnyMethodArgument>& args);
143  ~LazyInvoker();
144 
151  edm::ObjectWithDict invoke(const edm::ObjectWithDict& o, std::vector<StorageManager>& v) const;
152 
154  double invokeLast(const edm::ObjectWithDict& o, std::vector<StorageManager>& v) const;
155  };
156 
157  } // namespace parser
158 } // namespace reco
159 
160 #endif // CommonTools_Utils_MethodInvoker_h
const SingleInvoker & invoker(const edm::TypeWithDict &) const
const char * name() const
Definition: TypeIDBase.h:44
SingleInvoker(const SingleInvoker &)=delete
method::TypeCode retType_
Definition: MethodInvoker.h:90
edm::ObjectWithDict invoke(const edm::ObjectWithDict &obj, edm::ObjectWithDict &retstore) const
StorageManager(StorageManager &&iOther)
Definition: MethodInvoker.h:74
bool isRefGet_
true if this invoker just pops out a ref and returns (ref.get(), false)
Definition: MethodInvoker.h:95
tbb::concurrent_unordered_map< edm::TypeID, SingleInvokerPtr, edm::TypeIDHasher > InvokerMap
double retToDouble(const edm::ObjectWithDict &) const
convert the output of invoke to a double, if possible
std::string methodName() const
MethodInvoker(const edm::FunctionWithDict &method, const std::vector< AnyMethodArgument > &ints=std::vector< AnyMethodArgument >())
edm::MemberWithDict member_
Definition: MethodInvoker.h:29
edm::ObjectWithDict createStorage(bool &needsDestructor) const
void returnStorage(edm::ObjectWithDict &&) const
edm::ObjectWithDict object_
Definition: MethodInvoker.h:65
std::vector< AnyMethodArgument > ints_
Definition: MethodInvoker.h:30
Keeps different SingleInvokers for each dynamic type of the objects passed to invoke() ...
std::shared_ptr< SingleInvoker > SingleInvokerPtr
edm::FunctionWithDict const method() const
Definition: MethodInvoker.h:46
size_t operator()(TypeID const &tid) const
Definition: MethodInvoker.h:19
edm::FunctionWithDict method_
Definition: MethodInvoker.h:28
oneapi::tbb::concurrent_queue< edm::ObjectWithDict > storage_
Definition: MethodInvoker.h:92
edm::TypeWithDict retTypeFinal_
Definition: MethodInvoker.h:34
edm::ObjectWithDict invoke(const edm::ObjectWithDict &o, std::vector< StorageManager > &v) const
MethodInvoker & operator=(const MethodInvoker &)
StorageManager & operator=(const StorageManager &)=delete
std::pair< edm::ObjectWithDict, bool > invoke(const edm::ObjectWithDict &o, std::vector< StorageManager > &v) const
SingleInvoker const * invoker_
Definition: MethodInvoker.h:66
double invokeLast(const edm::ObjectWithDict &o, std::vector< StorageManager > &v) const
invoke and coerce result to double
edm::ObjectWithDict borrowStorage() const
fixed size matrix
std::vector< void * > args_
Definition: MethodInvoker.h:31
HLT enums.
std::string returnTypeName() const
std::vector< AnyMethodArgument > argsBeforeFixups_
edm::MemberWithDict const member() const
Definition: MethodInvoker.h:47
std::vector< MethodInvoker > invokers_
Definition: MethodInvoker.h:91
StorageManager(edm::ObjectWithDict const &iObj, SingleInvoker const *iInvoker, bool iNeedsDestructor_)
Definition: MethodInvoker.h:70
void throwFailedConversion(const edm::ObjectWithDict &) const
ROOT::VecOps::RVec< int > ints
Definition: Resolutions.cc:4
LazyInvoker(const std::string &name, const std::vector< AnyMethodArgument > &args)
SingleInvoker & operator=(const SingleInvoker &)=delete