ATLAS Offline Software
TrigEvent/TrigSteeringEvent/src/StringSerializer.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <cstring>
7 
8 
9 namespace TrigSteeringEvent {
10 
11 
12 const char* const StringSerializer::s_delimiter = "\n";
13 
14 
15 void StringSerializer::serialize (const std::vector<std::string>& strings, std::vector<uint32_t>& storage ) {
16 
17  m_ostream.str(""); // ostream reset
18 
19  // copy strings into m_ostream with delimiter after each element (even after last element)
20  copy(strings.begin(), strings.end(), std::ostream_iterator<std::string>(m_ostream, s_delimiter));
21 
22  /*
23  unsigned int i;
24  for ( i = 0 ; i < strings.size() ; ++i) {
25  std::cerr << "ser: " << strings[i] << std::endl;
26  }
27  */
28 
29  unsigned sizeToReserve = m_ostream.str().size();
30 
31  // Pad with '\n'
32  unsigned int sizeToPad = getPadding(sizeToReserve);
33 
34  if ( sizeToPad != 0 ) {
35  sizeToReserve += sizeToPad;
36  for ( unsigned i = 0 ; i < sizeToPad; i++ )
38  }
39  m_ostream.flush();
40 
41  std::string s (m_ostream.str());
42  const uint32_t * uarray = reinterpret_cast<const uint32_t*> (s.data());
43 
44  storage.push_back(strings.size()); // put number of strings first
45  storage.insert(storage.end(), &uarray[0], &uarray[sizeToReserve/sizeof(uint32_t)]);
46 
47  /*
48  std::cerr << "reserved = " << sizeToReserve << ", pad = " << sizeToPad
49  << ", storage size = " << storage.size() << ", serialized: " << std::endl;
50 
51  for ( unsigned int i = 0 ; i < sizeToReserve; ++i ) {
52  std::cerr << " " << std::hex << (int)carray[i] << std::dec << "(" << (char)carray[i] << ")";
53  }
54  std::cerr << " " << std::dec << std::endl;
55  */
56 
57 }
58 
59 void StringSerializer::serialize (const std::string& str, std::vector<uint32_t>& storage ) {
60  std::vector<std::string> tmp(1, str);
61  return serialize(tmp, storage);
62 }
63 
64 std::size_t StringSerializer::deserialize (std::vector<uint32_t>::const_iterator first,
65  std::vector<uint32_t>::const_iterator last,
66  std::vector<std::string>& strings)
67 {
68  std::size_t storageSize = std::distance(first, last);
69 
70  if ( storageSize <= 1 ) return storageSize;
71  unsigned int numOfStrings = *first;
72 
73  /*
74  unsigned int i;
75  std::cerr << "deserialized: " << std::endl;
76  for ( i = 0 ; i < storage.size()-1; ++i ) {
77  std::cerr << " " << std::hex << uarray[i] ;
78  }
79  std::cerr << " " << std::dec << std::endl;
80  */
81 
82  std::string whole(reinterpret_cast<const char*>(&*(first+1)),
83  (storageSize-1)*sizeof(uint32_t));
84 
85  m_istream.clear(); // istream reset
86  m_istream.str(whole);
87 
88  std::string one;
89  unsigned int readInStrings=0;
90  std::size_t nChars=0;
91 
92  while ( m_istream.good() && readInStrings < numOfStrings ) {
93  getline(m_istream, one, s_delimiter[0]);
94  strings.push_back(one);
95  readInStrings++;
96  nChars += one.size() + 1;
97  }
98 
99  return 1 + (nChars+getPadding(nChars))/sizeof(uint32_t); // # uint32_t words (incl. header)
100 }
101 
102 std::size_t StringSerializer::deserialize (std::vector<uint32_t>::const_iterator first,
103  std::vector<uint32_t>::const_iterator last,
104  std::string& str)
105 {
106  std::vector<std::string> tmp;
107  std::size_t nWords = deserialize(first, last, tmp);
108  if (tmp.size() == 1 )
109  str = tmp[0];
110 
111  return nWords;
112 }
113 
114 unsigned int StringSerializer::inquireSize(const std::vector<uint32_t>& storage) {
115  if ( storage.size() <= 1 )
116  return 0;
117  return storage[0];
118 }
119 
120 
121 } // namespace TrigSteeringEvent
TrigSteeringEvent::StringSerializer::s_delimiter
static const char *const s_delimiter
default delimeter which is put between strings from the input vector while serialization happens
Definition: TrigEvent/TrigSteeringEvent/TrigSteeringEvent/StringSerializer.h:82
TrigSteeringEvent::StringSerializer::inquireSize
unsigned int inquireSize(const std::vector< uint32_t > &storage)
Return number of strings serialized into 'storage'.
Definition: TrigEvent/TrigSteeringEvent/src/StringSerializer.cxx:114
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
Trk::one
@ one
Definition: TrkDetDescr/TrkSurfaces/TrkSurfaces/RealQuadraticEquation.h:22
TrigSteeringEvent::StringSerializer::m_ostream
std::ostringstream m_ostream
sstream used in serialization
Definition: TrigEvent/TrigSteeringEvent/TrigSteeringEvent/StringSerializer.h:80
TrigSteeringEvent
Definition: TrigEvent/TrigSteeringEvent/src/StringSerializer.cxx:9
TrigSteeringEvent::StringSerializer::getPadding
unsigned int getPadding(unsigned int sizeToReserve)
Get number of padding bytes needed to align with uint32_t.
Definition: TrigEvent/TrigSteeringEvent/TrigSteeringEvent/StringSerializer.h:76
lumiFormat.i
int i
Definition: lumiFormat.py:85
TrigSteeringEvent::StringSerializer::serialize
void serialize(const std::vector< std::string > &strings, std::vector< uint32_t > &storage)
Serializes vector of strings into vector of integers.
Definition: TrigEvent/TrigSteeringEvent/src/StringSerializer.cxx:15
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
TrigSteeringEvent::StringSerializer::m_istream
std::istringstream m_istream
sstream used in de-serialization
Definition: TrigEvent/TrigSteeringEvent/TrigSteeringEvent/StringSerializer.h:81
TrigSteeringEvent::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)
Deserialize vector into strings.
Definition: TrigEvent/TrigSteeringEvent/src/StringSerializer.cxx:64
DeMoScan.first
bool first
Definition: DeMoScan.py:534
StringSerializer.h
python.SystemOfUnits.s
float s
Definition: SystemOfUnits.py:147
str
Definition: BTagTrackIpAccessor.cxx:11
calibdata.copy
bool copy
Definition: calibdata.py:26
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