FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BoostDataSaver.h
Go to the documentation of this file.
1 /*
2  * File: BoostDataSaver.h
3  * Author: winckler
4  *
5  * Created on October 22, 2014, 5:16 PM
6  */
7 
8 #ifndef BOOSTDATASAVER_H
9 #define BOOSTDATASAVER_H
10 
11 // std
12 #include <fstream>
13 #include <iostream>
14 #include <string>
15 #include <type_traits>
16 #include <vector>
17 
18 // boost
19 namespace boost {
20 namespace serialization {
21 class access;
22 }
23 } // namespace boost
24 #include <boost/archive/binary_iarchive.hpp>
25 #include <boost/archive/binary_oarchive.hpp>
26 #include <boost/serialization/vector.hpp>
27 
28 // FairRoot
29 #include <FairMQMessage.h>
30 
31 template<typename TPayload,
32  typename TArchiveIn = boost::archive::binary_iarchive,
33  typename TArchiveOut = TBoboost::archive::binary_oarchiveostOut>
35 {
36  public:
38  virtual ~BoostDataSaver() {}
39 
40  virtual void InitOutputFile() {}
41 
42  void Write(std::ofstream& outfile, TPayload* objArr, long sizeArr = 1)
43  {
44  std::vector<TPayload> dataVector(objArr, objArr + sizeArr);
45  TArchiveOut outArchive(outfile);
46  outArchive << dataVector;
47  }
48 
49  void Write(std::ofstream& outfile, std::vector<TPayload> dataVector)
50  {
51  TArchiveOut outArchive(outfile);
52  outArchive << dataVector;
53  }
54 
55  void Write(std::ofstream& outfile, FairMQMessage* msg)
56  {
57  std::vector<TPayload> objArr;
58  std::string msgStr(static_cast<char*>(msg->GetData()), msg->GetSize());
59  std::istringstream ibuffer(msgStr);
60  TArchiveIn inputArchive(ibuffer);
61  try {
62  inputArchive >> objArr;
63  } catch (boost::archive::archive_exception& e) {
64  LOG(error) << e.what();
65  }
66  TArchiveOut outArchive(outfile);
67  outArchive << objArr;
68  }
69 
70  std::vector<std::vector<TPayload>> Read(std::ifstream& infile)
71  {
72  std::vector<std::vector<TPayload>> dataContainer;
73  while (infile.peek() != EOF && !infile.eof()) {
74  TArchiveIn inArchive(infile);
75  std::vector<TPayload> temp;
76  inArchive >> temp;
77  dataContainer.push_back(temp);
78  }
79  return dataContainer;
80  }
81 };
82 
83 #endif /* BOOSTDATASAVER_H */
std::vector< std::vector< TPayload > > Read(std::ifstream &infile)
void Write(std::ofstream &outfile, FairMQMessage *msg)
virtual ~BoostDataSaver()
virtual void InitOutputFile()
void Write(std::ofstream &outfile, TPayload *objArr, long sizeArr=1)
void Write(std::ofstream &outfile, std::vector< TPayload > dataVector)