1 // Dear emacs, this is -*- c++ -*-
4 Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
10 #include "AthenaKernel/getMessageSvc.h"
11 #include "AthenaKernel/ThinningDecisionBase.h"
12 #include "GaudiKernel/MsgStream.h"
14 /* #define __ELVDEBUG */
16 // Standard Constructor
17 template <typename DOBJ>
18 SG::ELVRef<DOBJ>::ELVRef ( const ElemLink& link ) :
19 m_link( link ), m_shortRef( 0 ), m_index(link.index())
24 template <typename DOBJ>
25 SG::ELVRef<DOBJ>::ELVRef(const ELVRef& rhs) :
27 m_shortRef(rhs.m_shortRef),
31 // constructor from indices.
32 template <typename DOBJ>
33 SG::ELVRef<DOBJ>::ELVRef
34 (typename DataLinkVector::size_type hostIndex,
35 index_type elementIndex,
36 const ElemLinkVec& owner)
37 : m_shortRef (hostIndex),
44 template <typename DOBJ>
45 typename SG::ELVRef<DOBJ>::index_type
46 SG::ELVRef<DOBJ>::elementIndex() const
48 return m_link.index();
52 template <typename DOBJ>
53 typename SG::ELVRef<DOBJ>::ID_type
54 SG::ELVRef<DOBJ>::dataID() const {
55 return m_link.dataID();
58 // Move to persistent state
59 template <typename DOBJ>
61 SG::ELVRef<DOBJ>::toPersistent
62 (const ElemLinkVec& owner)
64 // Handle null reference.
65 if (m_link.isDefault())
68 // Do the contained EL.
69 if (!m_link.toPersistent())
72 // Transfer the index.
73 m_index = this->m_link.index();
75 // Calculate the distance in DataLinkVector
76 typename DataLinkVector::const_iterator iHost(owner.findHostDObj(m_link));
78 if ( iHost != owner.endHostDObjs() ) {
79 m_shortRef = std::distance(owner.beginHostDObjs(), iHost);
82 MsgStream log(Athena::getMessageSvc(), "SG::ELVRef");
84 << "link not found in ElemLinkVector" << endmsg;
91 // Move to persistent state and return shortref.
92 template <typename DOBJ>
95 SG::ELVRef<DOBJ>::toPersistent
96 (const ElemLinkVec& owner,
97 typename DataLinkVector::size_type& shortref)
99 bool ret = toPersistent (owner);
100 shortref = m_shortRef;
105 // Move to transient state
106 template <typename DOBJ>
108 SG::ELVRef<DOBJ>::toTransient
109 (const ElemLinkVec& owner)
114 typename DataLinkVector::const_iterator iHost(owner.beginHostDObjs());
116 // handle thinned-away/null elements
117 if ( m_shortRef == SG::ThinningDecisionBase::RemovedIdx ||
118 !IndexingPolicy::isValid(m_index))
124 if (m_shortRef >= (unsigned)(owner.endHostDObjs() - iHost)) {
125 MsgStream log(Athena::getMessageSvc(), "ElementLinkVector");
127 << "ELVRef::toTransient: shortref index of " << m_shortRef
128 << " is beyond the end of the host container (size "
129 << owner.endHostDObjs() - iHost << ")" << endmsg;
134 std::advance(iHost, m_shortRef);
135 assert(iHost != owner.endHostDObjs());
137 // create a new ElementLink
138 m_link = ElemLink(iHost->key(), this->index());
143 template <typename DOBJ>
145 SG::ELVRef<DOBJ>::doRemap()
148 index_type new_index;
149 if (this->m_link.isDefault())
151 index_type index = this->m_link.index();
153 if (SG_detail::checkForRemap (this->m_link.source(),
159 this->m_link.resetWithKeyAndIndex (new_key, new_index);
167 template <typename DOBJ>
169 SG::ELVRef<DOBJ>::operator=(const ELVRef& rhs) {
171 MsgStream log(Athena::getMessageSvc(), "SG::ELVRef");
173 << "operator= called on " << dataID() << '/' << index()
174 << " with rhs " << rhs.dataID() << '/' << rhs.index()
179 m_index = rhs.m_index;
181 m_shortRef = rhs.m_shortRef;
184 MsgStream log(Athena::getMessageSvc(), "SG::ELVRef");
186 << "operator= result is " << dataID() << '/' << index()