ATLAS Offline Software
StorableConversions.h
Go to the documentation of this file.
1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
2 
3 /*
4  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 #ifndef ATHENAKERNEL_STORABLECONVERSIONS_H
8 # define ATHENAKERNEL_STORABLECONVERSIONS_H
9 
17 #include "GaudiKernel/DataObject.h"
18 
19 #include <memory>
20 #include <type_traits>
21 #include <typeinfo>
22 
23 
24 namespace SG {
25 
26  // put a dobj pointer in a bucket as appropriate
27  template <typename T>
28  DataObject* asStorable(T* pObject);
29 
30  // get a dobj pointer from a bucket as appropriate
31  template <typename T>
32  bool fromStorable(DataObject* pDObj, T*& pTrans, bool quiet=false,
33  IRegisterTransient* irt = 0,
34  bool isConst = true);
35 
36  template <typename T>
37  T* Storable_cast(DataObject* pDObj, bool quiet=true,
38  IRegisterTransient* irt = 0,
39  bool isConst = true) {
40  T* result;
41  return fromStorable(pDObj, result, quiet, irt, isConst) ? result : 0;
42  }
43 
44 
58  void* fromStorable(DataObject* pDObj, CLID clid,
59  const std::type_info* tinfo = nullptr,
60  bool quiet = false,
61  IRegisterTransient* irt = 0,
62  bool isConst = true);
63 
64 
78  void* Storable_cast(DataObject* pDObj, CLID clid,
79  const std::type_info* tinfo = nullptr,
80  bool quiet = false,
81  IRegisterTransient* irt = 0,
82  bool isConst = true);
83 }
84 
86 // implementation details
87 
88 
89 namespace SG {
90 
91 
128  template <class T, class U /* = T*/>
129  struct DataBucketTrait
130  {
131  // The first base of @c T (or @c SG::NoBase).
133 
134  // Test to see if it's valid.
135  static constexpr bool has_base = std::is_same_v<base1, SG::NoBase>;
136 
137  // This is what we'll return in the default (no base) case.
138  using deflt = std::type_identity<SG::DataBucket<U> >;
139 
140  // This is what we use to recursively check the base
142 
143  // Calculate the output.
145 
147  static void init() {}
148  };
149 
150 
151  // put a dobj pointer in a bucket as appropriate
152  template <typename T>
153  DataObject* asStorable(T* pObject) {
154  typedef typename DataBucketTrait<T>::type bucket_t;
155  return new bucket_t (pObject);
156  }
157 
158  template <typename T>
159  DataObject* asStorable(std::unique_ptr<T> pObject) {
160  typedef typename std::remove_const<T>::type T_nc;
161  typedef typename DataBucketTrait<T_nc>::type bucket_t;
162  return new bucket_t (std::move(pObject));
163  }
164 
165  // get a dobj pointer from a bucket as appropriate
166  //the DataObject must be a DataBucket<DATA>*.
167 
168  template <typename T>
169  bool fromStorable(DataObject* pDObj, T*& pTrans,
170  bool quiet,
171  IRegisterTransient* irt,
172  bool isConst /*= true*/)
173  {
174  typedef typename std::remove_const<T>::type T_nc;
176 
177  pTrans = static_cast<T*> (fromStorable (pDObj, ClassID_traits<T_nc>::ID(),
178  &typeid(T_nc),
179  quiet, irt, isConst));
180  return pTrans != nullptr;
181 }
182 
183 }
184 
185 #endif // ATHENAKERNEL_STORABLECONVERSIONS_H
SG::IRegisterTransient
Interface for registering a transient object in t2p map.
Definition: IRegisterTransient.h:28
SG::DataBucketTrait< ConstDataVector< CaloCellContainer > >::deflt
std::type_identity< SG::DataBucket< U > > deflt
Definition: StorableConversions.h:138
get_generator_info.result
result
Definition: get_generator_info.py:21
SG::DataBucketTrait::init
static void init()
Initialization hook. A no-op by default.
Definition: StorableConversions.h:147
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
SG::fromStorable
bool fromStorable(DataObject *pDObj, T *&pTrans, bool quiet=false, IRegisterTransient *irt=0, bool isConst=true)
Definition: StorableConversions.h:169
quiet
bool quiet
Definition: TrigGlobEffCorrValidation.cxx:190
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
DataBucket.h
SG::asStorable
DataObject * asStorable(SG::DataObjectSharedPtr< T > pObject)
Definition: DataObjectSharedPtr.h:65
ClassID_traits.h
a traits class that associates a CLID to a type T It also detects whether T inherits from Gaudi DataO...
SG::BaseType_t
typename SG::BaseType< T >::type BaseType_t
Definition: Bases.h:62
DataBucketTraitFwd.h
ClassID_traits
Default, invalid implementation of ClassID_traits.
Definition: Control/AthenaKernel/AthenaKernel/ClassID_traits.h:37
CLID
uint32_t CLID
The Class ID type.
Definition: Event/xAOD/xAODCore/xAODCore/ClassID_traits.h:47
SG::Storable_cast
T * Storable_cast(DataObject *pDObj, bool quiet=true, IRegisterTransient *irt=0, bool isConst=true)
Definition: StorableConversions.h:37
SG::DataBucketTrait< ConstDataVector< CaloCellContainer > >::base1
SG::BaseType_t< typename SG::Bases< ConstDataVector< CaloCellContainer > >::bases::Base1 > base1
Definition: StorableConversions.h:132
SG::DataBucketTrait::has_base
static constexpr bool has_base
Definition: StorableConversions.h:135
SG::DataBucketTrait
Metafunction to find the proper DataBucket class for T.
Definition: DataBucketTraitFwd.h:28
SG::DataBucketTrait< ConstDataVector< CaloCellContainer > >::type
typename std::conditional_t< has_base, deflt, recurse >::type type
Definition: StorableConversions.h:144