8 #ifndef TRIVIALLYCOPYABLEDATASAVER_H
9 #define TRIVIALLYCOPYABLEDATASAVER_H
11 #include <FairMQMessage.h>
13 template<
typename TPayload>
22 void Write(std::ofstream& outfile, TPayload* ObjArr,
long sizeArr = 1)
25 if (std::is_trivial<TPayload>::value) {
26 write_pod_array<TPayload>(outfile, ObjArr, sizeArr);
31 void Write(std::ofstream& outfile, FairMQMessage* msg)
33 int inputSize = msg->GetSize();
36 sizeArr = inputSize /
sizeof(TPayload);
38 TPayload* ObjArr =
static_cast<TPayload*
>(msg->GetData());
40 if (std::is_trivial<TPayload>::value) {
41 write_pod_array<TPayload>(outfile, ObjArr, sizeArr);
46 std::vector<std::vector<TPayload>>
Read(std::ifstream& infile)
48 std::vector<std::vector<TPayload>> DataContainer;
53 void ReadArr(std::ifstream& infile, std::vector<std::vector<TPayload>>& DataContainer)
56 if (std::is_trivial<TPayload>::value) {
57 int c = infile.peek();
64 read_pod<long>(infile, sizeArr);
65 TPayload* ObjArr =
new TPayload[sizeArr];
66 infile.read(reinterpret_cast<char*>(ObjArr), sizeArr *
sizeof(TPayload));
67 std::vector<TPayload> DataVector(ObjArr, ObjArr + sizeArr);
69 DataContainer.push_back(DataVector);
73 LOG(error) <<
"In ReadArr(std::ifstream& , std::vector<std::vector<TPayload> >& ) :";
75 <<
"(de)serialization of object is not supported (Object must be a 'trivially copyable' data class).";
80 void ReadArr(std::ifstream& infile, T* ObjArr,
long posArr = 0)
83 if (std::is_trivial<TPayload>::value) {
84 int c = infile.peek();
90 posArr += read_pod_array<T>(infile, ObjArr, posArr);
91 ReadArr<T>(infile, ObjArr, posArr);
94 LOG(error) <<
"In ReadArr(std::ifstream& infile, T* ObjArr, long posArr = 0):";
96 <<
"(de)serialization of object is not supported (Object must be a 'trivially copyable' data class).";
103 out.write(reinterpret_cast<char*>(&t),
sizeof(T));
109 in.read(reinterpret_cast<char*>(&t),
sizeof(T));
115 long size = vect.size();
116 write_pod<long>(out, size);
117 out.write(reinterpret_cast<char*>(vect.front()), size *
sizeof(T));
126 in.read(reinterpret_cast<char*>(vect.front()), size *
sizeof(T));
132 write_pod<long>(out, size);
133 out.write(reinterpret_cast<char*>(objarr), size *
sizeof(T));
141 if (std::is_trivial<TPayload>::value) {
149 read_pod<long>(in, size);
151 in.seekg(size *
sizeof(T), std::ios::cur);
152 count += count_podObj_inFile<T>(in);
162 read_pod<long>(in, size);
163 in.read(reinterpret_cast<char*>(objarr + posArr), size *
sizeof(T));
TriviallyCopyableDataSaver()
void read_pod(std::ifstream &in, T &t)
std::vector< std::vector< TPayload > > Read(std::ifstream &infile)
void write_pod_vector(std::ofstream &out, std::vector< T > &vect)
virtual ~TriviallyCopyableDataSaver()
void Write(std::ofstream &outfile, TPayload *ObjArr, long sizeArr=1)
virtual void InitOutputFile()
void ReadArr(std::ifstream &infile, std::vector< std::vector< TPayload >> &DataContainer)
void write_pod_array(std::ofstream &out, T *objarr, long size)
void Write(std::ofstream &outfile, FairMQMessage *msg)
void ReadArr(std::ifstream &infile, T *ObjArr, long posArr=0)
int count_podObj_inFile(std::ifstream &in)
void write_pod(std::ofstream &out, T &t)
void read_pod_vector(std::ifstream &in, std::vector< T > &vect)
long read_pod_array(std::ifstream &in, T *objarr, long posArr=0)