ATLAS Offline Software
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
TrigSteeringEvent::StringSerializer Class Reference

Utility class (not a tool or so) to serialize strings into stream of 32bit integers. More...

#include <StringSerializer.h>

Collaboration diagram for TrigSteeringEvent::StringSerializer:

Public Member Functions

void serialize (const std::vector< std::string > &strings, std::vector< uint32_t > &storage)
 Serializes vector of strings into vector of integers. More...
 
void serialize (const std::string &str, std::vector< uint32_t > &storage)
 
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. More...
 
std::size_t deserialize (const std::vector< uint32_t > &storage, std::vector< std::string > &strings)
 Same as above but with vector instead of iterators. More...
 
std::size_t deserialize (std::vector< uint32_t >::const_iterator first, std::vector< uint32_t >::const_iterator last, std::string &str)
 Deserialize one single string. More...
 
std::size_t deserialize (const std::vector< uint32_t > &storage, std::string &str)
 Deserialize one single string
More...
 
unsigned int inquireSize (const std::vector< uint32_t > &storage)
 Return number of strings serialized into 'storage'. More...
 

Private Member Functions

unsigned int getPadding (unsigned int sizeToReserve)
 Get number of padding bytes needed to align with uint32_t. More...
 

Private Attributes

std::ostringstream m_ostream
 sstream used in serialization More...
 
std::istringstream m_istream
 sstream used in de-serialization More...
 

Static Private Attributes

static const char *const s_delimiter = "\n"
 default delimeter which is put between strings from the input vector while serialization happens More...
 

Detailed Description

Utility class (not a tool or so) to serialize strings into stream of 32bit integers.

Definition at line 22 of file TrigEvent/TrigSteeringEvent/TrigSteeringEvent/StringSerializer.h.

Member Function Documentation

◆ deserialize() [1/4]

std::size_t TrigSteeringEvent::StringSerializer::deserialize ( const std::vector< uint32_t > &  storage,
std::string &  str 
)
inline

Deserialize one single string

Definition at line 62 of file TrigEvent/TrigSteeringEvent/TrigSteeringEvent/StringSerializer.h.

62  {
63  return deserialize(storage.begin(), storage.end(), str);
64  }

◆ deserialize() [2/4]

std::size_t TrigSteeringEvent::StringSerializer::deserialize ( const std::vector< uint32_t > &  storage,
std::vector< std::string > &  strings 
)
inline

Same as above but with vector instead of iterators.

Definition at line 52 of file TrigEvent/TrigSteeringEvent/TrigSteeringEvent/StringSerializer.h.

52  {
53  return deserialize(storage.begin(), storage.end(), strings);
54  }

◆ deserialize() [3/4]

std::size_t StringSerializer::deserialize ( std::vector< uint32_t >::const_iterator  first,
std::vector< uint32_t >::const_iterator  last,
std::string &  str 
)

Deserialize one single string.

Definition at line 112 of file TrigEvent/TrigSteeringEvent/src/StringSerializer.cxx.

115 {
116  std::vector<std::string> tmp;
117  std::size_t nWords = deserialize(first, last, tmp);
118  if (tmp.size() == 1 )
119  str = tmp[0];
120 
121  return nWords;
122 }

◆ deserialize() [4/4]

std::size_t StringSerializer::deserialize ( std::vector< uint32_t >::const_iterator  first,
std::vector< uint32_t >::const_iterator  last,
std::vector< std::string > &  strings 
)

Deserialize vector into strings.

Parameters
firstBeginning of 'byte-stream'
lastEnd of 'byte-stream', i.e. vector::end()
stringsVector of strings to hold result
Returns
Number of words consumed during deserialization

Definition at line 67 of file TrigEvent/TrigSteeringEvent/src/StringSerializer.cxx.

