Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
TSAVariant.h
1 /* ===================================================================
2 *
3 * T R A D I N G S Y S T E M A P I ™
4 * Copyright © 1999 - 2014 by Peter Ritter ( TradingSystemAPI.com )
5 * A L L R I G H T S R E S E R V E D
6 *
7 * Consult your license regarding permissions and restrictions.
8 * You may obtain a copy of the License at:
9 * http://www.TradingSystemAPI.com/licenses/LICENSE-TSAPI-3.0.html
10 *
11 * ====================================================================
12 */
13 
14 #ifndef TSA_VARIANT__INCLUDED
15 #define TSA_VARIANT__INCLUDED
16 
17 #include "TSATypeDef.h"
18 #include "TSATime.h"
19 
20 #include <string>
21 #include <ostream>
22 #include <vector>
23 
24 namespace tsa {
25  class out_storage_stream;
26  class in_storage_stream;
27  class object;
28  class string;
29 
33  /*
34  ** ====================================================
35  ** >>> class variant <<<
36  ** ====================================================
37  */
38 
140  class dll_export variant {
141  tsa_declare_testable;
142  static const unsigned SIZE_OF_LARGEST_TYPE;
143  friend class variant_series;
144  public:
145  //IU
146  static const char* class_name;
147  private:
148  union Data {
149  bool boolean;
150  float float32;
151  unsigned uint32;
152  int int32;
153  int64_t int64;
154  double float64;
155  void* void_ptr;
157  std::string* string_ptr;
158  date_time::bin_rep datetime;
159  } m_data;
160  char m_time_zone_half_hour_offset;
161  unsigned char m_type;
162  bool m_is_owner;
163  bool m_is_null;
164  public:
165  //===================================
168  //===================================
169 
171  variant(void);
172 
182  explicit variant(type_t type);
184  variant(const variant& other);
185 
187  variant(const date_time& val);
188 
190  variant(const date& val);
191 
193  variant(double val);
194 
196  variant(int val);
197 
199  variant(int64_t val);
200 
202  variant(uint64_t val);
203 
205  variant(bool val);
206 
208  variant(unsigned val);
209 
211  variant(float val);
212 
214  variant(const tsa::string& val);
215 
217  variant(const std::string& val);
218 
220  variant(const char* val);
221 
223  variant(char val);
224 
225  // Deprecated. Type member is initialized to type_t::object_ptr and value to @a val. Does NOT assume ownership of the pointer.
226  variant(const object* val);
227 
229  variant(const void* val);
230 
232  ~variant(void);
233 
234  //====================
236  //====================
237  public:
238 
239  void write_state(out_storage_stream&) const; //soon to be obsolete
240  void read_state(in_storage_stream&);
241 
242  void write(std::ostream&) const;
243  void read(std::istream&);
244 
245  template<class Archive>
246  void save(Archive & ar) const
247  {
248  type_t t = type();
249  ar(m_type);
250  bool is_def = defined();
251  ar(is_def);
252 
253  if (!defined()) {
254  return;
255  }
256 
257  if (is_ptr_type(t)) {
258  if (t == type_t::string) {
259  ar(*(m_data.string_ptr));
260  }
261  else throw tsa::exception("cannot serialize variant with pointer sub-type; save(Archive&).");
262  return;
263  }
264 
265  tsa_assert(defined());
266  ar.saveBinary((const char*)(this), sizeof(variant));
267  }
268 
269  template<class Archive>
270  void load(Archive & ar)
271  {
272  unsigned char tp;
273  ar(tp);
274  bool is_def;
275  ar(is_def);
276 
277  if (!is_def) {
278  reset_to_type((tsa::type_t)tp);
279  tsa_assert(!defined());
280  return;
281  }
282  tsa_assert(is_def);
283  if ((type_t)tp == type_t::string)
284  {
285  std::string str;
286  ar(str);
287  *this = str;
288  }
289  else {
290  ar.loadBinary((char*)(this), sizeof(variant));
291  tsa_assert(defined());
292  tsa_assert(type() == (tsa::type_t)tp);
293  }
294  }
295  public:
297  type_t type(void) const;
298 
308  bool is_numeric(void) const;
309 
315  bool defined(void) const;
316 
322  void clear(void);
323 
329  bool is_equal(const variant& other) const;
330 
331  bool operator==(const variant& other) const;
332  private:
333  bool not_defined(void) const;
334  void set_null(void);
335  void unset_null(void);
336  public:
337  void set_defined(bool);
338  void reset_to_type(type_t type);
339  void set_data_to_zero(void);
340 
349  bool is_owner(void) const;
350  public:
351  //===================================
354  //===================================
356  const variant& operator= (const variant& other);
357 
359  const variant& operator= (unsigned val);
360 
362  const variant& operator= (const date_time& val);
363 
365  const variant& operator= (const date& val);
366 
368  const variant& operator= (int val);
369 
371  const variant& operator= (int64_t val);
372 
374  const variant& operator= (uint64_t val);
375 
377  const variant& operator= (bool val);
378 
380  const variant& operator= (float val);
381 
383  const variant& operator= (double val);
384 
386  const variant& operator= (void* val);
387 
389  const variant& operator= (const char* val);
390 
391  // deprecated. Assignment operator. Type is set to type_t::string.
392  const variant& operator= (const tsa::string& val);
393 
395  const variant& operator= (const std::string& val);
396 
397  public:
398  // deprecated
399  void assign__no_type_chg(object* val);
400 
402  void assign__no_type_chg(float value);
403 
405  void assign__no_type_chg(double value);
406 
408  void assign__no_type_chg(unsigned value);
409 
411  void assign__no_type_chg(int value);
412 
414  void assign__no_type_chg(int64_t value);
415 
417  void assign__no_type_chg(unsigned long int value);
418 
420  void assign__no_type_chg(bool value);
421 
427  void assign__no_type_chg(const variant& other);
428 
430  void assign__no_type_chg(void* value);
431 
433  void assign__no_type_chg(const tsa::string& val);
434 
436  void assign__no_type_chg(const std::string& val);
437 
439  void assign__no_type_chg(const char* val);
440 
442  void assign__no_type_chg(const date_time& val);
443 
445  void assign__no_type_chg(const date& val);
446 
447  //====================
449  //====================
450  public:
451  const variant& operator= (object* val);
452  private:
453  void raise_nil(void) const;
454  void raise_cast_error(type_t, type_t) const;
455  void raise_type_error(type_t, type_t) const;
456  public:
462  double to_double(void) const;
463 
469  date_time to_datetime(void) const;
470 
476  date to_date(void) const;
477 
483  float to_float(void) const;
484 
490  unsigned to_uint(void) const;
491 
497  int to_int(void) const;
498 
504  int64_t to_int64(void) const;
505 
521  bool to_bool(void) const;
522 
523  //deprecated
524  object* to_object_ptr(void) const;
525 
531  void* to_void_ptr(void) const;
532  public:
534  std::string& get_string_ref(void)const;
535 
537  std::string& str(void)const;
538  public:
543  std::string to_string(void) const;
544 
550  template<typename T> T to(void) const;
551  public:
552  //===================================
555  //===================================
556 
573  template<typename T> T get(void) const;
574 
576  explicit operator float(void) const;
577 
579  explicit operator unsigned(void) const;
580 
582  explicit operator double(void) const;
583 
585  explicit operator int(void) const;
586 
588  explicit operator int64_t (void) const;
589 
590  // @brief Identical to to_float().
591  explicit operator uint64_t (void) const;
592 
594  explicit operator bool(void) const;
595 
597  explicit operator void* (void) const;
598 
599  //====================
601  //====================
602  explicit operator object* (void) const;
603  public:
604  std::string to_storage_string(void) const;
605  void from_storage_string(type_t type, const std::string& data);
606  public:
607  static char to_type_char(type_t);
608  static type_t type_char_to_type(char);
609  std::string to_typed_storage_string(void) const;
610  void from_typed_storage_string(const std::string& data);
611  public:
612  static const std::string visual_rep_of_undefined;
613  static const std::string string_placeholder_for_undefined;
614  static tsa::string truncate_zeros_from_float(const tsa::string&);
615  };
616 
617  //specializations of 'get' template function
618  template<> double variant::get<double>(void) const;
619  template<> float variant::get<float>(void) const;
620  template<> int variant::get<int>(void) const;
621  template<> int64_t variant::get<int64_t>(void) const;
622  template<> uint64_t variant::get<uint64_t>(void) const;
623  template<> unsigned variant::get<unsigned>(void) const;
624  template<> bool variant::get<bool>(void) const;
625  template<> date variant::get<date>(void) const;
626  template<> date_time variant::get<date_time>(void) const;
627  template<> tsa::string variant::get<string>(void) const;
628  template<> std::string variant::get<std::string>(void) const;
629  template<> tsa::object* variant::get<tsa::object*>(void) const;
630  template<> void* variant::get<void*>(void) const;
631 
632  //specializations of 'to' template function
633  template<> double variant::to<double>(void) const;
634  template<> float variant::to<float>(void) const;
635  template<> int variant::to<int>(void) const;
636  template<> int64_t variant::to<int64_t>(void) const;
637  template<> uint64_t variant::to<uint64_t>(void) const;
638  template<> unsigned variant::to<unsigned>(void) const;
639  template<> bool variant::to<bool>(void) const;
640  template<> date variant::to<date>(void) const;
641  template<> date_time variant::to<date_time>(void) const;
642  template<> std::string variant::to<std::string>(void) const;
643  template<> tsa::object* variant::to<tsa::object*>(void) const;
644  template<> void* variant::to<void*>(void) const;
645 
647  std::ostream& operator<< (std::ostream& stream, const variant& var);
648 
649  /*
650  ** ====================================================
651  ** >>> class variant <<<
652  ** ====================================================
653  */
654 
656  class auto_cast_variant : public variant
657  {
658  public:
659  auto_cast_variant(const variant& other) : variant(other) {}
660 
662  auto_cast_variant(const date_time& val) : variant(val) {}
663 
665  auto_cast_variant(const date& val) : variant(val) {}
666 
668  auto_cast_variant(double val) : variant(val) {}
669 
671  auto_cast_variant(int val) : variant(val) {}
672 
674  auto_cast_variant(int64_t val) : variant(val) {}
675 
677  auto_cast_variant(uint64_t val) : variant(val) {}
678 
680  auto_cast_variant(bool val) : variant(val) {}
681 
683  auto_cast_variant(unsigned val) : variant(val) {}
684 
686  auto_cast_variant(float val) : variant(val) {}
687 
689  auto_cast_variant(const tsa::string& val) : variant(val) {}
690 
692  auto_cast_variant(const std::string& val) : variant(val) {}
693 
695  auto_cast_variant(const char* val) : variant(val) {}
696 
698  auto_cast_variant(char val) : variant(val) {}
699  public:
701  operator float(void) const { return to<float>(); }
702 
704  operator unsigned(void) const { return to<unsigned>(); }
705 
707  operator double(void) const { return to<double>(); }
708 
710  operator int(void) const { return to<int>(); }
711 
713  operator int64_t (void) const { return to<int64_t>(); }
714 
715  // @brief Identical to to_float().
716  operator uint64_t (void) const { return to<uint64_t>(); }
717 
719  operator bool(void) const { return to<bool>(); }
720 
722  operator date (void) const { return to<date>(); }
723 
725  operator date_time(void) const { return to<date_time>(); }
726 
728  operator std::string(void) const { return to<std::string>(); }
729  };
730 
731 
733 
734 
735  class variant_vector : public std::vector<variant>{
736  static std::string class_name;
737  public:
738  variant_vector(void);
739  ~variant_vector(void);
740  const variant& operator[](size_t)const;
741  variant& operator[](size_t);
742  std::string to_string(void)const;
743  void from_string(const std::string&);
744  bool is_equal(const variant_vector&)const;
745  };
746 
747 
748 }//tsa
749 
750 #endif
auto_cast_variant(const date_time &val)
Constructor. Type member is initialized to type_t::datetime and value to val.
Definition: TSAVariant.h:662
auto_cast_variant(float val)
Constructor. Type member is initialized to type_t::float32 and value to val.
Definition: TSAVariant.h:686
Namespace for the &#39;Trading System API&#39; library.
Definition: original1.TSA3Core.cpp:20
auto_cast_variant(const tsa::string &val)
Constructor. Type member is initialized to string and value to val.
Definition: TSAVariant.h:689
auto_cast_variant(const char *val)
Constructor. Type member is initialized to type_t::string and value to val.
Definition: TSAVariant.h:695
auto_cast_variant(const std::string &val)
Constructor. Type member is initialized to type_t::string and value to val.
Definition: TSAVariant.h:692
32 bit floating point
32 bit unsigned integer
Definition: TSAVariant.h:656
64 bit floating point
auto_cast_variant(const date &val)
Constructor. Type member is initialized to type_t::date and value to val.
Definition: TSAVariant.h:665
auto_cast_variant(uint64_t val)
Constructor. Type member is initialized to type_t::int64 and value to val. (value will be signed) ...
Definition: TSAVariant.h:677
variant objects can represent values of different types.
Definition: TSAVariant.h:140
_value_types_type
Data type enumeration used throughout the library. Intended to be used via type_t.
Definition: TSATypeDef.h:166
Class tsa::exception used by most classes of the Trading System API library. The class inherits from ...
Definition: TSAError.h:37
auto_cast_variant(char val)
Constructor. Type member is initialized to type_t::string and value to val.
Definition: TSAVariant.h:698
Parent class for many library classes.
Definition: TSATypeDef.h:462
A date of the Gregorian calendar.
Definition: TSATime.h:119
auto_cast_variant(bool val)
Constructor. Type member is initialized to type_t::boolean and value to val.
Definition: TSAVariant.h:680
sref< bool > operator==(numeric_series_cref a, numeric_series_cref b)
comparison.
Definition: TSADSLBase.cpp:130
auto_cast_variant(int val)
Constructor. Type member is initialized to type_t::int32 and value to val.
Definition: TSAVariant.h:671
auto_cast_variant(double val)
Constructor. Type member is initialized to type_t::float64 and value to val.
Definition: TSAVariant.h:668
boolean true or false
auto_cast_variant(int64_t val)
Constructor. Type member is initialized to type_t::int64 and value to val.
Definition: TSAVariant.h:674
Class representing a gregorian-date and time-of-day combination. The time component has microsecond r...
Definition: TSATime.h:428
auto_cast_variant(unsigned val)
Constructor. Type member is initialized to type_t::uint32 and value to val.
Definition: TSAVariant.h:683