ATLAS Offline Software
T_AthenaPoolCnvBase.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 /** @file T_AthenaPoolCnvBase.icc
6  * @brief This file contains the implementation for the templated T_AthenaPoolCnvBase class.
7  * @author Peter van Gemmeren <gemmeren@anl.gov>
8  **/
9 
10 #include "AthenaPoolCnvSvc/IAthenaPoolCnvSvc.h"
11 
12 #include "GaudiKernel/StatusCode.h"
13 #include "GaudiKernel/DataObject.h"
14 #include "GaudiKernel/IOpaqueAddress.h"
15 #include "GaudiKernel/IRegistry.h"
16 #include "PersistentDataModel/Placement.h"
17 #include "PersistentDataModel/Token.h"
18 #include "PersistentDataModel/TokenAddress.h"
19 
20 #include "DataModelRoot/RootType.h"
21 
22 #include "AthenaKernel/CLASS_DEF.h"
23 #include "AthenaKernel/ClassName.h"
24 #include "AthenaKernel/StorableConversions.h"
25 #include "AthenaPoolCnvSvc/exceptions.h"
26 
27 //__________________________________________________________________________
28 template <class T>
29 T_AthenaPoolCnvBase<T>::T_AthenaPoolCnvBase(ISvcLocator* svcloc,
30  const char* name /*= nullptr*/)
31  : AthenaPoolConverter(classID(), svcloc, name) {
32 }
33 //______________________________________________________________________________
34 template <class T>
35 StatusCode T_AthenaPoolCnvBase<T>::initialize() {
36  ATH_MSG_DEBUG("initialize() in T_AthenaPoolCnvBase " << classID());
37  if (!AthenaPoolConverter::initialize().isSuccess()) {
38  ATH_MSG_FATAL("Failed to initialize AthenaPoolConverter base class.");
39  return(StatusCode::FAILURE);
40  }
41  return(StatusCode::SUCCESS);
42 }
43 //__________________________________________________________________________
44 template <class T>
45 const CLID& T_AthenaPoolCnvBase<T>::classID() {
46  return(ClassID_traits<T>::ID());
47 }
48 //__________________________________________________________________________
49 template <class T>
50 StatusCode T_AthenaPoolCnvBase<T>::DataObjectToPers(DataObject* pObj, IOpaqueAddress*& /*pAddr*/) {
51  const std::string className = ClassName<T>::name();
52  if (!m_classDesc) {
53  ATH_MSG_DEBUG("Retrieve class description for class (type/key) " << className << "/" << pObj->name());
54  m_classDesc = RootType( typeid(T) );
55  }
56  return(StatusCode::SUCCESS);
57 }
58 //__________________________________________________________________________
59 template <class T>
60 StatusCode T_AthenaPoolCnvBase<T>::DataObjectToPool(IOpaqueAddress* pAddr, DataObject* pObj) {
61  const std::string className = ClassName<T>::name();
62  T* obj = nullptr;
63  bool success = SG::fromStorable(pObj, obj);
64  if (!success || obj == nullptr) {
65  ATH_MSG_ERROR("failed to cast to T for class (type/key) " << className << "/" << pObj->name());
66  return(StatusCode::FAILURE);
67  }
68  Placement placement = setPlacement(pObj->name(), *pAddr->par());
69  Token* token = m_athenaPoolCnvSvc->registerForWrite(&placement, obj, m_classDesc);
70  // Null/empty token means ERROR
71  if (token == nullptr || token->classID() == Guid::null()) {
72  ATH_MSG_ERROR("failed to get Token for class (type/key) " << className << "/" << pObj->name());
73  return(StatusCode::FAILURE);
74  }
75  // Update IOpaqueAddress for this object.
76  TokenAddress* tokAddr = dynamic_cast<TokenAddress*>(pAddr);
77  if (tokAddr != nullptr) {
78  tokAddr->setToken(token); token = nullptr;
79  } else {
80  delete token; token = nullptr;
81  return(StatusCode::FAILURE);
82  }
83  return(StatusCode::SUCCESS);
84 }
85 //__________________________________________________________________________
86 template <class T>
87 StatusCode T_AthenaPoolCnvBase<T>::PoolToDataObject(DataObject*& pObj,
88  const Token* token,
89  const std::string& key)
90 {
91  const std::string className = ClassName<T>::name();
92  void* voidPtr = nullptr;
93  try {
94  m_athenaPoolCnvSvc->setObjPtr(voidPtr, token);
95  }
96  catch (const std::exception& ex) {
97  AthenaPoolCnvSvc::throwExcCaughtException (__PRETTY_FUNCTION__,
98  "converting to DataObject",
99  ex,
100  typeid(T),
101  key);
102  }
103  T* obj = reinterpret_cast<T*>(voidPtr);
104  pObj = SG::asStorable(obj);
105  return(StatusCode::SUCCESS);
106 }
107 //__________________________________________________________________________
108 template <class T>
109 Placement T_AthenaPoolCnvBase<T>::setPlacement(const std::string& key, const std::string& output) {
110  const std::string typenm = ClassName<T>::name();
111  return(setPlacementWithType(typenm, key, output));
112 }