Value.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 std::string to_string() const = 0;
  53. virtual const std::type_info &type() const noexcept = 0;
  54. };
  55. template<typename T>
  56. class Data : public Base, public std::any {
  57. public:
  58. template<typename U = T, std::enable_if_t<not std::is_same_v<U, std::string>, bool> = true>
  59. Data(const U &value) : std::any(value) {}
  60. Data(const std::string &value) : std::any(value) {}
  61. Data(const std::valarray<T> &value) : std::any(value) {}
  62. Data(const std::vector<T> &value) : std::any(value) {}
  63. bool operator==(const Base &other) const override {
  64. return type() == other.type() and
  65. std::any_cast<const T &>(*this) == std::any_cast<const T &>(*dynamic_cast<const Data<T> *>(&other));
  66. }
  67. template<typename U>
  68. void operator()(U &value) const {
  69. value = std::any_cast<const U &>(*this);
  70. }
  71. std::string to_string() const override {
  72. std::stringstream ss;
  73. if constexpr (HasToString<T>::Has) {
  74. if (type() == typeid(std::vector<T>)) {
  75. const std::vector<T> &values = std::any_cast<const std::vector<T> &>(*this);
  76. for (const auto &e: values) {
  77. ss << std::boolalpha << e.to_string() << " ";
  78. }
  79. } else {
  80. ss << std::any_cast<const T &>(*this).to_string();
  81. }
  82. } else {
  83. if (type() == typeid(std::string)) {
  84. ss << std::any_cast<const std::string &>(*this);
  85. } else if (type() == typeid(std::valarray<T>)) {
  86. const std::valarray<T> &values = std::any_cast<const std::valarray<T> &>(*this);
  87. for (const auto &e: values) {
  88. ss << std::boolalpha << e << " ";
  89. }
  90. } else if (type() == typeid(std::vector<T>)) {
  91. const std::vector<T> &values = std::any_cast<const std::vector<T> &>(*this);
  92. for (const auto &e: values) {
  93. ss << std::boolalpha << e << " ";
  94. }
  95. } else {
  96. ss << std::boolalpha << std::any_cast<const T &>(*this);
  97. }
  98. }
  99. return ss.str();
  100. }
  101. const std::type_info &type() const noexcept override { return std::any::type(); }
  102. private:
  103. using _type = T;
  104. };
  105. public:
  106. Value() : _data(nullptr) {}
  107. Value(const Value& value) : _data(value._data) {}
  108. template<typename T, std::enable_if_t<not HasToString<T>::Has, bool> = true>
  109. Value(const T &value) : _data(new Data<T>(value)) {}
  110. template<typename T>
  111. Value(const std::vector<T> &value) : _data(new Data<T>(value)) {}
  112. template<typename T, std::enable_if_t<HasToString<T>::Has, bool> = true>
  113. Value(const T &value) : _data(new Data<T>(value)) {}
  114. template<typename T, size_t size, std::enable_if_t<std::is_same<T, char>::value, bool> = true>
  115. Value(const T (&value)[size]) : _data(new Data<T>(std::string(value, size))) {}
  116. template<typename T>
  117. Value(const T *value, size_t size) : _data(new Data<T>(std::valarray<T>(value, size))) {}
  118. bool is_null() const { return _data.get() == nullptr; }
  119. template<typename Z>
  120. bool is_type() const { return _data->type() == typeid(Z); }
  121. Value& operator=(const Value &other) {
  122. _data = other._data;
  123. return *this;
  124. }
  125. bool operator==(const Value &other) const {
  126. return _data->operator==(*other._data);
  127. }
  128. template<typename T>
  129. void operator()(T &value) const {
  130. dynamic_cast<Data<T> *>(_data.get())->operator()(value);
  131. }
  132. template<typename T>
  133. void operator()(std::vector<T> &value) const {
  134. dynamic_cast<Data<T> *>(_data.get())->operator()(value);
  135. }
  136. template<typename T>
  137. void operator()(std::valarray<T> &value) const {
  138. dynamic_cast<Data<T> *>(_data.get())->operator()(value);
  139. }
  140. std::string to_string() const {
  141. return _data->to_string();
  142. }
  143. private:
  144. friend class boost::serialization::access;
  145. template<typename Archive>
  146. void serialize(Archive &ar, const unsigned int version) {
  147. // TODO
  148. }
  149. std::shared_ptr<Base> _data;
  150. };
  151. std::ostream& operator<<(std::ostream& o, const artis::common::event::Value& value);
  152. } // namespace artis common event
  153. #endif