Value.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 <string>
  33. #include <typeinfo>
  34. #include <vector>
  35. namespace artis {
  36. namespace common {
  37. class Value
  38. {
  39. public:
  40. Value()
  41. : _content(nullptr), _size(0)
  42. {}
  43. template<typename T>
  44. Value(const T value)
  45. { assign(&value, sizeof(T), typeid(T).hash_code()); }
  46. template<typename T>
  47. Value(const T *value, size_t size)
  48. { assign(value, sizeof(T) * size, typeid(T *).hash_code()); }
  49. template<typename T>
  50. Value(const std::vector <T> &value, size_t size)
  51. { assign(value.data(), sizeof(T) * value.size(), typeid(T *).hash_code()); }
  52. Value(void *content, size_t size)
  53. { assign(content, size, typeid(void *).hash_code()); }
  54. Value(const char *value, unsigned int size)
  55. { assign(value, size * sizeof(char), typeid(char *).hash_code()); }
  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 T>
  86. void operator()(std::vector <T> &value) const
  87. {
  88. assert(_type_id == typeid(T *).hash_code());
  89. size_t size = _size / sizeof(T);
  90. for (size_t i = 0; i < size; ++i) {
  91. value.push_back(((T *) (_content))[i]);
  92. }
  93. }
  94. template<typename Z>
  95. bool is_type() const
  96. { return _type_id == typeid(Z).hash_code(); }
  97. Value &operator=(const Value &value)
  98. {
  99. if (_content != nullptr) {
  100. delete _content;
  101. }
  102. if (value._content) {
  103. assign(value._content, value._size, value._type_id);
  104. } else {
  105. _content = nullptr;
  106. _size = 0;
  107. _type_id = 0;
  108. }
  109. return *this;
  110. }
  111. template<typename Z>
  112. size_t size() const
  113. {
  114. assert(is_type<Z>());
  115. return _size / sizeof(Z);
  116. }
  117. std::string to_string() const
  118. {
  119. if (empty()) {
  120. return "<null>";
  121. } else if (is_type<double>()) {
  122. double v;
  123. operator()(v);
  124. return std::to_string(v);
  125. } else if (is_type<int>()) {
  126. int v;
  127. operator()(v);
  128. return std::to_string(v);
  129. } else if (is_type<unsigned int>()) {
  130. unsigned int v;
  131. operator()(v);
  132. return std::to_string(v);
  133. } else if (is_type<bool>()) {
  134. bool v;
  135. operator()(v);
  136. return v ? "true" : "false";
  137. } else if (is_type<double *>()) {
  138. double *v;
  139. std::string str;
  140. size_t size = _size / sizeof(double);
  141. operator()(v);
  142. for (size_t i = 0; i < size; ++i) {
  143. str += std::to_string(v[i]) + std::string(" ");
  144. }
  145. return str;
  146. } else if (is_type<int *>()) {
  147. int *v;
  148. std::string str;
  149. size_t size = _size / sizeof(int);
  150. operator()(v);
  151. for (size_t i = 0; i < size; ++i) {
  152. str += std::to_string(v[i]) + std::string(" ");
  153. }
  154. return str;
  155. } else if (is_type<unsigned int *>()) {
  156. unsigned int *v;
  157. std::string str;
  158. size_t size = _size / sizeof(unsigned int);
  159. operator()(v);
  160. for (size_t i = 0; i < size; ++i) {
  161. str += std::to_string(v[i]) + std::string(" ");
  162. }
  163. return str;
  164. } else if (is_type<bool *>()) {
  165. bool *v;
  166. std::string str;
  167. size_t size = _size / sizeof(bool);
  168. operator()(v);
  169. for (size_t i = 0; i < size; ++i) {
  170. str += std::to_string(v[i]) + std::string(" ");
  171. }
  172. return str;
  173. } else {
  174. return "<unstringify>";
  175. }
  176. }
  177. private:
  178. void assign(const void *content, size_t size, size_t type_id)
  179. {
  180. _content = new char[size];
  181. std::memcpy(_content, content, size);
  182. _size = size;
  183. _type_id = type_id;
  184. }
  185. friend class boost::serialization::access;
  186. template<class Archive>
  187. void serialize(Archive &ar, const unsigned int version)
  188. {
  189. (void) version;
  190. ar & _size;
  191. if (Archive::is_loading::value) {
  192. assert(_content == nullptr);
  193. _content = new char[_size];
  194. }
  195. ar & boost::serialization::make_array<char>(_content, _size);
  196. ar & _type_id;
  197. }
  198. char *_content;
  199. size_t _size;
  200. size_t _type_id;
  201. };
  202. }
  203. } // namespace artis common
  204. #endif