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-2020 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 #ifndef ATHENAKERNEL_STORABLECONVERSIONS_H
8 # define ATHENAKERNEL_STORABLECONVERSIONS_H
9 
20 #include "GaudiKernel/DataObject.h"
21 
22 #ifndef NDEBUG
24 # include "GaudiKernel/MsgStream.h"
25 #endif
26 
27 #include <type_traits>
28 #include "boost/mpl/identity.hpp"
29 #include "boost/mpl/eval_if.hpp"
30 #include <memory>
31 #include <type_traits>
32 
33 
34 namespace SG {
35 
36  // put a dobj pointer in a bucket as appropriate
37  template <typename T>
38  DataObject* asStorable(T* pObject);
39 
40  // get a dobj pointer from a bucket as appropriate
41  template <typename T>
42  bool fromStorable(DataObject* pDObj, T*& pTrans, bool quiet=false,
43  IRegisterTransient* irt = 0,
44  bool isConst = true);
45 
46  template <typename T>
47  T* Storable_cast(DataObject* pDObj, bool quiet=true,
48  IRegisterTransient* irt = 0,
49  bool isConst = true) {
50  T* result;
51  return fromStorable(pDObj, result, quiet, irt, isConst) ? result : 0;
52  }
53 
54 
66  void* fromStorable(DataObject* pDObj, CLID clid,
67  IRegisterTransient* irt = 0,
68  bool isConst = true);
69 
70 
82  void* Storable_cast(DataObject* pDObj, CLID clid,
83  IRegisterTransient* irt = 0,
84  bool isConst = true);
85 
86 }
87 
89 // implementation details
90 
91 
92 namespace SG {
93 
94 
131  template <class T, class U /* = T*/>
132  struct DataBucketTrait
133  {
134  // The first base of @c T (or @c SG::NoBase).
136 
137  // Test to see if it's valid.
139 
140  // This is what we'll return in the default (no base) case.
141  typedef boost::mpl::identity<SG::DataBucket<U> > deflt;
142 
143  // This is what we use to recursively check the base
145 
146  // Calculate the output.
147  // (Note that it's important to use @c eval_if here; otherwise,
148  // applying this to @c SG::NoBase won't work.)
150 
152  static void init() {}
153  };
154 
155 
156  // put a dobj pointer in a bucket as appropriate
157  template <typename T>
158  DataObject* asStorable(T* pObject) {
159  typedef typename DataBucketTrait<T>::type bucket_t;
160  return new bucket_t (pObject);
161  }
162 
163  template <typename T>
164  DataObject* asStorable(std::unique_ptr<T> pObject) {
165  typedef typename std::remove_const<T>::type T_nc;
166  typedef typename DataBucketTrait<T_nc>::type bucket_t;
167  return new bucket_t (std::move(pObject));
168  }
169 
170  template <typename T>
171  DataObject* asStorable(SG::DataObjectSharedPtr<T> pObject) {
172  typedef typename DataBucketTrait<T>::type bucket_t;
173  return new bucket_t (std::move(pObject));
174  }
175 
176  // get a dobj pointer from a bucket as appropriate
177  //the DataObject must be a DataBucket<DATA>*.
178 
179  template <typename T>
180  bool fromStorable(DataObject* pDObj, T*& pTrans,
181  bool
182 #ifndef NDEBUG
183  quiet
184 #endif
185  , IRegisterTransient* irt,
186  bool isConst /*= true*/)
187  {
188  typedef typename std::remove_const<T>::type T_nc;
190 
191  //check inputs
192  if (0 == pDObj) {
193  pTrans=0;
194 #ifndef NDEBUG
195  MsgStream gLog(Athena::getMessageSvc(), "SG::fromStorable");
196  gLog << MSG::WARNING << "null input pointer " << endmsg;
197 #endif
198  return false;
199  }
200 
201  // get T* from DataBucket:
202  // All objects in the event store nowadays are instances
203  // of DataBucket, so just do a static_cast.
204  DataBucketBase* b = static_cast<DataBucketBase*>(pDObj);
205  pTrans = b->template cast<T_nc> (irt, isConst);
206  bool success = pTrans != nullptr;
207 
208 #ifndef NDEBUG
209  if (!quiet && !success) {
210  MsgStream gLog(Athena::getMessageSvc(), "SG::fromStorable");
211  gLog << MSG::WARNING
212  << "can't convert stored DataObject " << pDObj
213  << " to type ("
215  << ")\n Unless you are following a symlink,"
216  << " it probably means you have a duplicate "
217  << "CLID = " << pDObj->clID()
218  << endmsg;
219  }
220 #endif
221  return success;
222  }
223 }
224 
225 #endif // ATHENAKERNEL_STORABLECONVERSIONS_H
SG::IRegisterTransient
Interface for registering a transient object in t2p map.
Definition: IRegisterTransient.h:28
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:152
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
DataBucketBase
A non-templated base class for DataBucket, allows to access the transient object address as a void*.
Definition: DataBucketBase.h:24
SG::DataBucketTrait< ConstDataVector< CaloCellContainer > >::type
boost::mpl::eval_if< has_base, deflt, recurse >::type type
Definition: StorableConversions.h:149
SG::fromStorable
bool fromStorable(DataObject *pDObj, T *&pTrans, bool quiet=false, IRegisterTransient *irt=0, bool isConst=true)
Definition: StorableConversions.h:180
quiet
bool quiet
Definition: TrigGlobEffCorrValidation.cxx:190
SG::asStorable
DataObject * asStorable(T *pObject)
Definition: StorableConversions.h:158
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
DataBucket.h
SG::DataBucketTrait::recurse
DataBucketTrait< base1, U > recurse
Definition: StorableConversions.h:144
ClassID_traits::typeName
static const std::string & typeName()
the demangled type name of T
Definition: Control/AthenaKernel/AthenaKernel/ClassID_traits.h:56
ClassID_traits.h
a traits class that associates a CLID to a type T It also detects whether T inherits from Gaudi DataO...
SG::DataBucketTrait::deflt
boost::mpl::identity< SG::DataBucket< U > > deflt
Definition: StorableConversions.h:141
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
DataBucketTraitFwd.h
DataObjectSharedPtr.h
Smart pointer to manage DataObject reference counts.
CLID
uint32_t CLID
The Class ID type.
Definition: Event/xAOD/xAODCore/xAODCore/ClassID_traits.h:47
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
SG::DataBucketTrait::base1
SG::BaseType< typename SG::Bases< T >::bases::Base1 >::type base1
Definition: StorableConversions.h:135
SG::Storable_cast
T * Storable_cast(DataObject *pDObj, bool quiet=true, IRegisterTransient *irt=0, bool isConst=true)
Definition: StorableConversions.h:47
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
SG::DataBucketTrait::has_base
std::is_same< base1, SG::NoBase >::type has_base
Definition: StorableConversions.h:138
SG::DataObjectSharedPtr
Smart pointer to manage DataObject reference counts.
Definition: DataObjectSharedPtr.h:46
SG::DataBucketTrait
Metafunction to find the proper DataBucket class for T.
Definition: DataBucketTraitFwd.h:30
SG::BaseType
Helper metafunction to get base class types.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:364
type_tools.h
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35