Value.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 <any>
  29. #include <memory>
  30. #include <sstream>
  31. #include <string>
  32. #include <type_traits>
  33. #include <valarray>
  34. #include <vector>
  35. namespace artis::common::event {
  36. template<typename T>
  37. struct HasToString {
  38. template<typename U, std::string (U::*)() const>
  39. struct SFINAE {
  40. };
  41. template<typename U>
  42. static char Test(SFINAE<U, &U::to_string> *);
  43. template<typename U>
  44. static int Test(...);
  45. static const bool Has = sizeof(Test<T>(0)) == sizeof(char);
  46. };
  47. class Value {
  48. class Base {
  49. public:
  50. virtual ~Base() = default;
  51. virtual bool operator==(const Base &other) const = 0;
  52. virtual size_t size() const = 0;
  53. virtual std::string to_string() const = 0;
  54. virtual const std::type_info &type() const noexcept = 0;
  55. };
  56. template<typename T>
  57. class Data : public Base, public std::any {
  58. public:
  59. template<typename U = T, std::enable_if_t<not std::is_same_v<U, std::string>, bool> = true>
  60. Data(const U &value) : std::any(value) {}
  61. Data(const std::string &value) : std::any(value) {}
  62. Data(const std::valarray<T> &value) : std::any(value) {}
  63. Data(const std::vector<T> &value) : std::any(value) {}
  64. bool operator==(const Base &other) const override {
  65. return type() == other.type() and
  66. std::any_cast<const T &>(*this) == std::any_cast<const T &>(*dynamic_cast<const Data<T> *>(&other));
  67. }
  68. template<typename U>
  69. void operator()(U &value) const {
  70. value = std::any_cast<const U &>(*this);
  71. }
  72. std::string to_string() const override {
  73. std::stringstream ss;
  74. if constexpr (HasToString<T>::Has) {
  75. if (type() == typeid(std::vector<T>)) {
  76. const std::vector<T> &values = std::any_cast<const std::vector<T> &>(*this);
  77. for (const auto &e: values) {
  78. ss << std::boolalpha << e.to_string() << " ";
  79. }
  80. } else if (type() == typeid(std::vector<std::vector<T>>)) {
  81. const std::vector<std::vector<T>> &lines = std::any_cast<const std::vector<std::vector<T>> &>(*this);
  82. for (const auto &values: lines) {
  83. for (const auto &e: values) {
  84. ss << std::boolalpha << e.to_string() << " ";
  85. }
  86. ss << "| ";
  87. }
  88. } else {
  89. ss << std::any_cast<const T &>(*this).to_string();
  90. }
  91. } else {
  92. if (type() == typeid(std::string)) {
  93. ss << std::any_cast<const std::string &>(*this);
  94. } else if (type() == typeid(std::valarray<T>)) {
  95. const std::valarray<T> &values = std::any_cast<const std::valarray<T> &>(*this);
  96. for (const auto &e: values) {
  97. ss << std::boolalpha << e << " ";
  98. }
  99. } else if (type() == typeid(std::vector<T>)) {
  100. const std::vector<T> &values = std::any_cast<const std::vector<T> &>(*this);
  101. for (const auto &e: values) {
  102. ss << std::boolalpha << e << " ";
  103. }
  104. } else if (type() == typeid(std::vector<std::vector<T>>)) {
  105. const std::vector<std::vector<T>> &lines = std::any_cast<const std::vector<std::vector<T>> &>(*this);
  106. for (const auto &values: lines) {
  107. for (const auto &e: values) {
  108. ss << std::boolalpha << e << " ";
  109. }
  110. ss << "| ";
  111. }
  112. } else {
  113. ss << std::boolalpha << std::any_cast<const T &>(*this);
  114. }
  115. }
  116. return ss.str();
  117. }
  118. size_t size() const override {
  119. if (type() == typeid(std::vector<T>)) {
  120. return std::any_cast<const std::vector<T> &>(*this).size();
  121. } else if (type() == typeid(std::string)) {
  122. return std::any_cast<const std::string &>(*this).size();
  123. } else if (type() == typeid(std::valarray<T>)) {
  124. return std::any_cast<const std::valarray<T> &>(*this).size();
  125. } else { return 1; }
  126. }
  127. const std::type_info &type() const noexcept override { return std::any::type(); }
  128. private:
  129. using _type = T;
  130. };
  131. public:
  132. Value() : _data(nullptr) {}
  133. Value(const Value &value) : _data(value._data) {}
  134. template<typename T>
  135. Value(const std::vector<std::vector<T>> &value) : _data(new Data<T>(value)) {}
  136. template<typename T>
  137. Value(const std::vector<T> &value) : _data(new Data<T>(value)) {}
  138. template<typename T>
  139. Value(const T &value) : _data(new Data<T>(value)) {}
  140. template<typename T, size_t size, std::enable_if_t<std::is_same<T, char>::value, bool> = true>
  141. Value(const T (&value)[size]) : _data(new Data<T>(std::string(value, size))) {}
  142. template<typename T>
  143. Value(const T *value, size_t size) : _data(new Data<T>(std::valarray<T>(value, size))) {}
  144. bool is_null() const { return _data.get() == nullptr; }
  145. template<typename Z>
  146. bool is_type() const { return _data->type() == typeid(Z); }
  147. Value &operator=(const Value &other) {
  148. _data = other._data;
  149. return *this;
  150. }
  151. bool operator==(const Value &other) const {
  152. return _data->operator==(*other._data);
  153. }
  154. template<typename T>
  155. void operator()(T &value) const {
  156. dynamic_cast<Data<T> *>(_data.get())->operator()(value);
  157. }
  158. template<typename T>
  159. void operator()(T *&value) const {
  160. const std::valarray<T>& array = std::any_cast<const std::valarray<T>&>(*dynamic_cast<Data<T> *>(_data.get()));
  161. unsigned int index = 0;
  162. value = new T[size()];
  163. for (const auto& e: array) {
  164. value[index++] = e;
  165. }
  166. }
  167. template<typename T>
  168. void operator()(std::vector<T> &value) const {
  169. dynamic_cast<Data<T> *>(_data.get())->operator()(value);
  170. }
  171. template<typename T>
  172. void operator()(std::valarray<T> &value) const {
  173. dynamic_cast<Data<T> *>(_data.get())->operator()(value);
  174. }
  175. size_t size() const { return _data->size(); }
  176. std::string to_string() const { return _data->to_string(); }
  177. private:
  178. friend class boost::serialization::access;
  179. template<typename Archive>
  180. void serialize(Archive & /* ar */, const unsigned int /* version */) {
  181. // TODO
  182. }
  183. std::shared_ptr<Base> _data;
  184. };
  185. std::ostream &operator<<(std::ostream &o, const artis::common::event::Value &value);
  186. } // namespace artis common event
  187. #endif