Monarch  v3.8.2
Project 8 Data File Format Library
M3Monarch.hh
Go to the documentation of this file.
1 /*
2  * M3Monarch.hh
3  *
4  * Created on: Dec 4, 2014
5  * Author: nsoblath
6  */
7 
8 #ifndef M3MONARCH_HH_
9 #define M3MONARCH_HH_
10 
11 #include "M3Exception.hh"
12 #include "M3Header.hh"
13 #include "logger.hh"
14 #include "M3Stream.hh"
15 
16 #include "H5Cpp.h"
17 
18 #include <string>
19 #include <stdexcept>
20 
21 namespace monarch3
22 {
23  LOGGER( mlog_mmonarch, "M3Monarch3.h" );
24 
41  {
42  //***********************
43  // constructors and state
44  //***********************
45 
46  private:
47  //private to force use of static constructor methods
48  Monarch3();
49  Monarch3( const Monarch3& ) = delete;
50  Monarch3( Monarch3&& ) = delete;
51 
52  //current state of monarch
53  public:
54  typedef enum
55  {
56  eOpenToRead, // state when monarch has a file open but hasn't read the header
57  eOpenToWrite, // state when monarch has a file open but hasn't written the header
58  eReadyToRead, // state when monarch has dealt with the header and is reading records
59  eReadyToWrite, // state when monarch has dealt with the header and is writing records
60  eClosed // state when monarch has no file
61  } State;
62  State GetState() const;
63  private:
64  mutable State fState;
65 
66  public:
67  ~Monarch3();
68 
69  //********************************
70  // methods for reading (all const)
71  //********************************
72 
73  public:
74 
78  static const Monarch3* OpenForReading( const std::string& filename );
79 
84  void ReadHeader() const;
85 
87  const M3Header* GetHeader() const;
88 
90  const M3Stream* GetStream( unsigned stream ) const;
91 
93  void FinishReading() const;
94 
95  //*********************************
96  // methods for writing (none const)
97  //*********************************
98 
99  public:
100 
104  static Monarch3* OpenForWriting( const std::string& filename );
105 
109  void WriteHeader();
110 
112  M3Header* GetHeader();
113 
115  M3Stream* GetStream( unsigned stream );
116 
118  void FinishWriting();
119 
120  private:
121  // the HDF5 file
122  mutable H5::H5File* fFile;
123 
124  // the header
125  mutable M3Header* fHeader;
126 
127  // the streams
128  mutable std::vector< M3Stream* > fStreams;
129 
130  // stream read/write mutex
132 
133  };
134 
135  inline const M3Header* Monarch3::GetHeader() const
136  {
137  return fHeader;
138  }
140  {
141  return fHeader;
142  }
143 
144  inline const M3Stream* Monarch3::GetStream( unsigned iStream ) const
145  {
146  try
147  {
148  return fStreams.at( iStream );
149  }
150  catch( std::out_of_range& e )
151  {
152  return nullptr;
153  }
154  }
155  inline M3Stream* Monarch3::GetStream( unsigned iStream )
156  {
157  try
158  {
159  return fStreams.at( iStream );
160  }
161  catch( std::out_of_range& e )
162  {
163  return nullptr;
164  }
165  }
166 
167 }
168 
169 #endif /* M3MONARCH_HH_ */
M3Header * fHeader
Definition: M3Monarch.hh:125
const M3Stream * GetStream(unsigned stream) const
Get the pointer to a particular stream.
Definition: M3Monarch.hh:144
Egg file header information.
Definition: M3Header.hh:152
static scarab::logger mlog_mmonarch("M3Monarch3.h")
Read/write access for a data stream.
Definition: M3Stream.hh:41
mutex_ptr fMutexPtr
Definition: M3Monarch.hh:131
H5::H5File * fFile
Definition: M3Monarch.hh:122
const M3Header * GetHeader() const
Get the pointer to the header.
Definition: M3Monarch.hh:135
std::vector< M3Stream *> fStreams
Definition: M3Monarch.hh:128
#define M3_API
Definition: M3Constants.hh:21
Egg file read/write access.
Definition: M3Monarch.hh:40
std::shared_ptr< std::mutex > mutex_ptr
Definition: M3Types.hh:28