Value.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 {
  81. ss << std::any_cast<const T &>(*this).to_string();
  82. }
  83. } else {
  84. if (type() == typeid(std::string)) {
  85. ss << std::any_cast<const std::string &>(*this);
  86. } else if (type() == typeid(std::valarray<T>)) {
  87. const std::valarray<T> &values = std::any_cast<const std::valarray<T> &>(*this);
  88. for (const auto &e: values) {
  89. ss << std::boolalpha << e << " ";
  90. }
  91. } else if (type() == typeid(std::vector<T>)) {
  92. const std::vector<T> &values = std::any_cast<const std::vector<T> &>(*this);
  93. for (const auto &e: values) {
  94. ss << std::boolalpha << e << " ";
  95. }
  96. } else {
  97. ss << std::boolalpha << std::any_cast<const T &>(*this);
  98. }
  99. }
  100. return ss.str();
  101. }
  102. size_t size() const override {
  103. if (type() == typeid(std::vector<T>)) {
  104. return std::any_cast<const std::vector<T> &>(*this).size();
  105. } else if (type() == typeid(std::string)) {
  106. return std::any_cast<const std::string &>(*this).size();
  107. } else if (type() == typeid(std::valarray<T>)) {
  108. return std::any_cast<const std::valarray<T> &>(*this).size();
  109. } else { return 1; }
  110. }
  111. const std::type_info &type() const noexcept override { return std::any::type(); }
  112. private:
  113. using _type = T;
  114. };
  115. public:
  116. Value() : _data(nullptr) {}
  117. Value(const Value &value) : _data(value._data) {}
  118. template<typename T, std::enable_if_t<not HasToString<T>::Has, bool> = true>
  119. Value(const T &value) : _data(new Data<T>(value)) {}
  120. template<typename T>
  121. Value(const std::vector<T> &value) : _data(new Data<T>(value)) {}
  122. template<typename T, std::enable_if_t<HasToString<T>::Has, bool> = true>
  123. Value(const T &value) : _data(new Data<T>(value)) {}
  124. template<typename T, size_t size, std::enable_if_t<std::is_same<T, char>::value, bool> = true>
  125. Value(const T (&value)[size]) : _data(new Data<T>(std::string(value, size))) {}
  126. template<typename T>
  127. Value(const T *value, size_t size) : _data(new Data<T>(std::valarray<T>(value, size))) {}
  128. bool is_null() const { return _data.get() == nullptr; }
  129. template<typename Z>
  130. bool is_type() const { return _data->type() == typeid(Z); }
  131. Value &operator=(const Value &other) {
  132. _data = other._data;
  133. return *this;
  134. }
  135. bool operator==(const Value &other) const {
  136. return _data->operator==(*other._data);
  137. }
  138. template<typename T>
  139. void operator()(T &value) const {
  140. dynamic_cast<Data<T> *>(_data.get())->operator()(value);
  141. }
  142. template<typename T>
  143. void operator()(std::vector<T> &value) const {
  144. dynamic_cast<Data<T> *>(_data.get())->operator()(value);
  145. }
  146. template<typename T>
  147. void operator()(std::valarray<T> &value) const {
  148. dynamic_cast<Data<T> *>(_data.get())->operator()(value);
  149. }
  150. size_t size() const { return _data->size(); }
  151. std::string to_string() const { return _data->to_string(); }
  152. private:
  153. friend class boost::serialization::access;
  154. template<typename Archive>
  155. void serialize(Archive & /* ar */, const unsigned int /* version */) {
  156. // TODO
  157. }
  158. std::shared_ptr<Base> _data;
  159. };
  160. std::ostream &operator<<(std::ostream &o, const artis::common::event::Value &value);
  161. } // namespace artis common event
  162. #endif