Jpp  17.1.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
9 #include "JLang/JAbstractIO.hh"
10 #include "JLang/JEquationFacet.hh"
11 
12 
13 /**
14  * \author mdejong
15  */
16 
17 namespace JLANG {}
18 namespace JPP { using namespace JLANG; }
19 
20 namespace 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>
36  class JValueInput :
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>
92  class JValueOutput :
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  */
111  JValueOutput(const JValueInput<T>& object) :
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
JValueOutput(const T &object)
Constructor.
Definition: JValue.hh:101
JValueInput(void *ps)
Constructor.
Definition: JValue.hh:60
Facet class to specify parsing of equations in currect locale (see class JLANG::JEquation).
Forward declaration for friend declaration of JValueOutput inside JValueInput.
Definition: JValue.hh:27
Interface for ASCII input using standard streams.
Definition: JAbstractIO.hh:21
JValue(T &object)
Constructor.
Definition: JValue.hh:161
Wrapper class around template object.
Definition: JValue.hh:36
JValue(void *ps)
Constructor.
Definition: JValue.hh:172
is
Definition: JDAQCHSM.chsm:167
JValueOutput(const void *ps)
Constructor.
Definition: JValue.hh:121
do set_variable OUTPUT_DIRECTORY $WORKDIR T
std::istream & getline(std::istream &in, std::string &buffer) const
Read characters until next end of line.
JValueOutput(const JValueInput< T > &object)
Constructor.
Definition: JValue.hh:111
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1737
Interface for ASCII output using standard streams.
Definition: JAbstractIO.hh:56
Wrapper class around template object.
Definition: JValue.hh:151
char * loc(char *orig)
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 JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
virtual JEquationFacet * clone() const override
Clone this facet.
JValueInput(T &object)
Constructor.
Definition: JValue.hh:50
virtual std::ostream & write(std::ostream &out) const override
Stream output.
Definition: JValue.hh:135
virtual std::istream & read(std::istream &in) override
Stream input.
Definition: JValue.hh:75