ClockWork DB CoreAPI 1.0.48
Abstract Time Series and Storage/Management Library
Loading...
Searching...
No Matches
scalar.hpp
1#ifndef HAVE_TOM__SCALAR_V2_HPP
2#define HAVE_TOM__SCALAR_V2_HPP
3
4#include <tom-util/observation.hpp>
5#include <cstdio>
6
7namespace tom {
8
9// This will use POD types (and tom::string which is just a std::string)
10// and the lower level tom::value_types::scalar<> struct to handle missing
11// data, conversions, etc.
12template <typename T>
13class scalar : public observation,
14 public boost::arithmetic< scalar<T>,
15 boost::arithmetic< scalar<T>, T,
16 boost::incrementable< scalar<T>,
17 boost::decrementable< scalar<T> > > >
18 >
19{
20public:
21
22 typedef T value_type;
27
28 // Constructors
29 scalar();
30 scalar( const T & );
32 scalar( const observation & );
33 ~scalar();
34
35 // Copy Constructors
36 template <typename Tp>
37 scalar( const scalar<Tp> & );
38 scalar( const scalar & );
39
40 virtual
41 scalar &
42 clone() const;
43
44 // Assignment Operators
45
46 // inherited from observation...don't forget it!
47 virtual
49 operator=( const observation & );
50
51
52 // class assignment ops
53 template <typename Tp>
54 scalar &operator=( const scalar<Tp> & );
55 scalar &operator=( const scalar & );
56 scalar &operator=( const T & );
57 scalar &operator=( tom::value_type t );
58
59 bool is_normal() const;
60
61 // now implement the inherited interface
62 virtual std::ostream &print( std::ostream & ) const;
63 virtual tom::value_type flag() const;
64
65 virtual observation &operator+=( const observation & );
66 virtual observation &operator-=( const observation & );
67 virtual observation &operator*=( const observation & );
68 virtual observation &operator/=( const observation & );
69
70 virtual bool operator<( const observation & ) const;
71 virtual bool operator<=( const observation & ) const;
72 virtual bool operator>( const observation & ) const;
73 virtual bool operator>=( const observation & ) const;
74
75 virtual bool operator==( const observation & ) const;
76 virtual bool operator!=( const observation & ) const;
77 virtual bool operator==( const scalar & rhs ) const { return this->value() == rhs.value(); }
78 virtual bool operator!=( const scalar & rhs ) const { return ! ( *this == rhs ); }
79
80 // cast operators
81 virtual operator unsigned char() const;
82 virtual operator tom::value_types::string() const;
83 virtual operator short int() const;
84 virtual operator int() const;
85 virtual operator long int() const;
86 virtual operator unsigned short int() const;
87 virtual operator unsigned int() const;
88 virtual operator unsigned long int() const;
89 virtual operator float() const;
90 virtual operator double() const;
91
92 virtual operator bool() const;
93
94 scalar & operator+=( const T & );
95 scalar & operator+=( const scalar & );
96
97 scalar & operator-=( const T & );
98 scalar & operator-=( const scalar & );
99
100 scalar & operator*=( const T & );
101 scalar & operator*=( const scalar & );
102
103 scalar & operator/=( const T & );
104 scalar & operator/=( const scalar & );
105
106 scalar &operator++(){ ++m_value; return *this; }
107 scalar &operator--(){ --m_value; return *this; }
108
109 static const scalar HAS_NO_DATA;
110 static const scalar HOLIDAY;
111 static const scalar NON_CALC;
112
113 const T &
114 value() const { return m_value.value_; }
115
116protected:
117 scalar_type m_value; // the value
118
119};
120
121template <typename T>
122std::ostream &
123operator<<( std::ostream &os, const scalar<T> & rhs );
124
125typedef scalar<float> Float;
126typedef scalar<double> Double;
129typedef scalar<int> Int;
131typedef scalar<long> Long;
133
134namespace {
135
136 // helper functions
137 template <typename T>
138 T
139 cast_observation_( const observation &v )
140 {
141 // this works due to the class operators
142 return static_cast<T>( v );
143 }
144
145} // end anonymous namespace
146
147// Constructors scalar();
148
149template <typename T>
150scalar<T>::scalar( ){ }
151
152template <typename T>
153scalar<T>::scalar( const T &t )
154{
155 m_value = t;
156}
157template <typename T>
158scalar<T>::scalar( tom::value_type vt) : m_value( vt ) { }
159
160template <typename T>
161scalar<T>::~scalar(){ }
162
163
164// Copy Constructors
165template <typename T>
166template <typename Tp>
167scalar<T>::scalar( const scalar<Tp> &rhs )
168{
169 m_value = cast_observation_<T> ( rhs );
170}
171template <typename T>
172scalar<T>::scalar( const scalar &rhs )
173{
174 m_value = rhs.m_value;
175}
176template <typename T>
177scalar<T>::scalar( const observation & rhs )
178{
179 m_value = cast_observation_<T>( rhs );
180}
181
182template <typename T>
183scalar<T> &
184scalar<T>::clone() const
185{
186 return *new scalar<T>( *this );
187}
188
189// Assignment Operators
190
191// inherited from observation...don't forget it!
192template <typename T>
193observation &
194scalar<T>::operator=( const observation & rhs )
195{
196 m_value = cast_observation_<T>( rhs );
197 return *this;
198}
199
200
201// class assignment ops
202template <typename T>
203template <typename Tp>
204scalar<T> &
205scalar<T>::operator=( const scalar<Tp> & rhs )
206{
207 m_value = cast_observation_<T>( rhs );
208 return *this;
209}
210template <typename T>
211scalar<T> &
212scalar<T>::operator=( const scalar & rhs )
213{
214 m_value = rhs.m_value;
215 return *this;
216}
217template <typename T>
218scalar<T> &
219scalar<T>::operator=( const T &v )
220{
221 m_value = v;
222 return *this;
223}
224template <typename T>
225scalar<T> &
226scalar<T>::operator=( tom::value_type vt )
227{
228 m_value = vt;
229 return *this;
230}
231
232template <typename T>
233bool
234scalar<T>::is_normal() const
235{
236 return m_value.is_normal();
237}
238
239template <typename T>
240std::ostream &
241scalar<T>::print( std::ostream & os ) const
242{
243 return os << m_value;
244}
245
246template <typename T>
248scalar<T>::flag() const
249{
250 return m_value.get_value_type();
251}
252
253template <typename T>
254observation &
255scalar<T>::operator+=( const observation & rhs )
256{
257 m_value += cast_observation_<T>( rhs );
258 return *this;
259}
260template <typename T>
261scalar<T> &
262scalar<T>::operator+=( const scalar & rhs )
263{
264 m_value += rhs.m_value;
265 return *this;
266}
267template <typename T>
268scalar<T> &
269scalar<T>::operator+=( const T &rhs )
270{
271 m_value += rhs;
272 return *this;
273}
274template <typename T>
275observation &
276scalar<T>::operator-=( const observation & rhs )
277{
278 m_value -= cast_observation_<T>( rhs );
279 return *this;
280}
281template <typename T>
282scalar<T> &
283scalar<T>::operator-=( const scalar & rhs )
284{
285 m_value -= rhs.m_value;
286 return *this;
287}
288template <typename T>
289scalar<T> &
290scalar<T>::operator-=( const T & rhs )
291{
292 m_value -= rhs;
293 return *this;
294}
295template <typename T>
296observation &
297scalar<T>::operator*=( const observation & rhs )
298{
299 m_value *= cast_observation_<T>( rhs );
300 return *this;
301}
302template <typename T>
303scalar<T> &
304scalar<T>::operator*=( const scalar & rhs )
305{
306 m_value *= rhs.m_value;
307 return *this;
308}
309template <typename T>
310scalar<T> &
311scalar<T>::operator*=( const T & rhs )
312{
313 m_value *= rhs;
314 return *this;
315}
316template <typename T>
317observation &
318scalar<T>::operator/=( const observation & rhs )
319{
320 m_value /= cast_observation_<T>( rhs );
321 return *this;
322}
323template <typename T>
324scalar<T> &
325scalar<T>::operator/=( const scalar & rhs )
326{
327 m_value /= rhs.m_value;
328 return *this;
329}
330template <typename T>
331scalar<T> &
332scalar<T>::operator/=( const T & rhs )
333{
334 m_value /= rhs;
335 return *this;
336}
337
338
339template <typename T>
340bool
341scalar<T>::operator<( const observation & rhs ) const
342{
343 scalar_type tmp = cast_observation_<T>( rhs );
344 return m_value < tmp;
345}
346template <typename T>
347bool
348scalar<T>::operator<=( const observation & rhs ) const
349{
350 scalar_type tmp = cast_observation_<T>( rhs );
351 return m_value <= tmp;
352}
353template <typename T>
354bool
355scalar<T>::operator>( const observation & rhs ) const
356{
357 scalar_type tmp = cast_observation_<T>( rhs );
358 return m_value > tmp;
359}
360template <typename T>
361bool
362scalar<T>::operator>=( const observation & rhs ) const
363{
364 scalar_type tmp = cast_observation_<T>( rhs );
365 return m_value >= tmp;
366}
367
368template <typename T>
369bool
370scalar<T>::operator==( const observation & rhs ) const
371{
372 scalar_type tmp = cast_observation_<T>( rhs );
373 return m_value == tmp;
374}
375template <typename T>
376bool
377scalar<T>::operator!=( const observation & rhs ) const
378{
379 scalar_type tmp = cast_observation_<T>( rhs );
380 return m_value != tmp;
381}
382
383
384// cast operators
385template <typename T>
386scalar<T>::operator unsigned char() const
387{
389 return tmp.value_;
390}
391template <typename T>
392scalar<T>::operator tom::value_types::string() const
393{
395 return tmp.value_;
396}
397template <typename T>
398scalar<T>::operator short int() const
399{
401 return tmp.value_;
402}
403template <typename T>
404scalar<T>::operator int() const
405{
406 tom::value_types::scalar<int> tmp( m_value );
407 return tmp.value_;
408}
409template <typename T>
410scalar<T>::operator long int() const
411{
413 return tmp.value_;
414}
415template <typename T>
416scalar<T>::operator unsigned short int() const
417{
419 return tmp.value_;
420}
421template <typename T>
422scalar<T>::operator unsigned int() const
423{
425 return tmp.value_;
426}
427template <typename T>
428scalar<T>::operator unsigned long int() const
429{
431 return tmp.value_;
432}
433template <typename T>
434scalar<T>::operator float() const
435{
436 tom::value_types::scalar<float> tmp( m_value );
437 return tmp.value_;
438}
439template <typename T>
440scalar<T>::operator double() const
441{
443 return tmp.value_;
444}
445template <typename T>
446scalar<T>::operator bool() const
447{
449 if ( tmp.is_normal() )
450 return tmp.value_;
451 else
452 return false;
453}
454
455template <typename T>
456std::ostream &
457operator<<( std::ostream &os, const scalar<T> & rhs )
458{
459 return rhs.print(os);
460}
461
462template <typename T>
463const scalar<T> scalar<T>::HAS_NO_DATA( tom::value_types::resolve_nd<T>::get_value_of_nd() );
464
465template <typename T>
466const scalar<T> scalar<T>::HOLIDAY( tom::value_types::resolve_nd<T>::get_value_of_na() );
467
468template <typename T>
469const scalar<T> scalar<T>::NON_CALC( tom::value_types::resolve_nd<T>::get_value_of_nc() );
470
471
472// okay! How about some mixed scalar<> math
473template<typename T, typename Tp>
474scalar<T>
475operator+( const scalar<T> &a, const scalar<Tp> &b )
476{
477 scalar<T> result( a );
478 result += b;
479 return result;
480}
481
482} // end namespace tom
483
484
485#include <tom-util/string.hpp>
486#include <tom-util/dates.hpp>
487
488#endif
Definition observation.hpp:13
Definition scalar.hpp:19
Definition value_types.hpp:381
Definition value_types.hpp:96
Definition value_types.hpp:144
Definition value_types.hpp:324
Definition value_types.hpp:319
Definition value_types.hpp:329
Definition value_types.hpp:772