ATLAS Offline Software
Loading...
Searching...
No Matches
TrigEvent/TrigNavStructure/Root/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>
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
15namespace {
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}
22namespace HLT {
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 std::string s (ostream.str());
51 const uint32_t* udata = reinterpret_cast<const uint32_t*> (s.data());
52 storage.push_back(strings.size()); // put number of strings first
53 storage.insert(storage.end(), udata, udata+sizeToReserve/sizeof(uint32_t));
54 }
55
56 void serialize (const std::string& str, std::vector<uint32_t>& storage ) {
57 std::vector<std::string> tmp(1, str);
58 return serialize(tmp, storage);
59 }
60
61
62 std::size_t deserialize (std::vector<uint32_t>::const_iterator first,
63 std::vector<uint32_t>::const_iterator last,
64 std::vector<std::string>& strings)
65 {
66 std::size_t storageSize = std::distance(first, last);
67
68 if ( storageSize <= 1 ) return storageSize;
69 unsigned int numOfStrings = *first;
70
71 std::string whole(reinterpret_cast<const char*>(&*(first+1)),
72 (storageSize-1)*sizeof(uint32_t));
73 std::istringstream istream;
74
75 istream.clear(); // istream reset
76 istream.str(whole);
77
78 std::string one;
79 unsigned int readInStrings=0;
80 std::size_t nChars=0;
81
82 while ( istream.good() && readInStrings < numOfStrings ) {
83 getline(istream, one, delimiter[0]);
84 strings.push_back(one);
85 readInStrings++;
86 nChars += one.size() + 1;
87 }
88
89 return 1 + (nChars+getPadding(nChars))/sizeof(uint32_t); // # uint32_t words (incl. header)
90 }
91
92 std::size_t deserialize (std::vector<uint32_t>::const_iterator first,
93 std::vector<uint32_t>::const_iterator last,
94 std::string& str)
95 {
96 std::vector<std::string> tmp;
97 std::size_t nWords = deserialize(first, last, tmp);
98 if (tmp.size() == 1 )
99 str = tmp[0];
100
101 return nWords;
102 }
103
104
105} // eof StringSerialzier
106} // eof HLT
STL class.
std::size_t deserialize(std::vector< uint32_t >::const_iterator first, std::vector< uint32_t >::const_iterator last, std::vector< std::string > &strings)
void serialize(const std::vector< std::string > &strings, std::vector< uint32_t > &storage)
It used to be useful piece of code for replacing actual SG with other store of similar functionality ...
const FeatureContainerInit< FEATURE, CONTAINER > RegisterFeatureContainerTypes< FEATURE, CONTAINER >::s
setEventNumber uint32_t