ATLAS Offline Software
Loading...
Searching...
No Matches
TrigDataAccess/TrigSerializeResult/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>
7const char* const StringSerializer::s_delimiter = "\n";
8
9
10void StringSerializer::serialize (const std::vector<std::string>& strings, std::vector<uint32_t>& storage ) {
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 std::string s (m_ostream.str());
37 const uint32_t * uarray = reinterpret_cast<const uint32_t*> (s.data());
38
39 storage.push_back(strings.size()); // put number of strings first
40 storage.insert(storage.end(), &uarray[0], &uarray[sizeToReserve/sizeof(uint32_t)]);
41
42 /*
43 std::cerr << "reserved = " << sizeToReserve << ", pad = " << sizeToPad
44 << ", storage size = " << storage.size() << ", serialized: " << std::endl;
45
46 for ( unsigned int i = 0 ; i < sizeToReserve; ++i ) {
47 std::cerr << " " << std::hex << (int)carray[i] << std::dec << "(" << (char)carray[i] << ")";
48 }
49 std::cerr << " " << std::dec << std::endl;
50 */
51
52}
53
54void StringSerializer::serialize (const std::string& str, std::vector<uint32_t>& storage ) {
55 std::vector<std::string> tmp(1, str);
56 return serialize(tmp, storage);
57}
58
59std::size_t StringSerializer::deserialize (std::vector<uint32_t>::const_iterator first,
60 std::vector<uint32_t>::const_iterator last,
61 std::vector<std::string>& strings)
62{
63 std::size_t storageSize = std::distance(first, last);
64
65 if ( storageSize <= 1 ) return storageSize;
66 unsigned int numOfStrings = *first;
67
68 /*
69 unsigned int i;
70 std::cerr << "deserialized: " << std::endl;
71 for ( i = 0 ; i < storage.size()-1; ++i ) {
72 std::cerr << " " << std::hex << uarray[i] ;
73 }
74 std::cerr << " " << std::dec << std::endl;
75 */
76
77 std::string whole(reinterpret_cast<const char*>(&*(first+1)),
78 (storageSize-1)*sizeof(uint32_t));
79
80 m_istream.clear(); // istream reset
81 m_istream.str(whole);
82
83 std::string one;
84 unsigned int readInStrings=0;
85 std::size_t nChars=0;
86
87 while ( m_istream.good() && readInStrings < numOfStrings ) {
88 getline(m_istream, one, s_delimiter[0]);
89 strings.push_back(one);
90 readInStrings++;
91 nChars += one.size() + 1;
92 }
93
94 return 1 + (nChars+getPadding(nChars))/sizeof(uint32_t); // # uint32_t words (incl. header)
95}
96
97std::size_t StringSerializer::deserialize (std::vector<uint32_t>::const_iterator first,
98 std::vector<uint32_t>::const_iterator last,
99 std::string& str)
100{
101 std::vector<std::string> tmp;
102 std::size_t nWords = deserialize(first, last, tmp);
103 if (tmp.size() == 1 )
104 str = tmp[0];
105
106 return nWords;
107}
108
109unsigned int StringSerializer::inquireSize(const std::vector<uint32_t>& storage) {
110 if ( storage.size() <= 1 )
111 return 0;
112 return storage[0];
113}
114
static const char *const s_delimiter
default delimeter which is put between strings from the input vector while serialization happens
void serialize(const std::vector< std::string > &strings, std::vector< uint32_t > &storage)
Serializes vector of strings into vector of integers.
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.
unsigned int inquireSize(const std::vector< uint32_t > &storage)
Return number of strings serialized into 'storage'.
std::istringstream m_istream
sstream used in de-serialization
unsigned int getPadding(unsigned int sizeToReserve)
Get number of padding bytes needed to align with uint32_t.