Jpp  19.1.0-rc.1
the software that should make you happy
JObjectIO.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JOBJECTIO__
2 #define __JLANG__JOBJECTIO__
3 
4 #include <string>
5 #include <fstream>
6 
7 #include "JLang/JType.hh"
8 #include "JLang/JException.hh"
9 
10 
11 /**
12  * \file
13  *
14  * General methods for loading and storing a single object from and to a file, respectively.
15  * \author mdejong
16  */
17 namespace JLANG {}
18 namespace JPP { using namespace JLANG; }
19 
20 
21 namespace JLANG {
22 
23  /**
24  * Get error status of reader.
25  *
26  * \param reader reader
27  * \return true if error; else false
28  */
29  template<class JReader_t>
30  inline bool getError(const JReader_t& reader)
31  {
32  return !reader;
33  }
34 
35 
36  /**
37  * Get error status of reader.
38  *
39  * \param reader reader
40  * \return true if error; else false
41  */
42  inline bool getError(const std::ifstream& reader)
43  {
44  return reader.bad() || (reader.fail() && !reader.eof());
45  }
46 
47 
48  /**
49  * Load object from input file.
50  *
51  * \param file_name file name
52  * \param object object to be read
53  */
54  template<class JReader_t, class T>
55  inline void load(const std::string& file_name, T& object)
56  {
57  load(file_name, object, JType<JReader_t>());
58  }
59 
60 
61  /**
62  * Store object to output file.
63  *
64  * \param file_name file name
65  * \param object object to be written
66  */
67  template<class JWriter_t, class T>
68  inline void store(const std::string& file_name, const T& object)
69  {
70  store(file_name, object, JType<JWriter_t>());
71  }
72 
73 
74  /**
75  * Load object from input file.
76  *
77  * This method makes use of the STD protocol for the given type reader, namely:
78  * <pre>
79  * JReader_t in(file_name);
80  *
81  * in >> object;
82  *
83  * in.close();
84  * </pre>
85  * This method should be overloaded if the type reader requires a different protocol.
86  *
87  * \param file_name file name
88  * \param object object to be read
89  * \param type reader type
90  */
91  template<class JReader_t, class T>
92  inline void load(const std::string& file_name, T& object, JType<JReader_t> type)
93  {
94  JReader_t in(file_name.c_str());
95 
96  if (!in) {
97  THROW(JFileOpenException, "Error opening file: " << file_name);
98  }
99 
100  in >> object;
101 
102  if (getError(in)) {
103  THROW(JFileReadException, "Error reading file: " << file_name);
104  }
105 
106  in.close();
107  }
108 
109 
110  /**
111  * Store object to output file.
112  *
113  * This method makes use of the STD protocol for the given type writer, namely:
114  * <pre>
115  * JWriter_t out(file_name);
116  *
117  * out << object;
118  *
119  * out.close();
120  * </pre>
121  * This method should be overloaded if the type writer requires a different protocol.
122  *
123  * \param file_name file name
124  * \param object object to be written
125  * \param type writer type
126  */
127  template<class JWriter_t, class T>
128  inline void store(const std::string& file_name, const T& object, JType<JWriter_t> type)
129  {
130  JWriter_t out(file_name.c_str());
131 
132  if (!out) {
133  THROW(JFileOpenException, "Error opening file: " << file_name);
134  }
135 
136  out << object;
137 
138  out.close();
139  }
140 
141 
142  /**
143  * Load object from input file.
144  *
145  * \param file_name file name
146  * \param object object to be read
147  */
148  template<class T>
149  inline void load(const std::string& file_name, T& object)
150  {
151  load(file_name, object, JType<std::ifstream>());
152  }
153 
154 
155  /**
156  * Store object to output file.
157  *
158  * \param file_name file name
159  * \param object object to be written
160  */
161  template<class T>
162  inline void store(const std::string& file_name, const T& object)
163  {
164  store(file_name, object, JType<std::ofstream>());
165  }
166 }
167 
168 #endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Exception for opening of file.
Definition: JException.hh:360
Exception for reading of file.
Definition: JException.hh:396
Auxiliary classes and methods for language specific functionality.
void load(const std::string &file_name, T &object)
Load object from input file.
Definition: JObjectIO.hh:149
bool getError(const std::ifstream &reader)
Get error status of reader.
Definition: JObjectIO.hh:42
void store(const std::string &file_name, const T &object)
Store object to output file.
Definition: JObjectIO.hh:162
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary class for a type holder.
Definition: JType.hh:19