70 {
71  std::size_t storageSize = std::distance(first, last);
72 
73  if ( storageSize <= 1 ) return storageSize;
74  unsigned int numOfStrings = *first;
75 
76  // Copy storage into array of uint32_t
77  uint32_t * uarray = new uint32_t[storageSize-1];
78  std::vector<uint32_t>::const_iterator itBegin = first;
79  advance(itBegin, 1);
80  copy(itBegin, last, &uarray[0]);
81 
82  /*
83  unsigned int i;
84  std::cerr << "deserialized: " << std::endl;
85  for ( i = 0 ; i < storage.size()-1; ++i ) {
86  std::cerr << " " << std::hex << uarray[i] ;
87  }
88  std::cerr << " " << std::dec << std::endl;
89  */
90 
91  const char * carray = (const char*)uarray;
92  std::string whole(carray, (storageSize-1)*sizeof(uint32_t));
93 
94  m_istream.clear(); // istream reset
95  m_istream.str(whole);
96 
97  std::string one;
98  unsigned int readInStrings=0;
99  std::size_t nChars=0;
100 
101  while ( m_istream.good() && readInStrings < numOfStrings ) {
102  getline(m_istream, one, s_delimiter[0]);
103  strings.push_back(one);
104  readInStrings++;
105  nChars += one.size() + 1;
106  }
107  delete[] uarray;
108 
109  return 1 + (nChars+getPadding(nChars))/sizeof(uint32_t); // # uint32_t words (incl. header)
110 }

◆ getPadding()

unsigned int TrigSteeringEvent::StringSerializer::getPadding ( unsigned int  sizeToReserve)
inlineprivate

Get number of padding bytes needed to align with uint32_t.

Definition at line 76 of file TrigEvent/TrigSteeringEvent/TrigSteeringEvent/StringSerializer.h.

76  {
77  return (sizeof(uint32_t) - sizeToReserve%sizeof(uint32_t)) % sizeof(uint32_t);
78  }

◆ inquireSize()

unsigned int StringSerializer::inquireSize ( const std::vector< uint32_t > &  storage)

Return number of strings serialized into 'storage'.

Definition at line 124 of file TrigEvent/TrigSteeringEvent/src/StringSerializer.cxx.

124  {
125  if ( storage.size() <= 1 )
126  return 0;
127  return storage[0];
128 }

◆ serialize() [1/2]

void StringSerializer::serialize ( const std::string &  str,
std::vector< uint32_t > &  storage 
)

Definition at line 62 of file TrigEvent/TrigSteeringEvent/src/StringSerializer.cxx.

62  {
63  std::vector<std::string> tmp(1, str);
64  return serialize(tmp, storage);
65 }

◆ serialize() [2/2]

void StringSerializer::serialize ( const std::vector< std::string > &  strings,
std::vector< uint32_t > &  storage 
)

Serializes vector of strings into vector of integers.

The number of strings (size of input vector) is put first into the stream. In case the strings is an empty vector the 0 is put into the storage.

Parameters
strings
storageis place where serizlized from of stings is appended to

Definition at line 15 of file TrigEvent/TrigSteeringEvent/src/StringSerializer.cxx.

15  {
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  char * carray = new char [sizeToReserve];
42  strncpy(carray, m_ostream.str().c_str(), sizeToReserve);
43  const uint32_t * uarray = (const uint32_t*) carray;
44 
45  storage.push_back(strings.size()); // put number of strings first
46  storage.insert(storage.end(), &uarray[0], &uarray[sizeToReserve/sizeof(uint32_t)]);
47 
48  /*
49  std::cerr << "reserved = " << sizeToReserve << ", pad = " << sizeToPad
50  << ", storage size = " << storage.size() << ", serialized: " << std::endl;
51 
52  for ( unsigned int i = 0 ; i < sizeToReserve; ++i ) {
53  std::cerr << " " << std::hex << (int)carray[i] << std::dec << "(" << (char)carray[i] << ")";
54  }
55  std::cerr << " " << std::dec << std::endl;
56  */
57 
58  delete[] carray;
59 
60 }

Member Data Documentation

◆ m_istream

std::istringstream TrigSteeringEvent::StringSerializer::m_istream
private

sstream used in de-serialization

Definition at line 81 of file TrigEvent/TrigSteeringEvent/TrigSteeringEvent/StringSerializer.h.

◆ m_ostream

std::ostringstream TrigSteeringEvent::StringSerializer::m_ostream
private

sstream used in serialization

Definition at line 80 of file TrigEvent/TrigSteeringEvent/TrigSteeringEvent/StringSerializer.h.

◆ s_delimiter

const char *const StringSerializer::s_delimiter = "\n"
staticprivate

default delimeter which is put between strings from the input vector while serialization happens

Definition at line 82 of file TrigEvent/TrigSteeringEvent/TrigSteeringEvent/StringSerializer.h.


The documentation for this class was generated from the following files:
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
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::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:92
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:67
DeMoScan.first
bool first
Definition: DeMoScan.py:534
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