Jpp  master_rocky-40-g5f0272dcd
the software that should make you happy
JIOToolkit.hh
Go to the documentation of this file.
1 #ifndef __JIO__JIOTOOLKIT__
2 #define __JIO__JIOTOOLKIT__
3 
4 #include "JLang/JException.hh"
5 #include "JIO/JFileStreamIO.hh"
6 #include "JIO/JSTDIO.hh"
7 
8 
9 /**
10  * \file
11  * Auxiliary methods for binary I/O.
12  * \author mdejong
13  */
14 namespace JIO {}
15 namespace JPP { using namespace JIO; }
16 
17 namespace JIO {
18 
21 
22 
23  /**
24  * Load object from input file.
25  *
26  * \param file_name file name
27  * \param object object to be read
28  */
29  template<class T>
30  inline void load(const char* file_name, T& object)
31  {
32  JFileStreamReader in(file_name);
33 
34  if (!in) {
35  THROW(JFileOpenException, "Error opening file " << file_name);
36  }
37 
38  in.load(object);
39 
40  if (!in) {
41  THROW(JFileReadException, "Error reading file " << file_name);
42  }
43 
44  in.close();
45  }
46 
47 
48  /**
49  * Store object to output file.
50  *
51  * \param file_name file name
52  * \param object object to be written
53  */
54  template<class T>
55  inline void store(const char* file_name, const T& object)
56  {
57  JFileStreamWriter out(file_name);
58 
59  if (!out) {
60  THROW(JFileOpenException, "Error opening file " << file_name);
61  }
62 
63  out.store(object);
64  out.close();
65  }
66 }
67 
68 #endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
STD extensions for binary I/O.
Binary buffered file input.
Binary buffered file output.
void close()
Close file.
JReader & load(JSerialisable &object)
Read object.
JWriter & store(const JSerialisable &object)
Write object.
Exception for opening of file.
Definition: JException.hh:360
Exception for reading of file.
Definition: JException.hh:396
Auxiliary classes and methods for binary I/O.
void load(const char *file_name, T &object)
Load object from input file.
Definition: JIOToolkit.hh:30
void store(const char *file_name, const T &object)
Store object to output file.
Definition: JIOToolkit.hh:55
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).