ATLAS Offline Software
VectorConverter.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id$
6 /**
7  * @file RootConversions/VectorConverter.icc
8  * @author scott snyder <snyder@bnl.gov>
9  * @date Oct, 2009, from previous code.
10  * @brief Template for streamer converter for vector<T> -> vector<U>,
11  * assuming T is convertable to U.
12  */
13 
14 #include <cassert>
15 #include "TROOT.h"
16 
17 namespace RootConversions {
18 
19 
20 /**
21  * @brief Constructor.
22  * @param tname The name of the vector element type T.
23  */
24 template <typename T, typename U>
25 VectorConverter<T, U>::VectorConverter (const char* tname)
26 {
27  std::string vname = "vector<";
28  vname += tname;
29  if (vname[vname.size()-1] == '>')
30  vname += ' ';
31  vname += '>';
32  m_cl = gROOT->GetClass (vname.c_str());
33 }
34 
35 
36 /**
37  * @brief Run the streamer.
38  * @param b Buffer from which to read.
39  * @param pmember Pointer to the object into which to read.
40  * @param size Number of instances to read.
41  */
42 template <typename T, typename U>
43 void VectorConverter<T, U>::operator() (TBuffer& b,
44  void* pmember,
45  Int_t size /*=0*/)
46 {
47  // This only works for reading!
48  assert (b.IsReading());
49 
50  // The transient object.
51  std::vector<U>* obj = reinterpret_cast<std::vector<U>*> (pmember);
52 
53  // We'll read into this object.
54  std::vector<T> tmp;
55 
56  while (size-- > 0) {
57  // Read into tmp and copy data to *obj.
58  tmp.clear();
59  m_cl->Streamer (&tmp, b);
60  obj->assign (tmp.begin(), tmp.end());
61  ++obj;
62  }
63 }
64 
65 
66 } // namespace RootConversions