Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JValue.hh
Go to the documentation of this file.
1#ifndef __JLANG__JVALUE__
2#define __JLANG__JVALUE__
3
4#include <istream>
5#include <ostream>
6#include <sstream>
7#include <string>
8
11
12
13/**
14 * \author mdejong
15 */
16
17namespace JLANG {}
18namespace JPP { using namespace JLANG; }
19
20namespace JLANG {
21
22
23 /**
24 * Forward declaration for friend declaration of JValueOutput inside JValueInput.
25 */
26 template<class T>
27 class JValueOutput;
28
29
30 /**
31 * Wrapper class around template object.
32 * This class implements the JStreamInput interface.
33 * Note that this class can be used in conjuction with the JEquationFacet.
34 */
35 template<class T>
37 public JStreamInput
38 {
39 public:
40
41
42 friend class JValueOutput<T>;
43
44
45 /**
46 * Constructor.
47 *
48 * \param object input object
49 */
50 JValueInput(T& object) :
51 p(&object)
52 {}
53
54
55 /**
56 * Constructor.
57 *
58 * \param ps pointer to valid object
59 */
60 JValueInput(void* ps) :
61 p((T*) ps)
62 {}
63
64
65 operator const T&() const { return *p; } //!< type conversion operator
66 operator T&() { return *p; } //!< type conversion operator
67
68
69 /**
70 * Stream input.
71 *
72 * \param in input stream
73 * \return input stream
74 */
75 virtual std::istream& read(std::istream& in) override
76 {
77 return in >> *p;
78 }
79
80
81 protected:
82 T* p;
83 };
84
85
86 /**
87 * Wrapper class around template object.
88 * This class implements the JStreamOutput interface.
89 * Note that this class can be used in conjuction with the JEquationFacet.
90 */
91 template<class T>
93 public JStreamOutput
94 {
95 public:
96 /**
97 * Constructor.
98 *
99 * \param object input object
100 */
101 JValueOutput(const T& object) :
102 p(&object)
103 {}
104
105
106 /**
107 * Constructor.
108 *
109 * \param object input object
110 */
112 p(object.p)
113 {}
114
115
116 /**
117 * Constructor.
118 *
119 * \param ps pointer to valid object
120 */
121 JValueOutput(const void* ps) :
122 p((const T*) ps)
123 {}
124
125
126 operator const T&() const { return *p; } //!< type conversion operator
127
128
129 /**
130 * Stream output.
131 *
132 * \param out output stream
133 * \return output stream
134 */
135 virtual std::ostream& write(std::ostream& out) const override
136 {
137 return out << *p;
138 }
139
140
141 protected:
142 const T* p;
143 };
144
145
146 /**
147 * Wrapper class around template object.
148 * This class implements the JStreamInput and JStreamOutput interfaces.
149 */
150 template<class T>
151 class JValue :
152 public JValueInput <T>,
153 public JValueOutput<T>
154 {
155 public:
156 /**
157 * Constructor.
158 *
159 * \param object input object
160 */
161 JValue(T& object) :
162 JValueInput <T>(&object),
163 JValueOutput<T>(&object)
164 {}
165
166
167 /**
168 * Constructor.
169 *
170 * \param ps pointer to valid object
171 */
172 JValue(void* ps) :
173 JValueInput <T>(ps) ,
174 JValueOutput<T>(ps)
175 {}
176 };
177
178
179 /**
180 * Read JStreamInput from input stream.
181 *
182 * \param in input stream
183 * \param object JStreamInput
184 * \return input stream
185 */
186 template<class T>
187 inline std::istream& operator>>(std::istream& in, JValueInput<T>& object)
188 {
189 using namespace std;
190
191 istream::sentry sentry(in); // skips white spaces
192
193 if (sentry) {
194
195 const locale& loc = in.getloc();
196
197 if (has_facet<JEquationFacet>(loc)) {
198
199 string buffer;
200
201 const JEquationFacet& facet = use_facet<JEquationFacet>(loc);
202
203 facet.getline(in, buffer);
204
205 istringstream is(buffer);
206
207 is.imbue(locale(in.getloc(), facet.clone()));
208
209 object.read(is);
210
211 if (is.fail()) {
212 in.setstate(ios_base::badbit);
213 }
214
215 } else {
216
217 object.read(in);
218 }
219 }
220
221 return in;
222 }
223
224
225 /**
226 * Write JStreamOutput to output stream.
227 *
228 * \param out output stream
229 * \param object JStreamOutput
230 * \return output stream
231 */
232 template<class T>
233 inline std::ostream& operator<<(std::ostream& out, const JValueOutput<T>& object)
234 {
235 return object.write(out);
236 }
237}
238
239#endif
Facet class to specify parsing of equations in currect locale (see class JLANG::JEquation).
virtual JEquationFacet * clone() const override
Clone this facet.
Interface for ASCII input using standard streams.
Interface for ASCII output using standard streams.
std::istream & getline(std::istream &in, std::string &buffer) const
Read characters until next end of line.
Wrapper class around template object.
Definition JValue.hh:38
virtual std::istream & read(std::istream &in) override
Stream input.
Definition JValue.hh:75
JValueInput(T &object)
Constructor.
Definition JValue.hh:50
JValueInput(void *ps)
Constructor.
Definition JValue.hh:60
Forward declaration for friend declaration of JValueOutput inside JValueInput.
Definition JValue.hh:94
JValueOutput(const JValueInput< T > &object)
Constructor.
Definition JValue.hh:111
virtual std::ostream & write(std::ostream &out) const override
Stream output.
Definition JValue.hh:135
JValueOutput(const T &object)
Constructor.
Definition JValue.hh:101
JValueOutput(const void *ps)
Constructor.
Definition JValue.hh:121
Wrapper class around template object.
Definition JValue.hh:154
JValue(T &object)
Constructor.
Definition JValue.hh:161
JValue(void *ps)
Constructor.
Definition JValue.hh:172
char * loc(char *orig)
Auxiliary classes and methods for language specific functionality.
std::ostream & operator<<(std::ostream &out, const JLANG::JEquationFacet &facet)
Parse facet.
std::istream & operator>>(std::istream &in, const JLANG::JEquationFacet &facet)
Parse facet.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).