ATLAS Offline Software
Loading...
Searching...
No Matches
T_AthenaPoolCnvBase.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 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//__________________________________________________________________________
28template <class T>
29T_AthenaPoolCnvBase<T>::T_AthenaPoolCnvBase(ISvcLocator* svcloc,
30 const char* name)
31 : AthenaPoolConverter(classID(), svcloc, name) {
32}
33//______________________________________________________________________________
34template <class T>
35StatusCode 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 const std::string className = ClassName<T>::name();
42 if (!m_classDesc) {
43 ATH_MSG_DEBUG("Retrieve class description for class " << className);
44 m_classDesc = RootType( typeid(T) );
45 }
46 return(StatusCode::SUCCESS);
47}
48//__________________________________________________________________________
49template <class T>
50CLID T_AthenaPoolCnvBase<T>::classID() {
51 return(ClassID_traits<T>::ID());
52}
53//__________________________________________________________________________
54template <class T>
55StatusCode T_AthenaPoolCnvBase<T>::DataObjectToPool(IOpaqueAddress* pAddr, DataObject* pObj) {
56 const std::string className = ClassName<T>::name();
57 T* obj = nullptr;
58 bool success = SG::fromStorable(pObj, obj);
59 if (!success || obj == nullptr) {
60 ATH_MSG_ERROR("failed to cast to T for class (type/key) " << className << "/" << pObj->name());
61 return(StatusCode::FAILURE);
62 }
63 Placement placement = setPlacement(pObj->name(), *pAddr->par());
64 // No lock needed for m_classDesc, since set only in initialize()
65 std::unique_ptr<Token> token(m_athenaPoolCnvSvc->registerForWrite(&placement, obj, m_classDesc));
66 // Null/empty token means ERROR
67 if (token == nullptr || token->classID() == Guid::null()) {
68 ATH_MSG_ERROR("failed to get Token for class (type/key) " << className << "/" << pObj->name());
69 return(StatusCode::FAILURE);
70 }
71 // Update IOpaqueAddress for this object.
72 TokenAddress* tokAddr = dynamic_cast<TokenAddress*>(pAddr);
73 if (tokAddr != nullptr) {
74 tokAddr->setToken(std::move(token));
75 } else {
76 return(StatusCode::FAILURE);
77 }
78 return(StatusCode::SUCCESS);
79}
80//__________________________________________________________________________
81template <class T>
82StatusCode T_AthenaPoolCnvBase<T>::PoolToDataObject(DataObject*& pObj,
83 const Token* token,
84 const std::string& key)
85{
86 const std::string className = ClassName<T>::name();
87 void* voidPtr = nullptr;
88 try {
89 m_athenaPoolCnvSvc->setObjPtr(voidPtr, token);
90 }
91 catch (const std::exception& ex) {
92 AthenaPoolCnvSvc::throwExcCaughtException (__PRETTY_FUNCTION__,
93 "converting to DataObject",
94 ex,
95 typeid(T),
96 key);
97 }
98 T* obj = reinterpret_cast<T*>(voidPtr);
99 pObj = SG::asStorable(obj);
100 return(StatusCode::SUCCESS);
101}
102//__________________________________________________________________________
103template <class T>
104Placement T_AthenaPoolCnvBase<T>::setPlacement(const std::string& key, const std::string& output) {
105 const std::string typenm = ClassName<T>::name();
106 return(setPlacementWithType(typenm, key, output));
107}