Jpp 21.0.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JDriver.hh
Go to the documentation of this file.
1#ifndef __JTOOLS__JDRIVER__
2#define __JTOOLS__JDRIVER__
3
4#include <istream>
5#include <ostream>
6#include <type_traits>
7
9
10
11/**
12 * \author mdejong
13 */
14
15namespace JTOOLS {}
16namespace JPP { using namespace JTOOLS; }
17
18namespace JTOOLS {
19
20
21
22 /**
23 * Auxiliary class to load and store objects.
24 */
25 template<class JClass_t>
26 class JDriver {
27 private:
28 /**
29 * Load object without input.
30 *
31 * \param option I/O option
32 * \return pointer to newly loaded object
33 */
34 static inline JClass_t* load(std::false_type option)
35 {
36 return new JClass_t();
37 }
38
39
40 /**
41 * Load object without input.
42 *
43 * \param in input stream
44 * \param option I/O option
45 * \return pointer to newly loaded object
46 */
47 static inline JClass_t* load(std::istream& in, std::false_type option)
48 {
49 return load(option);
50 }
51
52
53 /**
54 * Load object with input.
55 *
56 * \param in input stream
57 * \param option I/O option
58 * \return pointer to newly loaded object
59 */
60 static inline JClass_t* load(std::istream& in, std::true_type option)
61 {
62 JClass_t* p = load(std::false_type());
63
64 in >> *p;
65
66 if (in.fail()) {
67
68 delete p;
69
70 p = NULL;
71 }
72
73 return p;
74 }
75
76
77 /**
78 * Store object without output.
79 * This implementation doesn't do anything.
80 *
81 * \param out output stream
82 * \param option I/O option
83 * \param p pointer to object
84 */
85 static inline void store(std::ostream& out, const JClass_t* p, std::false_type option)
86 {}
87
88
89 /**
90 * Store object with output.
91 *
92 * \param out output stream
93 * \param option I/O option
94 * \param p pointer object
95 */
96 static inline void store(std::ostream& out, const JClass_t* p, std::true_type option)
97 {
98 if (p != NULL) {
99 out << *p;
100 }
101 }
102
103
104 public:
105 /**
106 * Load object.
107 *
108 * \return pointer to newly loaded object
109 */
110 static inline JClass_t* load()
111 {
112 return load(std::bool_constant<JStreamAvailable<JClass_t>::has_istream>());
113 }
114
115
116 /**
117 * Load object.
118 *
119 * \param in input stream
120 * \return pointer to newly loaded object
121 */
122 static inline JClass_t* load(std::istream& in)
123 {
124 return load(in, std::bool_constant<JStreamAvailable<JClass_t>::has_istream>());
125 }
126
127
128 /**
129 * Store object.
130 *
131 * \param out output stream
132 * \param p pointer to object
133 */
134 template<class T>
135 static inline void store(std::ostream& out, const T* p)
136 {
137 store(out, dynamic_cast<const JClass_t*>(p), std::bool_constant<JStreamAvailable<JClass_t>::has_ostream>());
138 }
139 };
140}
141
142#endif
Test availability of stream operators.
Auxiliary class to load and store objects.
Definition JDriver.hh:26
static JClass_t * load(std::istream &in, std::false_type option)
Load object without input.
Definition JDriver.hh:47
static void store(std::ostream &out, const JClass_t *p, std::true_type option)
Store object with output.
Definition JDriver.hh:96
static JClass_t * load(std::false_type option)
Load object without input.
Definition JDriver.hh:34
static void store(std::ostream &out, const T *p)
Store object.
Definition JDriver.hh:135
static JClass_t * load()
Load object.
Definition JDriver.hh:110
static JClass_t * load(std::istream &in, std::true_type option)
Load object with input.
Definition JDriver.hh:60
static void store(std::ostream &out, const JClass_t *p, std::false_type option)
Store object without output.
Definition JDriver.hh:85
static JClass_t * load(std::istream &in)
Load object.
Definition JDriver.hh:122
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.