FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BinaryOutFileManager.h
Go to the documentation of this file.
1 /*
2  * File: BinaryOutFileManager.h
3  * Author: winckler
4  *
5  * Created on October 15, 2014, 11:36 AM
6  */
7 
8 #ifndef BINARYOUTFILEMANAGER_H
9 #define BINARYOUTFILEMANAGER_H
10 
11 // std
12 #include <fstream>
13 #include <string>
14 #include <type_traits>
15 #include <vector>
16 
17 // boost
18 namespace boost {
19 namespace serialization {
20 class access;
21 }
22 } // namespace boost
23 #include <boost/archive/binary_iarchive.hpp>
24 #include <boost/archive/binary_oarchive.hpp>
25 #include <boost/serialization/vector.hpp>
26 
27 // FairRoot
28 #include "BoostDataSaver.h"
30 #include "baseMQtools.h"
31 
32 #include <FairMQLogger.h>
33 #include <FairMQMessage.h>
34 
35 #define GET_POLICY_ID(Policy) #Policy
36 
37 using BinArchive = boost::archive::binary_oarchive;
38 
39 template<typename T, typename BoostArchive>
40 using BoostSerializable = baseMQ::tools::resolve::has_BoostSerialization<T, void(BoostArchive&, const unsigned int)>;
41 
42 template<typename TPayload, typename TStoragePolicy = TriviallyCopyableDataSaver<TPayload>>
43 class BinaryOutFileManager : public TStoragePolicy
44 {
45  public:
46  using TStoragePolicy::Read;
47  using TStoragePolicy::Write;
48 
50  : TStoragePolicy()
51  , fFileName("test.dat")
52  {
53  ;
54  }
55  virtual ~BinaryOutFileManager() { fOutfile.close(); }
56 
57  std::string GetPolicyID()
58  {
59  std::string str(GET_POLICY_ID(TStoragePolicy));
60  return str;
61  }
62 
63  void SetFileProperties(const std::string& filename) { fFileName = filename; }
64 
65  void AddToFile(FairMQMessage* msg) { TStoragePolicy::Write(fOutfile, msg); }
66 
67  void AddToFile(TPayload* objArr, long size) { AppendObjArray<TPayload>(fOutfile, objArr, size); }
68 
69  template<typename T>
70  void AppendObjArray(std::ofstream& outfile, T* objArr, long size)
71  {
72  TStoragePolicy::Write(outfile, objArr, size);
73  }
74 
75  std::vector<std::vector<TPayload>> GetAllObj(const std::string& filename)
76  {
77  std::ifstream infile;
78  infile = std::ifstream(filename);
79  std::vector<std::vector<TPayload>> vec = TStoragePolicy::Read(infile);
80 
81  return vec;
82  }
83 
84  virtual void InitOutputFile()
85  {
86  fOutfile = std::ofstream(fFileName, std::ios::out | std::ios::binary | std::ios::app);
87  }
88 
89  protected:
90  std::string fFileName;
91  std::ofstream fOutfile;
92 };
93 
94 #endif /* BINARYOUTFILEMANAGER_H */
boost::archive::binary_oarchive BinArchive
baseMQ::tools::resolve::has_BoostSerialization< T, void(BoostArchive &, const unsigned int)> BoostSerializable
void AppendObjArray(std::ofstream &outfile, T *objArr, long size)
void SetFileProperties(const std::string &filename)
void AddToFile(TPayload *objArr, long size)
std::vector< std::vector< TPayload > > GetAllObj(const std::string &filename)
void AddToFile(FairMQMessage *msg)
virtual void InitOutputFile()
#define GET_POLICY_ID(Policy)