Value.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @file common/event/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-2022 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_EVENT_VALUE
  26. #define COMMON_EVENT_VALUE
  27. #include <boost/serialization/serialization.hpp>
  28. #include <boost/serialization/array.hpp>
  29. #include <cassert>
  30. #include <cstring>
  31. #include <functional>
  32. #include <map>
  33. #include <numeric>
  34. #include <string>
  35. #include <typeinfo>
  36. #include <vector>
  37. namespace artis::common::event {
  38. class Value {
  39. public:
  40. Value()
  41. : _content(nullptr), _size(0), _type_id(0) {}
  42. template<typename T>
  43. Value(const T &value) { assign(&value, sizeof(T), typeid(T).hash_code()); }
  44. template<typename T>
  45. Value(const T *value, size_t size) { assign(value, sizeof(T) * size, typeid(T *).hash_code()); }
  46. template<typename T>
  47. Value(const std::vector<T> &value) {
  48. const T *v = value.data();
  49. assign(v, sizeof(T) * value.size(), typeid(T *).hash_code());
  50. }
  51. Value(const std::vector<bool> &value) {
  52. size_t size = sizeof(bool) * value.size();
  53. _content = new char[size];
  54. for (size_t i = 0; i < value.size(); ++i) {
  55. _content[i] = value[i];
  56. }
  57. _size = size;
  58. _type_id = typeid(bool *).hash_code();
  59. }
  60. Value(void *content, size_t size) { assign(content, size, typeid(void *).hash_code()); }
  61. Value(const char *value, unsigned int size) { assign(value, size * sizeof(char), typeid(char *).hash_code()); }
  62. Value(const Value &value) {
  63. if (value._content != nullptr) {
  64. assign(value._content, value._size, value._type_id);
  65. } else {
  66. _content = nullptr;
  67. _size = 0;
  68. _type_id = 0;
  69. }
  70. }
  71. Value(Value &&value) : _content(value._content), _size(value._size), _type_id(value._type_id) {
  72. value._content = nullptr;
  73. value._size = 0;
  74. value._type_id = 0;
  75. }
  76. virtual ~Value() {
  77. delete[] _content;
  78. }
  79. bool is_null() const { return _content == nullptr; }
  80. template<typename Z>
  81. bool is_type() const { return _type_id == typeid(Z).hash_code(); }
  82. bool operator==(const Value &other) const {
  83. return _type_id == other._type_id and _size == other._size
  84. and std::memcmp(_content, other._content, _size) == 0;
  85. }
  86. template<typename T>
  87. void operator()(T &value) const {
  88. assert(_type_id == typeid(T).hash_code());
  89. value = *(T *) (_content);
  90. }
  91. template<typename T>
  92. void operator()(T *&value) const {
  93. assert(_type_id == typeid(T *).hash_code());
  94. value = (T *) (_content);
  95. }
  96. template<typename T>
  97. void operator()(std::vector<T> &value) const {
  98. assert(_type_id == typeid(T *).hash_code());
  99. size_t size = _size / sizeof(T);
  100. for (size_t i = 0; i < size; ++i) {
  101. value.push_back(((T *) (_content))[i]);
  102. }
  103. }
  104. Value &operator=(const Value &value) {
  105. delete[] _content;
  106. if (value._content != nullptr) {
  107. assign(value._content, value._size, value._type_id);
  108. } else {
  109. _content = nullptr;
  110. _size = 0;
  111. _type_id = 0;
  112. }
  113. return *this;
  114. }
  115. Value &operator=(Value &&value) {
  116. delete[] _content;
  117. _content = value._content;
  118. value._content = nullptr;
  119. _size = value._size;
  120. value._size = 0;
  121. _type_id = value._type_id;
  122. value._type_id = 0;
  123. return *this;
  124. }
  125. template<typename Z>
  126. size_t size() const {
  127. assert(is_type<Z *>());
  128. return _size / sizeof(Z);
  129. }
  130. std::string to_string() const {
  131. if (is_null()) {
  132. return "<null>";
  133. }
  134. auto it = convert.find(_type_id);
  135. if (it != convert.cend()) {
  136. return it->second(*this);
  137. }
  138. return "<unstringify>";
  139. }
  140. private:
  141. typedef std::map<unsigned long, std::function<std::string(const artis::common::event::Value &)>> ConvertFunctions;
  142. static const ConvertFunctions convert;
  143. void assign(const void *content, size_t size, size_t type_id) {
  144. _content = new char[size];
  145. std::memcpy(_content, content, size);
  146. _size = size;
  147. _type_id = type_id;
  148. }
  149. friend class boost::serialization::access;
  150. template<typename Archive>
  151. void serialize(Archive &ar, const unsigned int version) {
  152. (void) version;
  153. ar & _size;
  154. if (Archive::is_loading::value) {
  155. assert(_content == nullptr);
  156. _content = new char[_size];
  157. }
  158. ar & boost::serialization::make_array<char>(_content, _size);
  159. ar & _type_id;
  160. }
  161. char *_content = nullptr;
  162. size_t _size = 0;
  163. size_t _type_id = 0;
  164. };
  165. std::ostream& operator<<(std::ostream& o, const artis::common::event::Value& value);
  166. } // namespace artis common event
  167. #endif