Value.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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)
  51. {
  52. const T *v = value.data();
  53. assign(v, sizeof(T) * value.size(), typeid(T *).hash_code());
  54. }
  55. Value(const std::vector <bool> &value)
  56. {
  57. size_t size = sizeof(bool) * value.size();
  58. _content = new char[size];
  59. for (size_t i = 0; i < value.size(); ++i) {
  60. _content[i] = value[i];
  61. }
  62. _size = size;
  63. _type_id = typeid(bool *).hash_code();
  64. }
  65. Value(void *content, size_t size)
  66. { assign(content, size, typeid(void *).hash_code()); }
  67. Value(const char *value, unsigned int size)
  68. { assign(value, size * sizeof(char), typeid(char *).hash_code()); }
  69. Value(const Value &value)
  70. {
  71. if (value._content) {
  72. assign(value._content, value._size, value._type_id);
  73. } else {
  74. _content = nullptr;
  75. _size = 0;
  76. _type_id = 0;
  77. }
  78. }
  79. virtual ~Value()
  80. {
  81. if (_content != nullptr)
  82. delete[] _content;
  83. }
  84. bool empty() const
  85. { return _content == nullptr; }
  86. template<typename T>
  87. void operator()(T &value) const
  88. {
  89. assert(_type_id == typeid(T).hash_code());
  90. value = *(T *) (_content);
  91. }
  92. template<typename T>
  93. void operator()(T *&value) const
  94. {
  95. assert(_type_id == typeid(T *).hash_code());
  96. value = (T *) (_content);
  97. }
  98. template<typename T>
  99. void operator()(std::vector <T> &value) const
  100. {
  101. assert(_type_id == typeid(T *).hash_code());
  102. size_t size = _size / sizeof(T);
  103. for (size_t i = 0; i < size; ++i) {
  104. value.push_back(((T *) (_content))[i]);
  105. }
  106. }
  107. template<typename Z>
  108. bool is_type() const
  109. { return _type_id == typeid(Z).hash_code(); }
  110. Value &operator=(const Value &value)
  111. {
  112. if (_content != nullptr) {
  113. delete _content;
  114. }
  115. if (value._content) {
  116. assign(value._content, value._size, value._type_id);
  117. } else {
  118. _content = nullptr;
  119. _size = 0;
  120. _type_id = 0;
  121. }
  122. return *this;
  123. }
  124. template<typename Z>
  125. size_t size() const
  126. {
  127. assert(is_type<Z>());
  128. return _size / sizeof(Z);
  129. }
  130. std::string to_string() const
  131. {
  132. if (empty()) {
  133. return "<null>";
  134. } else if (is_type<double>()) {
  135. double v;
  136. operator()(v);
  137. return std::to_string(v);
  138. } else if (is_type<int>()) {
  139. int v;
  140. operator()(v);
  141. return std::to_string(v);
  142. } else if (is_type<unsigned int>()) {
  143. unsigned int v;
  144. operator()(v);
  145. return std::to_string(v);
  146. } else if (is_type<bool>()) {
  147. bool v;
  148. operator()(v);
  149. return v ? "true" : "false";
  150. } else if (is_type<char>()) {
  151. char v;
  152. operator()(v);
  153. return std::to_string(v);
  154. } else if (is_type<double *>()) {
  155. double *v;
  156. std::string str;
  157. size_t size = _size / sizeof(double);
  158. operator()(v);
  159. for (size_t i = 0; i < size; ++i) {
  160. str += std::to_string(v[i]) + std::string(" ");
  161. }
  162. return str;
  163. } else if (is_type<int *>()) {
  164. int *v;
  165. std::string str;
  166. size_t size = _size / sizeof(int);
  167. operator()(v);
  168. for (size_t i = 0; i < size; ++i) {
  169. str += std::to_string(v[i]) + std::string(" ");
  170. }
  171. return str;
  172. } else if (is_type<unsigned int *>()) {
  173. unsigned int *v;
  174. std::string str;
  175. size_t size = _size / sizeof(unsigned int);
  176. operator()(v);
  177. for (size_t i = 0; i < size; ++i) {
  178. str += std::to_string(v[i]) + std::string(" ");
  179. }
  180. return str;
  181. } else if (is_type<bool *>()) {
  182. bool *v;
  183. std::string str;
  184. size_t size = _size / sizeof(bool);
  185. operator()(v);
  186. for (size_t i = 0; i < size; ++i) {
  187. str += std::to_string(v[i]) + std::string(" ");
  188. }
  189. return str;
  190. } else if (is_type<char *>()) {
  191. char *v;
  192. operator()(v);
  193. return std::string(v, _size / sizeof(char));
  194. } else {
  195. return "<unstringify>";
  196. }
  197. }
  198. private:
  199. void assign(const void *content, size_t size, size_t type_id)
  200. {
  201. _content = new char[size];
  202. std::memcpy(_content, content, size);
  203. _size = size;
  204. _type_id = type_id;
  205. }
  206. friend class boost::serialization::access;
  207. template<class Archive>
  208. void serialize(Archive &ar, const unsigned int version)
  209. {
  210. (void) version;
  211. ar & _size;
  212. if (Archive::is_loading::value) {
  213. assert(_content == nullptr);
  214. _content = new char[_size];
  215. }
  216. ar & boost::serialization::make_array<char>(_content, _size);
  217. ar & _type_id;
  218. }
  219. char *_content;
  220. size_t _size;
  221. size_t _type_id;
  222. };
  223. }
  224. } // namespace artis common
  225. #endif