ATLAS Offline Software
TrigEvent/TrigNavStructure/Root/StringSerializer.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <cstring>
6 #include <sstream>
7 #include <iterator>
8 
9 #include <iostream>
10 
12 
13 // the implementation is almost exact copy of the one in the TrigSerializeResult
14 
15 namespace {
16  const char* const delimiter = "\n";
17 
18  unsigned int getPadding(unsigned int sizeToReserve) {
19  return (sizeof(uint32_t) - sizeToReserve%sizeof(uint32_t)) % sizeof(uint32_t);
20  }
21 }
22 namespace HLT {
23 namespace StringSerializer {
24  /* unsigned int inquireSize(const std::vector<uint32_t>& storage) {
25  if ( storage.size() <= 1 )
26  return 0;
27  return storage[0];
28  }
29  */
30 
31  void serialize (const std::vector<std::string>& strings, std::vector<uint32_t>& storage ) {
32  std::ostringstream ostream;
33  ostream.str(""); // ostream reset
34 
35  // copy strings into m_ostream with delimiter after each element (even after last element)
36  copy(strings.begin(), strings.end(), std::ostream_iterator<std::string>(ostream, delimiter));
37 
38  unsigned sizeToReserve = ostream.str().size();
39 
40  // Pad with '\n'
41  unsigned int sizeToPad = getPadding(sizeToReserve);
42 
43  if ( sizeToPad != 0 ) {
44  sizeToReserve += sizeToPad;
45  for ( unsigned i = 0 ; i < sizeToPad; i++ )
46  ostream << delimiter;
47  }
48  ostream.flush();
49 
50  char * carray = new char [sizeToReserve];
51  strncpy(carray, ostream.str().c_str(), sizeToReserve);
52  const uint32_t * uarray = (const uint32_t*) carray;
53 
54  storage.push_back(strings.size()); // put number of strings first
55  storage.insert(storage.end(), &uarray[0], &uarray[sizeToReserve/sizeof(uint32_t)]);
56 
57  delete[] carray;
58  }
59 
60  void serialize (const std::string& str, std::vector<uint32_t>& storage ) {
61  std::vector<std::string> tmp(1, str);
62  return serialize(tmp, storage);
63  }
64 
65 
66  std::size_t deserialize (std::vector<uint32_t>::const_iterator first,
67  std::vector<uint32_t>::const_iterator last,
68  std::vector<std::string>& strings)
69  {
70  std::size_t storageSize = std::distance(first, last);
71 
72  if ( storageSize <= 1 ) return storageSize;
73  unsigned int numOfStrings = *first;
74 
75  // Copy storage into array of uint32_t
76  uint32_t * uarray = new uint32_t[storageSize-1];
77  std::vector<uint32_t>::const_iterator itBegin = first;
78  advance(itBegin, 1);
79  copy(itBegin, last, &uarray[0]);
80 
81  const char * carray = (const char*)uarray;
82  std::string whole(carray, (storageSize-1)*sizeof(uint32_t));
83  std::istringstream istream;
84 
85  istream.clear(); // istream reset
86  istream.str(whole);
87 
88  std::string one;
89  unsigned int readInStrings=0;
90  std::size_t nChars=0;
91 
92  while ( istream.good() && readInStrings < numOfStrings ) {
93  getline(istream, one, delimiter[0]);
94  strings.push_back(one);
95  readInStrings++;
96  nChars += one.size() + 1;
97  }
98  delete[] uarray;
99 
100  return 1 + (nChars+getPadding(nChars))/sizeof(uint32_t); // # uint32_t words (incl. header)
101  }
102 
103  std::size_t deserialize (std::vector<uint32_t>::const_iterator first,
104  std::vector<uint32_t>::const_iterator last,
105  std::string& str)
106  {
107  std::vector<std::string> tmp;
108  std::size_t nWords = deserialize(first, last, tmp);
109  if (tmp.size() == 1 )
110  str = tmp[0];
111 
112  return nWords;
113  }
114 
115 
116 } // eof StringSerialzier
117 } // eof HLT
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
Trk::one
@ one
Definition: TrkDetDescr/TrkSurfaces/TrkSurfaces/RealQuadraticEquation.h:22
HLT::StringSerializer::serialize
void serialize(const std::vector< std::string > &strings, std::vector< uint32_t > &storage)
Definition: TrigEvent/TrigNavStructure/Root/StringSerializer.cxx:31
StringSerializer.h
python.AthDsoLogger.delimiter
delimiter
Definition: AthDsoLogger.py:71
HLT
It used to be useful piece of code for replacing actual SG with other store of similar functionality ...
Definition: HLTResultReader.h:26
lumiFormat.i
int i
Definition: lumiFormat.py:92
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
HLT::StringSerializer::deserialize
std::size_t deserialize(std::vector< uint32_t >::const_iterator first, std::vector< uint32_t >::const_iterator last, std::vector< std::string > &strings)
Definition: TrigEvent/TrigNavStructure/Root/StringSerializer.cxx:66
DeMoScan.first
bool first
Definition: DeMoScan.py:534
StringSerializer
Utility class (not a tool or so) to serialize strings into stream of 32bit integers.
Definition: TrigDataAccess/TrigSerializeResult/TrigSerializeResult/StringSerializer.h:19
str
Definition: BTagTrackIpAccessor.cxx:11
calibdata.copy
bool copy
Definition: calibdata.py:27
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54