Value.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @file artis/kernel/Value.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2017 ULCO http://www.univ-littoral.fr
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef __ARTIS_KERNEL_VALUE_HPP
  22. #define __ARTIS_KERNEL_VALUE_HPP
  23. #include <boost/serialization/serialization.hpp>
  24. #include <boost/serialization/array.hpp>
  25. #include <algorithm>
  26. #include <cstring>
  27. #include <typeinfo>
  28. #include <iostream>
  29. namespace artis { namespace kernel {
  30. enum ValueType { DOUBLE, INT, BOOL };
  31. class Any
  32. {
  33. private:
  34. struct base
  35. {
  36. virtual ~base() { }
  37. virtual base* clone() const = 0;
  38. };
  39. template < typename T, typename V >
  40. struct data : base
  41. {
  42. data(V T::* const& value) : value_(value)
  43. { }
  44. base* clone() const { return new data < T, V >(*this); }
  45. V T::* value_;
  46. };
  47. base* ptr_;
  48. public:
  49. Any() : ptr_(nullptr)
  50. { }
  51. template < typename T, typename V >
  52. Any(V T::* const& value) : ptr_(new data < T, V >(value))
  53. { }
  54. Any(Any const& other) : ptr_(other.ptr_? other.ptr_->clone() : nullptr)
  55. { }
  56. virtual ~Any()
  57. { if (this->ptr_) delete this->ptr_; }
  58. void operator=(Any const& other)
  59. { Any(other).swap(*this); }
  60. void swap(Any& other)
  61. { std::swap(this->ptr_, other.ptr_); }
  62. template < typename T, typename V >
  63. V T::* get() const
  64. { return dynamic_cast < data < T, V >* >(ptr_)->value_; }
  65. bool is_null() const
  66. { return ptr_ == nullptr; }
  67. template < typename T, typename V >
  68. void put(T* o, const V& value )
  69. {
  70. V T::* p = dynamic_cast <
  71. data < T, V >* >(ptr_)->value_;
  72. o->*(p) = value;
  73. }
  74. template < typename T >
  75. std::string to_string(const T* o) const
  76. {
  77. if (ptr_) {
  78. data < T, double >* q_double =
  79. dynamic_cast < data < T, double >* >(ptr_);
  80. if (q_double) {
  81. return std::to_string(o->*(q_double->value_));
  82. } else {
  83. data < T, int >* q_int =
  84. dynamic_cast < data < T, int >* >(ptr_);
  85. if (q_int) {
  86. return std::to_string(o->*(q_int->value_));
  87. } else {
  88. data < T, bool >* q_bool =
  89. dynamic_cast < data < T, bool >* >(ptr_);
  90. if (q_bool) {
  91. return o->*(q_bool->value_) ? "true": "false";
  92. } else {
  93. return "NA";
  94. }
  95. }
  96. }
  97. } else {
  98. return "NA";
  99. }
  100. }
  101. };
  102. class Value
  103. {
  104. public:
  105. Value() : _content(nullptr), _size(0)
  106. { }
  107. template < typename T >
  108. Value(T value)
  109. { assign(&value, sizeof(T), typeid(T).hash_code()); }
  110. Value(void* content, size_t size)
  111. { assign(content, size, typeid(void*).hash_code()); }
  112. Value(const char* value, unsigned int size)
  113. { assign(value, size * sizeof(char), typeid(char*).hash_code()); }
  114. virtual ~Value()
  115. { /* if (_content) delete[] _content; */ }
  116. bool empty() const
  117. { return _content == nullptr; }
  118. template < typename T >
  119. void get_content(T& value) const
  120. {
  121. assert(_type_id == typeid(T).hash_code());
  122. value = *(T*)(_content);
  123. }
  124. std::string to_string() const
  125. {
  126. return std::string();
  127. }
  128. private:
  129. void assign(const void* content, size_t size, size_t type_id)
  130. {
  131. _content = new char[size];
  132. std::memcpy(_content, content, size);
  133. _size = size;
  134. _type_id = type_id;
  135. }
  136. friend class boost::serialization::access;
  137. template < class Archive >
  138. void serialize(Archive & ar, const unsigned int version)
  139. {
  140. (void) version;
  141. ar & _size;
  142. if (Archive::is_loading::value) {
  143. assert(_content == nullptr);
  144. _content = new char[_size];
  145. }
  146. ar & boost::serialization::make_array < char >(_content, _size);
  147. ar & _type_id;
  148. }
  149. char* _content;
  150. size_t _size;
  151. size_t _type_id;
  152. };
  153. } }
  154. #endif