ATLAS Offline Software
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
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 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::vector< std::string > deserialize (const std::vector< uint32_t > &storage)
 Same as above but return string by value (useful when invoked from Python) 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 19 of file TrigDataAccess/TrigSerializeResult/TrigSerializeResult/StringSerializer.h.

Member Function Documentation

◆ deserialize() [1/5]

std::vector<std::string> StringSerializer::deserialize ( const std::vector< uint32_t > &  storage)
inline

Same as above but return string by value (useful when invoked from Python)

Definition at line 54 of file TrigDataAccess/TrigSerializeResult/TrigSerializeResult/StringSerializer.h.

54  {
55  std::vector<std::string> strings;
56  deserialize(storage.begin(), storage.end(), strings);
57  return strings;
58  }

◆ deserialize() [2/5]

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

Deserialize one single string

Definition at line 66 of file TrigDataAccess/TrigSerializeResult/TrigSerializeResult/StringSerializer.h.

66  {
67  return deserialize(storage.begin(), storage.end(), str);
68  }

◆ deserialize() [3/5]

std::size_t 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 49 of file TrigDataAccess/TrigSerializeResult/TrigSerializeResult/StringSerializer.h.

49  {
50  return deserialize(storage.begin(), storage.end(), strings);
51  }

◆ deserialize() [4/5]

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 107 of file TrigDataAccess/TrigSerializeResult/src/StringSerializer.cxx.

110 {
111  std::vector<std::string> tmp;
112  std::size_t nWords = deserialize(first, last, tmp);
113  if (tmp.size() == 1 )
114  str = tmp[0];
115 
116  return nWords;
117 }

◆ deserialize() [5/5]

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 62 of file TrigDataAccess/TrigSerializeResult/src/StringSerializer.cxx.

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

◆ getPadding()

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

Get number of padding bytes needed to align with uint32_t.

Definition at line 80 of file TrigDataAccess/TrigSerializeResult/TrigSerializeResult/StringSerializer.h.

80  {
81  return (sizeof(uint32_t) - sizeToReserve%sizeof(uint32_t)) % sizeof(uint32_t);
82  }

◆ inquireSize()

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

Return number of strings serialized into 'storage'.

Definition at line 119 of file TrigDataAccess/TrigSerializeResult/src/StringSerializer.cxx.

119  {
120  if ( storage.size() <= 1 )
121  return 0;
122  return storage[0];
123 }

◆ serialize() [1/2]

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

Definition at line 57 of file TrigDataAccess/TrigSerializeResult/src/StringSerializer.cxx.

57  {
58  std::vector<std::string> tmp(1, str);
59  return serialize(tmp, storage);
60 }

◆ 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 10 of file TrigDataAccess/TrigSerializeResult/src/StringSerializer.cxx.

10  {
11 
12  m_ostream.str(""); // ostream reset
13 
14  // copy strings into m_ostream with delimiter after each element (even after last element)
15  copy(strings.begin(), strings.end(), std::ostream_iterator<std::string>(m_ostream, s_delimiter));
16 
17  /*
18  unsigned int i;
19  for ( i = 0 ; i < strings.size() ; ++i) {
20  std::cerr << "ser: " << strings[i] << std::endl;
21  }
22  */
23 
24  unsigned sizeToReserve = m_ostream.str().size();
25 
26  // Pad with '\n'
27  unsigned int sizeToPad = getPadding(sizeToReserve);
28 
29  if ( sizeToPad != 0 ) {
30  sizeToReserve += sizeToPad;
31  for ( unsigned i = 0 ; i < sizeToPad; i++ )
33  }
34  m_ostream.flush();
35 
36  char * carray = new char [sizeToReserve];
37  strncpy(carray, m_ostream.str().c_str(), sizeToReserve);
38  const uint32_t * uarray = (const uint32_t*) carray;
39 
40  storage.push_back(strings.size()); // put number of strings first
41  storage.insert(storage.end(), &uarray[0], &uarray[sizeToReserve/sizeof(uint32_t)]);
42 
43  /*
44  std::cerr << "reserved = " << sizeToReserve << ", pad = " << sizeToPad
45  << ", storage size = " << storage.size() << ", serialized: " << std::endl;
46 
47  for ( unsigned int i = 0 ; i < sizeToReserve; ++i ) {
48  std::cerr << " " << std::hex << (int)carray[i] << std::dec << "(" << (char)carray[i] << ")";
49  }
50  std::cerr << " " << std::dec << std::endl;
51  */
52 
53  delete[] carray;
54 
55 }

Member Data Documentation

◆ m_istream

std::istringstream StringSerializer::m_istream
private

sstream used in de-serialization

Definition at line 85 of file TrigDataAccess/TrigSerializeResult/TrigSerializeResult/StringSerializer.h.

◆ m_ostream

std::ostringstream StringSerializer::m_ostream
private

sstream used in serialization

Definition at line 84 of file TrigDataAccess/TrigSerializeResult/TrigSerializeResult/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 86 of file TrigDataAccess/TrigSerializeResult/TrigSerializeResult/StringSerializer.h.


The documentation for this class was generated from the following files:
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
Trk::one
@ one
Definition: TrkDetDescr/TrkSurfaces/TrkSurfaces/RealQuadraticEquation.h:22
StringSerializer::serialize
void serialize(const std::vector< std::string > &strings, std::vector< uint32_t > &storage)
Serializes vector of strings into vector of integers.
Definition: TrigDataAccess/TrigSerializeResult/src/StringSerializer.cxx:10
StringSerializer::m_istream
std::istringstream m_istream
sstream used in de-serialization
Definition: TrigDataAccess/TrigSerializeResult/TrigSerializeResult/StringSerializer.h:85
lumiFormat.i
int i
Definition: lumiFormat.py:85
StringSerializer::m_ostream
std::ostringstream m_ostream
sstream used in serialization
Definition: TrigDataAccess/TrigSerializeResult/TrigSerializeResult/StringSerializer.h:84
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: TrigDataAccess/TrigSerializeResult/src/StringSerializer.cxx:62
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
StringSerializer::s_delimiter
static const char *const s_delimiter
default delimeter which is put between strings from the input vector while serialization happens
Definition: TrigDataAccess/TrigSerializeResult/TrigSerializeResult/StringSerializer.h:86
DeMoScan.first
bool first
Definition: DeMoScan.py:536
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
StringSerializer::getPadding
unsigned int getPadding(unsigned int sizeToReserve)
Get number of padding bytes needed to align with uint32_t.
Definition: TrigDataAccess/TrigSerializeResult/TrigSerializeResult/StringSerializer.h:80