Jpp  pmt_effective_area_update_2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JObjectIO.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JOBJECTIO__
2 #define __JLANG__JOBJECTIO__
3 
4 #include <fstream>
5 
6 #include "JLang/JType.hh"
7 #include "JLang/JException.hh"
8 
9 
10 /**
11  * \file
12  *
13  * General methods for loading and storing a single object from and to a file, respectively.
14  * \author mdejong
15  */
16 namespace JLANG {}
17 namespace JPP { using namespace JLANG; }
18 
19 
20 namespace JLANG {
21 
22  /**
23  * Get error status of reader.
24  *
25  * \param reader reader
26  * \return true if error; else false
27  */
28  template<class JReader_t>
29  inline bool getError(const JReader_t& reader)
30  {
31  return !reader;
32  }
33 
34 
35  /**
36  * Get error status of reader.
37  *
38  * \param reader reader
39  * \return true if error; else false
40  */
41  inline bool getError(const std::ifstream& reader)
42  {
43  return reader.bad() || (reader.fail() && !reader.eof());
44  }
45 
46 
47  /**
48  * Load object from input file.
49  *
50  * \param file_name file name
51  * \param object object to be read
52  */
53  template<class JReader_t, class T>
54  inline void load(const char* file_name, T& object)
55  {
56  load(file_name, object, JType<JReader_t>());
57  }
58 
59 
60  /**
61  * Store object to output file.
62  *
63  * \param file_name file name
64  * \param object object to be written
65  */
66  template<class JWriter_t, class T>
67  inline void store(const char* file_name, const T& object)
68  {
69  store(file_name, object, JType<JWriter_t>());
70  }
71 
72 
73  /**
74  * Load object from input file.
75  *
76  * This method makes use of the STD protocol for the given type reader, namely:
77  * <pre>
78  * JReader_t in(file_name);
79  *
80  * in >> object;
81  *
82  * in.close();
83  * </pre>
84  * This method should be overloaded if the type reader requires a different protocol.
85  *
86  * \param file_name file name
87  * \param object object to be read
88  * \param type reader type
89  */
90  template<class JReader_t, class T>
91  inline void load(const char* file_name, T& object, JType<JReader_t> type)
92  {
93  JReader_t in(file_name);
94 
95  if (!in) {
96  THROW(JFileOpenException, "Error opening file: " << file_name);
97  }
98 
99  in >> object;
100 
101  if (getError(in)) {
102  THROW(JFileReadException, "Error reading file: " << file_name);
103  }
104 
105  in.close();
106  }
107 
108 
109  /**
110  * Store object to output file.
111  *
112  * This method makes use of the STD protocol for the given type writer, namely:
113  * <pre>
114  * JWriter_t out(file_name);
115  *
116  * out << object;
117  *
118  * out.close();
119  * </pre>
120  * This method should be overloaded if the type writer requires a different protocol.
121  *
122  * \param file_name file name
123  * \param object object to be written
124  * \param type writer type
125  */
126  template<class JWriter_t, class T>
127  inline void store(const char* file_name, const T& object, JType<JWriter_t> type)
128  {
129  JWriter_t out(file_name);
130 
131  if (!out) {
132  THROW(JFileOpenException, "Error opening file: " << file_name);
133  }
134 
135  out << object;
136 
137  out.close();
138  }
139 }
140 
141 #endif
Exception for opening of file.
Definition: JException.hh:342
Exception for reading of file.
Definition: JException.hh:360
Exceptions.
bool getError(const JReader_t &reader)
Get error status of reader.
Definition: JObjectIO.hh:29
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
Auxiliary class for a type holder.
Definition: JType.hh:19
do set_variable OUTPUT_DIRECTORY $WORKDIR T
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:40