ATLAS Offline Software
Loading...
Searching...
No Matches
AthLinks/ElementLinkVector.icc
Go to the documentation of this file.
1// Dear emacs, this is -*- c++ -*-
2
3/*
4 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5*/
6
7#include "GaudiKernel/System.h"
8#include "AthenaKernel/errorcheck.h"
9
10#include <cassert>
11#include <stdexcept>
12/* #define __ELVDEBUG */
13
14template <typename DOBJ>
15typename ElementLinkVector<DOBJ>::DataLinkVector::const_iterator
16ElementLinkVector<DOBJ>::findHostDObj(const ElemLink& link) const
17{
18 //DataLink<DOBJ> dl (link.storagePolicy());
19 DataLink<DOBJ> dl = link.getDataLink();
20 typename DataLinkVector::const_iterator iHost(beginHostDObjs()), eHost(endHostDObjs());
21 while ((eHost != iHost) && (*iHost != dl)) ++iHost;
22#ifdef __ELVDEBUG
23 std::println ("findHostDObj (const version) called for link {}/{} - which is {} in ELV host dobjs list ",
24 link.dataID(), link.index(), ((iHost != eHost) ? "" : " NOT "));
25#endif
26 return iHost;
27}
28
29template <typename DOBJ>
30typename ElementLinkVector<DOBJ>::DataLinkVector::iterator
31ElementLinkVector<DOBJ>::findHostDObj(const ElemLink& link)
32{
33 //DataLink<DOBJ> dl (link.storagePolicy());
34 DataLink<DOBJ> dl (link.key());
35 typename DataLinkVector::iterator iHost(beginHostDObjs()), eHost(endHostDObjs());
36 while ((eHost != iHost) && (*iHost != dl)) ++iHost;
37#ifdef __ELVDEBUG
38 std::println ("findHostDObj (non-const version) called for link {}/{} - which is {} in ELV host dobjs list ",
39 link.dataID(), link.index(), ((iHost != eHost) ? "" : " NOT "));
40#endif
41 return iHost;
42}
43
44
45template <typename DOBJ>
46typename ElementLinkVector<DOBJ>::iterator
47ElementLinkVector<DOBJ>::insert(iterator position, const ElemLink& link)
48{
49#ifdef __ELVDEBUG
50 std::println ("insert of link {}/{} after {}/{}" ,
51 link.dataID(), link.index(),
52 position->dataID(), position->index());
53#endif
54 addHostDObj(link);
55 return iterator(m_shortRefs.insert(shortIterFromLong(position), ElemLinkRef( link)),
56 Short2LongRef());
57}
58
59template <typename DOBJ>
60void
61ElementLinkVector<DOBJ>::insert(iterator position, size_type n, const ElemLink& link)
62{
63 addHostDObj(link);
64 m_shortRefs.insert(shortIterFromLong(position), n, ElemLinkRef(link));
65}
66
67template <typename DOBJ>
68typename ElementLinkVector<DOBJ>::iterator
69ElementLinkVector<DOBJ>::erase(iterator position)
70{
71 const ElemLink& save(*position);
72 iterator ret(m_shortRefs.erase(shortIterFromLong(position)), Short2LongRef());
73 removeHostObj(save);
74 return ret;
75}
76
77template <typename DOBJ>
78typename ElementLinkVector<DOBJ>::iterator
79ElementLinkVector<DOBJ>::erase(iterator first, iterator last)
80{
81 iterator i(first);
82 while (i != last) removeHostObj(*i++); //FIXME won't work in ELVDEBUG as such
83 return iterator(m_shortRefs.erase(shortIterFromLong(first),
84 shortIterFromLong(last)),
85 Short2LongRef());
86}
87
88template <typename DOBJ>
89void
90ElementLinkVector<DOBJ>::
91resize(size_type sz, const ElemLink& link)
92{
93 if (sz > size()) { insert(end(), sz-size(), link); }
94 else if (sz < size()) { erase(begin()+sz, end()); }
95}
96
97template <typename DOBJ>
98bool
99ElementLinkVector<DOBJ>::toTransient()
100{
101 // Call the appropriate function for making use of the persistent data
102 // if we're doing direct ROOT I/O:
103 if( m_isDirectIO ) {
104 index_type dummy;
105 toTransient( dummy );
106 m_isDirectIO = false;
107 }
108
109 bool success = true;
110 // set the proper state of all DataLinks
111 typename DataLinkVector::iterator iter = beginHostDObjs();
112 for (; iter!= endHostDObjs(); ++iter) {
113 if (! (*iter).toTransient()) success = false;
114 }
115
116 // set the refVector's to transient mode. Note that RefVector would
117 // need the owner (this) as it is constructed from persistency using
118 // the default constructor
119 typename RefVector::iterator refIter = m_shortRefs.begin();
120 for (; refIter!= m_shortRefs.end(); ++refIter) {
121 if (! (*refIter).toTransient(*this)) success = false;
122 }
123
124 return success;
125}
126
127
128template <typename DOBJ>
129bool
130ElementLinkVector<DOBJ>::toPersistentDL()
131{
132 doRemap();
133
134 //set all DataLinks to persistent state
135 bool success = true;
136 typename DataLinkVector::iterator iter = beginHostDObjs();
137 for (; iter!= endHostDObjs(); ++iter) {
138 if (! (*iter).toPersistentNoRemap()) success = false;
139 }
140
141 return success;
142}
143
144
145template <typename DOBJ>
146bool
147ElementLinkVector<DOBJ>::toPersistent()
148{
149 bool success = toPersistentDL();
150
151 // set all ElementLink Refs to persistent state
152 typename RefVector::iterator refIter = m_shortRefs.begin();
153 for (; refIter!= m_shortRefs.end(); ++refIter) {
154 if (! (*refIter).toPersistent(*this)) success = false;
155 }
156
157 // WARNING: The code doesn't take thinning into account at the
158 // moment!!!
159
160 // Reset the base class's variables:
161 m_persKeys.clear();
162 m_persIndices.clear();
163
164 // Copy the info into the base class:
165 typename RefVector::const_iterator ref_itr = m_shortRefs.begin();
166 typename RefVector::const_iterator ref_end = m_shortRefs.end();
167 for( ; ref_itr != ref_end; ++ref_itr ) {
168 m_persKeys.push_back( ref_itr->elementLink().persKey() );
169 m_persIndices.push_back( ref_itr->elementLink().persIndex() );
170 }
171
172 return success;
173}
174
175
176template <typename DOBJ>
177bool
178ElementLinkVector<DOBJ>::toPersistent
179 (std::vector<typename DataLinkVector::size_type>& shortrefs)
180{
181 bool success = toPersistentDL();
182
183 // set all ElementLink Refs to persistent state
184 shortrefs.reserve (m_shortRefs.size());
185 typename RefVector::iterator refIter = m_shortRefs.begin();
186 for (; refIter!= m_shortRefs.end(); ++refIter) {
187 typename DataLinkVector::size_type shortref;
188 if (! (*refIter).toPersistent(*this, shortref)) success = false;
189 shortrefs.push_back (shortref);
190 }
191
192 return success;
193}
194
195
196template <typename DOBJ>
197void
198ElementLinkVector<DOBJ>::doRemap()
199{
200 bool remapping = false;
201 typename RefVector::iterator refIter = m_shortRefs.begin();
202 for (; refIter!= m_shortRefs.end(); ++refIter) {
203 if (refIter->doRemap()) {
204 if (!remapping) {
205 m_hostDObjs.clear();
206 typename RefVector::iterator ri2 = m_shortRefs.begin();
207 for (; ri2 != refIter; ++ri2) {
208 addHostDObj (ri2->elementLink());
209 }
210 remapping = true;
211 }
212 }
213 if (remapping)
214 addHostDObj (refIter->elementLink());
215 }
216}
217
218
219// Set the vector of host data objects from @a dobjs.
220// @a dobjs is destroyed.
221// This is an error if the vector is not empty.
222template <typename DOBJ>
223void
224ElementLinkVector<DOBJ>::moveHostDObjs
225 (DataLinkVector& dobjs)
226{
227 if (!this->empty()) {
228 MsgStream log(Athena::getMessageSvc(), "ElementLink");
229 log << MSG::ERROR
230 << "ElementLinkVector::moveHostDObjs called on non-empty vector."
231 << endmsg;
232 throw std::runtime_error("ElementLinkVector::moveHistDObjs : vector not empty");
233 }
234 m_hostDObjs.swap (dobjs);
235}
236
237
238template <typename DOBJ>
239void
240ElementLinkVector<DOBJ>::push_back
241 (typename DataLinkVector::size_type nameIndex,
242 typename ElemLinkRef::index_type elementIndex)
243{
244 m_shortRefs.push_back (ElemLinkRef (nameIndex, elementIndex, *this));
245}
246
247template< typename DOBJ >
248void ElementLinkVector< DOBJ >::
249toTransient( uint64_t& /*dummy*/ ) {
250
251 // A little sanity check:
252 assert( m_persKeys.size() == m_persIndices.size() );
253
254 // Clear the object:
255 clear();
256
257 // Now re-create the links based on the persistent info:
258 for( size_t i = 0; i < m_persKeys.size(); ++i ) {
259 push_back( ElemLink( m_persKeys[ i ], m_persIndices[ i ] ) );
260 }
261
262 return;
263}
264
265template< typename DOBJ >
266void ElementLinkVector< DOBJ >::
267toTransient( uint32_t& /*dummy*/ ) {
268
269 // A little sanity check:
270 assert( m_persKeys.size() == m_persIndices.size() );
271
272 // Clear the object:
273 clear();
274
275 // Now re-create the links based on the persistent info:
276 for( size_t i = 0; i < m_persKeys.size(); ++i ) {
277 push_back( ElemLink( m_persKeys[ i ], m_persIndices[ i ] ) );
278 }
279
280 return;
281}
282
283template< typename DOBJ >
284template< typename INDEX_TYPE >
285void ElementLinkVector< DOBJ >::
286toTransient( INDEX_TYPE& /*dummy*/ ) {
287
288 REPORT_MESSAGE_WITH_CONTEXT( MSG::ERROR, "ElementLinkVector" )
289 << "Using direct ROOT I/O for type "
290 << System::typeinfoName( typeid( this ) )
291 << " not supported";
292
293 return;
294}
295
296template< typename DOBJ >
297void ElementLinkVector< DOBJ >::
298addHostDObj( const ElemLink& link ) {
299
300 // Check if we already have it:
301 if( endHostDObjs() != findHostDObj( link ) ) {
302 return;
303 }
304
305 //DataLink< DOBJ > dl( link.storagePolicy() );
306 DataLink< DOBJ > dl( link.key() );
307 if( ! dl.isDefault() ) {
308 m_hostDObjs.push_back( dl );
309#ifdef __ELVDEBUG
310 std::println ("addHostDObj added link {}/{}",
311 link.dataID(), link.index());
312#endif
313 }
314
315 return;
316}