Value.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @file 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 1
  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. public:
  38. Value()
  39. :_content(nullptr), _size(0) { }
  40. template<typename T>
  41. Value(T value) { assign(&value, sizeof(T), typeid(T).hash_code()); }
  42. template<typename T>
  43. Value(T* value, size_t size)
  44. {
  45. assign(value, sizeof(T) * size, typeid(T*).hash_code());
  46. }
  47. Value(void* content, size_t size) { assign(content, size, typeid(void*).hash_code()); }
  48. Value(const char* value, unsigned int size)
  49. {
  50. assign(value, size * sizeof(char), typeid(char*).hash_code());
  51. }
  52. Value(const Value& value)
  53. {
  54. if (value._content) {
  55. assign(value._content, value._size, value._type_id);
  56. } else {
  57. _content = nullptr;
  58. _size = 0;
  59. _type_id = 0;
  60. }
  61. }
  62. virtual ~Value() { if (_content != nullptr) delete[] _content; }
  63. bool empty() const { return _content == nullptr; }
  64. template<typename T>
  65. void operator()(T& value) const
  66. {
  67. assert(_type_id == typeid(T).hash_code());
  68. value = *(T*) (_content);
  69. }
  70. template<typename T>
  71. void operator()(T*& value) const
  72. {
  73. assert(_type_id == typeid(T*).hash_code());
  74. value = (T*) (_content);
  75. }
  76. template<typename Z>
  77. bool is_type() const { return _type_id == typeid(Z).hash_code(); }
  78. Value& operator=(const Value& value)
  79. {
  80. if (_content != nullptr) {
  81. delete _content;
  82. }
  83. if (value._content) {
  84. assign(value._content, value._size, value._type_id);
  85. } else {
  86. _content = nullptr;
  87. _size = 0;
  88. _type_id = 0;
  89. }
  90. return *this;
  91. }
  92. std::string to_string() const
  93. {
  94. if (empty()) {
  95. return "<null>";
  96. } else if (is_type<double>()) {
  97. double v;
  98. operator()(v);
  99. return std::to_string(v);
  100. } else if (is_type<int>()) {
  101. int v;
  102. operator()(v);
  103. return std::to_string(v);
  104. } else if (is_type<unsigned int>()) {
  105. unsigned int v;
  106. operator()(v);
  107. return std::to_string(v);
  108. } else if (is_type<bool>()) {
  109. bool v;
  110. operator()(v);
  111. return v ? "true" : "false";
  112. } else if (is_type<double*>()) {
  113. double* v;
  114. std::string str;
  115. size_t size = _size / sizeof(double);
  116. operator()(v);
  117. for (size_t i = 0; i < size; ++i) {
  118. str += std::to_string(v[i]) + std::string(" ");
  119. }
  120. return str;
  121. } else if (is_type<int*>()) {
  122. int* v;
  123. std::string str;
  124. size_t size = _size / sizeof(int);
  125. operator()(v);
  126. for (size_t i = 0; i < size; ++i) {
  127. str += std::to_string(v[i]) + std::string(" ");
  128. }
  129. return str;
  130. } else if (is_type<unsigned int*>()) {
  131. unsigned int* v;
  132. std::string str;
  133. size_t size = _size / sizeof(unsigned int);
  134. operator()(v);
  135. for (size_t i = 0; i < size; ++i) {
  136. str += std::to_string(v[i]) + std::string(" ");
  137. }
  138. return str;
  139. } else if (is_type<bool*>()) {
  140. bool* v;
  141. std::string str;
  142. size_t size = _size / sizeof(bool);
  143. operator()(v);
  144. for (size_t i = 0; i < size; ++i) {
  145. str += std::to_string(v[i]) + std::string(" ");
  146. }
  147. return str;
  148. } else {
  149. return "<unstringify>";
  150. }
  151. }
  152. private:
  153. void assign(const void* content, size_t size, size_t type_id)
  154. {
  155. _content = new char[size];
  156. std::memcpy(_content, content, size);
  157. _size = size;
  158. _type_id = type_id;
  159. }
  160. friend class boost::serialization::access;
  161. template<class Archive>
  162. void serialize(Archive& ar, const unsigned int version)
  163. {
  164. (void) version;
  165. ar & _size;
  166. if (Archive::is_loading::value) {
  167. assert(_content == nullptr);
  168. _content = new char[_size];
  169. }
  170. ar & boost::serialization::make_array<char>(_content, _size);
  171. ar & _type_id;
  172. }
  173. char* _content;
  174. size_t _size;
  175. size_t _type_id;
  176. };
  177. }
  178. } // namespace artis common
  179. #endif