ATLAS Offline Software
Loading...
Searching...
No Matches
MethodAccessor.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4#ifndef _ExpressionEvaluation_MethodAccessor_h_
5#define _ExpressionEvaluation_MethodAccessor_h_
6
13
14#include "TClass.h"
17#include "TVirtualCollectionProxy.h"
18#include "ClingCallWrapper.h"
19#include "TFunction.h"
20
21#include <memory>
22#include <map>
23#include <stdexcept>
24#include <cassert>
25#include <cstdint> //for uint64_t etc.
26
27namespace ExpressionParsing {
28
31 template <class T_Cont,typename T_src>
33 public:
35 const TVirtualCollectionProxy &collection_proxy,
36 const void *data,
37 [[maybe_unused]] unsigned int n_elements)
38 : m_methodWrapper(method_wrapper),
39 m_collectionProxy( collection_proxy.Generate())
40 {
41 void* data_nc ATLAS_THREAD_SAFE = const_cast<void *>(data); // required by TVirtualCollectionProxy
42 m_collectionProxy->PushProxy(data_nc);
43 assert( m_collectionProxy->Size() == n_elements);
44 }
45
46 std::size_t size(const T_Cont &/*cont*/) const {
47 return m_collectionProxy->Size();
48 }
49
51 T_src get(const T_Cont &/*cont*/) const {
52 assert( m_collectionProxy->Size() == 1);
53 T_src ret = m_methodWrapper((*m_collectionProxy)[0]);
54 return ret;
55 }
56
58 T_src get(const T_Cont &/*cont*/, std::size_t idx) const {
59 void *element_data=(*m_collectionProxy)[idx];
60 T_src ret = m_methodWrapper(element_data);
61 return ret;
62 }
63
66 class Kit {
67 public:
68 Kit(Kit &&) = default;
70 TVirtualCollectionProxy &collection_proxy)
71 : m_methodWrapper(std::move(method_wrapper)),
72 m_collectionProxy(&collection_proxy)
73 {}
74
81 private:
83 const TVirtualCollectionProxy *m_collectionProxy;
84 };
85
86 private:
88 std::unique_ptr<TVirtualCollectionProxy> m_collectionProxy;
89 };
90
93 template <class T_Cont,typename T_src>
95 public:
97 const void *data)
98 : m_methodWrapper(method_wrapper),
100 {
101 }
102
103 std::size_t size(const T_Cont &cont) const {
104 return getContainerSize(cont); //@TODO correct ? or 1 ?
105 }
106
108 T_src get(const T_Cont &/*cont*/) const {
109 void* data_nc ATLAS_THREAD_SAFE = const_cast<void *>(m_data);
110 T_src ret = this->m_methodWrapper(data_nc);
111 return ret;
112 }
113
115 T_src get(const T_Cont &/*cont*/, [[maybe_unused]] std::size_t idx) const {
116 assert( idx==0);
117 void* data_nc ATLAS_THREAD_SAFE = const_cast<void *>(m_data); // required by TMethodCall
118 T_src ret = this->m_methodWrapper(data_nc);
119 return ret;
120 }
121
124 class Kit {
125 public:
126 Kit(Kit &&) = default;
127 Kit(RootUtils::ClingCallWrapper<T_src> &&method_wrapper) : m_methodWrapper(std::move(method_wrapper)) {}
128
129 MethodHelper<T_Cont,T_src> create(const EventContext& /*ctx*/, SG::ReadHandle<T_Cont> &handle) const {
131 }
132 private:
134 };
135 private:
137 const void *m_data;
138 };
139
142 class MethodAccessorFactory : public Singleton<MethodAccessorFactory> {
143 private:
146 public:
148 virtual std::unique_ptr<IAccessor> create( const SG::ReadHandleKey<SG::AuxVectorBase> &key,
150 TVirtualCollectionProxy *proxy=nullptr) const = 0;
151 virtual std::unique_ptr<IAccessor> create( const SG::ReadHandleKey<SG::AuxElement> &key,
153 TVirtualCollectionProxy *proxy=nullptr) const = 0;
154 };
155
158 template <class T_src>
160 public:
166
168 virtual std::unique_ptr<IAccessor> create( const SG::ReadHandleKey<SG::AuxVectorBase> &key,
170 TVirtualCollectionProxy *proxy) const override {
171 if (!proxy) {
172 const auto msg = "Cannot use method access of types SG::AuxVectorBase without a collection proxy.";
173 throw std::logic_error(msg);
174 }
175 return createAccessor<SG::AuxVectorBase, VectorHelper>(key,std::move(method_wrapper),proxy,m_vectorType);
176 }
177
179 virtual std::unique_ptr<IAccessor> create( const SG::ReadHandleKey<SG::AuxElement> &key,
181 TVirtualCollectionProxy *proxy=nullptr) const override {
182 if (proxy) {
183 return createAccessor<SG::AuxElement,VectorHelper>(key,std::move(method_wrapper),proxy, m_vectorType);
184 }
185 else {
186 return createAccessor<SG::AuxElement,ScalarHelper>(key,std::move(method_wrapper),proxy, m_scalarType );
187 }
188 // @TODO really vectorType and GenScalarAccessor if collection proxy is given ?
189 }
190 private:
191 template <class T_Aux, class T_ScalarVectorHelper>
192 std::unique_ptr<IAccessor> createAccessor( const SG::ReadHandleKey<T_Aux> &key,
194 TVirtualCollectionProxy *proxy,
195 ExpressionParsing::IAccessor::VariableType variable_type) const {
196 if (proxy) {
197 return std::make_unique<GenAccessor<T_Aux,
199 T_ScalarVectorHelper> >(key,
201 RootUtils::getClingCallWrapperChecked<T_src>(std::move(method_wrapper)),
202 *proxy),
203 variable_type);
204 }
205 else {
206 return std::make_unique<GenAccessor<T_Aux,
208 T_ScalarVectorHelper> >(key,
210 RootUtils::getClingCallWrapperChecked<T_src>(std::move(method_wrapper))),
211 variable_type);
212 }
213 }
216 };
217
218 template <typename T>
219 static std::pair<std::string, std::pair<std::unique_ptr<IMethodAccessorKit>, TInterpreter::EReturnType> > keyAndIntMethodKit() {
220 return std::make_pair(RootUtils::getNormalizedTypeName<T>(),
221 std::make_pair(std::make_unique<MethodAccessorKit<T> >(IProxyLoader::VT_INT,IProxyLoader::VT_VECINT),
222 TInterpreter::EReturnType::kLong));
223 }
224 template <typename T>
225 static std::pair<std::string, std::pair<std::unique_ptr<IMethodAccessorKit>, TInterpreter::EReturnType> > keyAndDoubleMethodKit() {
226 return std::make_pair(RootUtils::getNormalizedTypeName<T>(),
228 TInterpreter::EReturnType::kDouble));
229 }
230 public:
244
247 std::unique_ptr<IAccessor> create(const SG::ReadHandleKey<SG::AuxElement> &key,
249 TVirtualCollectionProxy *proxy=nullptr) const {
250 return getKit(method_wrapper).create(key,std::move(method_wrapper), proxy);
251 }
252
255 std::unique_ptr<IAccessor> create(const SG::ReadHandleKey<SG::AuxVectorBase> &key,
257 TVirtualCollectionProxy *proxy=nullptr) const {
258 return getKit(method_wrapper).create(key,std::move(method_wrapper), proxy);
259 }
260
261 private:
262
266
267 std::map<std::string, std::pair<std::unique_ptr<IMethodAccessorKit>, TInterpreter::EReturnType> >::const_iterator
268 iter = m_kits.find(method_wrapper.getReturnTypeNormalizedName());
269 if (iter == m_kits.end()) {
270 const std::string amsg = "ExpressionParsing::MethodAccessorFactory: no kit for return type " + method_wrapper.getReturnTypeNormalizedName()
271 + " wrapper " + typeid(method_wrapper).name();
272 throw std::runtime_error(amsg);
273 }
274 return *(iter->second.first);
275 }
276 std::map<std::string, std::pair<std::unique_ptr<IMethodAccessorKit>, TInterpreter::EReturnType> > m_kits;
277 };
278
279}
280#endif
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
Property holding a SG store/key/clid from which a ReadHandle is made.
Handle class for reading from StoreGate.
Helper for thread-safe TMethodCall. Extracted from Type.
Define macros for attributes used to control the static checker.
Auxiliary class to create the corresponding auxiliary helper object.
CollectionMethodHelper< T_Cont, T_src > create(const EventContext &, SG::ReadHandle< T_Cont > &handle) const
Kit(RootUtils::ClingCallWrapper< T_src > &&method_wrapper, TVirtualCollectionProxy &collection_proxy)
RootUtils::ClingCallWrapper< T_src > m_methodWrapper
const TVirtualCollectionProxy * m_collectionProxy
std::unique_ptr< TVirtualCollectionProxy > m_collectionProxy
RootUtils::ClingCallWrapper< T_src > m_methodWrapper
T_src get(const T_Cont &) const
Get the scalar provided by the container.
std::size_t size(const T_Cont &) const
CollectionMethodHelper(const RootUtils::ClingCallWrapper< T_src > &method_wrapper, const TVirtualCollectionProxy &collection_proxy, const void *data, unsigned int n_elements)
T_src get(const T_Cont &, std::size_t idx) const
Get the specified element of the vector provided by the container.
Generic accessor to access xAOD object content.
Definition GenAccessor.h:74
virtual std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxElement > &key, RootUtils::ClingCallWrapperUncheckedReturnValue<> &&method_wrapper, TVirtualCollectionProxy *proxy=nullptr) const =0
virtual std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxVectorBase > &key, RootUtils::ClingCallWrapperUncheckedReturnValue<> &&method_wrapper, TVirtualCollectionProxy *proxy=nullptr) const =0
Auxiliary class to create a specific accessor which calls a method of an AuxElement or AuxVectorBase.
virtual std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxElement > &key, RootUtils::ClingCallWrapperUncheckedReturnValue<> &&method_wrapper, TVirtualCollectionProxy *proxy=nullptr) const override
create an accessor which called the specified method of an AuxElement.
ExpressionParsing::IAccessor::VariableType m_vectorType
virtual std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxVectorBase > &key, RootUtils::ClingCallWrapperUncheckedReturnValue<> &&method_wrapper, TVirtualCollectionProxy *proxy) const override
create an accessor which called the specified method of an AuxVectorBase.
MethodAccessorKit(ExpressionParsing::IAccessor::VariableType scalar_type, ExpressionParsing::IAccessor::VariableType vector_type)
std::unique_ptr< IAccessor > createAccessor(const SG::ReadHandleKey< T_Aux > &key, RootUtils::ClingCallWrapperUncheckedReturnValue<> &&method_wrapper, TVirtualCollectionProxy *proxy, ExpressionParsing::IAccessor::VariableType variable_type) const
ExpressionParsing::IAccessor::VariableType m_scalarType
std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxElement > &key, RootUtils::ClingCallWrapperUncheckedReturnValue<> &&method_wrapper, TVirtualCollectionProxy *proxy=nullptr) const
Create an accessor which calls the specified method of an AuxElement.
std::map< std::string, std::pair< std::unique_ptr< IMethodAccessorKit >, TInterpreter::EReturnType > > m_kits
static std::pair< std::string, std::pair< std::unique_ptr< IMethodAccessorKit >, TInterpreter::EReturnType > > keyAndIntMethodKit()
std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxVectorBase > &key, RootUtils::ClingCallWrapperUncheckedReturnValue<> &&method_wrapper, TVirtualCollectionProxy *proxy=nullptr) const
Create an accessor which calls the specified method of an AuxVectorBase.
const IMethodAccessorKit & getKit(const RootUtils::ClingCallWrapperUncheckedReturnValue<> &method_wrapper) const
Get an specific class which creates the accessor for the given method.
static std::pair< std::string, std::pair< std::unique_ptr< IMethodAccessorKit >, TInterpreter::EReturnType > > keyAndDoubleMethodKit()
Auxiliary class to create the corresponding auxiliary helper object.
MethodHelper< T_Cont, T_src > create(const EventContext &, SG::ReadHandle< T_Cont > &handle) const
RootUtils::ClingCallWrapper< T_src > m_methodWrapper
Kit(RootUtils::ClingCallWrapper< T_src > &&method_wrapper)
T_src get(const T_Cont &) const
Get the scalar provided by the container.
MethodHelper(const RootUtils::ClingCallWrapper< T_src > &method_wrapper, const void *data)
RootUtils::ClingCallWrapper< T_src > m_methodWrapper
std::size_t size(const T_Cont &cont) const
T_src get(const T_Cont &, std::size_t idx) const
Get the specified element of the vector provided by the container.
Intermediate helper class wrapping a cling method call with arbitrary return type.
std::string getReturnTypeNormalizedName() const
the normalized name of the function return type.
Helper class to wrap a cling method call of an object's method with defined return type and argument ...
Property holding a SG store/key/clid from which a ReadHandle is made.
const_pointer_type cptr()
Dereference the pointer.
Namespace holding all the expression evaluation code.
const SG::AuxVectorData * getVectorData(const T &cont)
CxxUtils::CachedUniquePtrT< T_Derived > Singleton< T_Derived >::s_instance ATLAS_THREAD_SAFE
ClingCallWrapper< T_ReturnValue, T_MethodArgs... > getClingCallWrapperChecked(ClingCallWrapperUncheckedReturnValue< T_MethodArgs... > &&call_wrapper)
wrap a cling method call for a method with defined arguments and return types.
std::string getNormalizedTypeName()
STL namespace.
Convert a type_info to a normalized string representation (matching the names used in the root dictio...
MsgStream & msg
Definition testRead.cxx:32