Value.hpp 5.3 KB

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