Value.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * @file common/Value.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef COMMON_VALUE
  26. #define COMMON_VALUE
  27. #include <boost/serialization/serialization.hpp>
  28. #include <boost/serialization/array.hpp>
  29. #include <cassert>
  30. #include <cstring>
  31. #include <numeric>
  32. #include <typeinfo>
  33. #include <vector>
  34. namespace artis {
  35. namespace common {
  36. class Value
  37. {
  38. public:
  39. Value()
  40. : _content(nullptr), _size(0)
  41. {}
  42. template<typename T>
  43. Value(T value)
  44. { assign(&value, sizeof(T), typeid(T).hash_code()); }
  45. template<typename T>
  46. Value(T *value, size_t size)
  47. {
  48. assign(value, sizeof(T) * size, typeid(T *).hash_code());
  49. }
  50. Value(void *content, size_t size)
  51. { assign(content, size, typeid(void *).hash_code()); }
  52. Value(const char *value, unsigned int size)
  53. {
  54. assign(value, size * sizeof(char), typeid(char *).hash_code());
  55. }
  56. Value(const Value &value)
  57. {
  58. if (value._content) {
  59. assign(value._content, value._size, value._type_id);
  60. } else {
  61. _content = nullptr;
  62. _size = 0;
  63. _type_id = 0;
  64. }
  65. }
  66. virtual ~Value()
  67. {
  68. if (_content != nullptr)
  69. delete[] _content;
  70. }
  71. bool empty() const
  72. { return _content == nullptr; }
  73. template<typename T>
  74. void operator()(T &value) const
  75. {
  76. assert(_type_id == typeid(T).hash_code());
  77. value = *(T *) (_content);
  78. }
  79. template<typename T>
  80. void operator()(T *&value) const
  81. {
  82. assert(_type_id == typeid(T *).hash_code());
  83. value = (T *) (_content);
  84. }
  85. template<typename Z>
  86. bool is_type() const
  87. { return _type_id == typeid(Z).hash_code(); }
  88. Value &operator=(const Value &value)
  89. {
  90. if (_content != nullptr) {
  91. delete _content;
  92. }
  93. if (value._content) {
  94. assign(value._content, value._size, value._type_id);
  95. } else {
  96. _content = nullptr;
  97. _size = 0;
  98. _type_id = 0;
  99. }
  100. return *this;
  101. }
  102. std::string to_string() const
  103. {
  104. if (empty()) {
  105. return "<null>";
  106. } else if (is_type<double>()) {
  107. double v;
  108. operator()(v);
  109. return std::to_string(v);
  110. } else if (is_type<int>()) {
  111. int v;
  112. operator()(v);
  113. return std::to_string(v);
  114. } else if (is_type<unsigned int>()) {
  115. unsigned int v;
  116. operator()(v);
  117. return std::to_string(v);
  118. } else if (is_type<bool>()) {
  119. bool v;
  120. operator()(v);
  121. return v ? "true" : "false";
  122. } else if (is_type<double *>()) {
  123. double *v;
  124. std::string str;
  125. size_t size = _size / sizeof(double);
  126. operator()(v);
  127. for (size_t i = 0; i < size; ++i) {
  128. str += std::to_string(v[i]) + std::string(" ");
  129. }
  130. return str;
  131. } else if (is_type<int *>()) {
  132. int *v;
  133. std::string str;
  134. size_t size = _size / sizeof(int);
  135. operator()(v);
  136. for (size_t i = 0; i < size; ++i) {
  137. str += std::to_string(v[i]) + std::string(" ");
  138. }
  139. return str;
  140. } else if (is_type<unsigned int *>()) {
  141. unsigned int *v;
  142. std::string str;
  143. size_t size = _size / sizeof(unsigned int);
  144. operator()(v);
  145. for (size_t i = 0; i < size; ++i) {
  146. str += std::to_string(v[i]) + std::string(" ");
  147. }
  148. return str;
  149. } else if (is_type<bool *>()) {
  150. bool *v;
  151. std::string str;
  152. size_t size = _size / sizeof(bool);
  153. operator()(v);
  154. for (size_t i = 0; i < size; ++i) {
  155. str += std::to_string(v[i]) + std::string(" ");
  156. }
  157. return str;
  158. } else {
  159. return "<unstringify>";
  160. }
  161. }
  162. private:
  163. void assign(const void *content, size_t size, size_t type_id)
  164. {
  165. _content = new char[size];
  166. std::memcpy(_content, content, size);
  167. _size = size;
  168. _type_id = type_id;
  169. }
  170. friend class boost::serialization::access;
  171. template<class Archive>
  172. void serialize(Archive &ar, const unsigned int version)
  173. {
  174. (void) version;
  175. ar & _size;
  176. if (Archive::is_loading::value) {
  177. assert(_content == nullptr);
  178. _content = new char[_size];
  179. }
  180. ar & boost::serialization::make_array<char>(_content, _size);
  181. ar & _type_id;
  182. }
  183. char *_content;
  184. size_t _size;
  185. size_t _type_id;
  186. };
  187. }
  188. } // namespace artis common
  189. #endif