Any.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * @file common/Any.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_STATE_ANY_HPP
  26. #define COMMON_STATE_ANY_HPP
  27. #include "artis-star/common/event/Value.hpp"
  28. #include <string>
  29. #include <tuple>
  30. #include <utility>
  31. #include <vector>
  32. namespace artis::common::state {
  33. class Any {
  34. private:
  35. struct base {
  36. virtual ~base() {}
  37. virtual base *clone() const = 0;
  38. };
  39. template<typename T, typename V>
  40. struct data : base {
  41. data(V T::* const &value)
  42. : value_(value) {}
  43. base *clone() const { return new data<T, V>(*this); }
  44. V T::* value_;
  45. };
  46. base *ptr_;
  47. public:
  48. Any()
  49. : ptr_(nullptr) {}
  50. template<typename T, typename V>
  51. Any(V T::* const &value)
  52. : ptr_(new data<T, V>(value)) {}
  53. Any(Any const &other)
  54. : ptr_(other.ptr_ ? other.ptr_->clone() : nullptr) {}
  55. virtual ~Any() {
  56. if (ptr_) {
  57. delete ptr_;
  58. }
  59. }
  60. void operator=(Any const &other) { Any(other).swap(*this); }
  61. void swap(Any &other) { std::swap(ptr_, other.ptr_); }
  62. template<typename T, typename V>
  63. V T::* get() const { return dynamic_cast < data<T, V> * >(ptr_)->value_; }
  64. template<typename T, typename V>
  65. void get(T *o, V &value) const {
  66. data<T, V> *q = dynamic_cast < data<T, V> * >(ptr_);
  67. if (q) {
  68. value = o->*(q->value_);
  69. } else {
  70. assert(false);
  71. }
  72. }
  73. bool is_null() const { return ptr_ == nullptr; }
  74. template<typename T, typename V>
  75. void put(T *o, const V &value) {
  76. V T::* p = dynamic_cast <data<T, V> * >(ptr_)->value_;
  77. o->*(p) = value;
  78. }
  79. template<typename T>
  80. void restore(T *o, const common::event::Value &value) {
  81. if (value.is_type<double>()) {
  82. double v;
  83. value(v);
  84. put(o, v);
  85. } else if (value.is_type<unsigned int>()) {
  86. unsigned int v;
  87. value(v);
  88. put(o, v);
  89. } else if (value.is_type<int>()) {
  90. int v;
  91. value(v);
  92. put(o, v);
  93. } else if (value.is_type<bool>()) {
  94. bool v;
  95. value(v);
  96. put(o, v);
  97. } else if (value.is_type < std::vector < double > > ()) {
  98. std::vector<double> v;
  99. value(v);
  100. put(o, v);
  101. } else if (value.is_type < std::vector < unsigned int > > ()) {
  102. std::vector<unsigned int> v;
  103. value(v);
  104. put(o, v);
  105. } else if (value.is_type < std::vector < int > > ()) {
  106. std::vector<int> v;
  107. value(v);
  108. put(o, v);
  109. } else if (value.is_type < std::vector < bool > > ()) {
  110. std::vector<bool> v;
  111. value(v);
  112. put(o, v);
  113. } else {
  114. assert(false);
  115. }
  116. }
  117. template<typename T>
  118. common::event::Value save(const T *o) const {
  119. if (ptr_) {
  120. data<T, double> *q_double =
  121. dynamic_cast < data<T, double> * >(ptr_);
  122. if (q_double) {
  123. return common::event::Value(o->*(q_double->value_));
  124. } else {
  125. data<T, unsigned int> *q_uint =
  126. dynamic_cast < data<T, unsigned int> * >(ptr_);
  127. if (q_uint) {
  128. return common::event::Value(o->*(q_uint->value_));
  129. } else {
  130. data<T, int> *q_int =
  131. dynamic_cast < data<T, int> * >(ptr_);
  132. if (q_int) {
  133. return common::event::Value(o->*(q_int->value_));
  134. } else {
  135. data<T, bool> *q_bool =
  136. dynamic_cast < data<T, bool> * >(ptr_);
  137. if (q_bool) {
  138. return common::event::Value(o->*(q_bool->value_));
  139. } else {
  140. data<T, std::vector < double> > *q_double_v =
  141. dynamic_cast < data<T, std::vector < double> > * > (ptr_);
  142. if (q_double_v) {
  143. return common::event::Value(o->*(q_double_v->value_));
  144. } else {
  145. data<T, std::vector < int> > *q_int_v =
  146. dynamic_cast < data<T, std::vector < int> > * > (ptr_);
  147. if (q_int_v) {
  148. return common::event::Value(o->*(q_int_v->value_));
  149. } else {
  150. data<T, std::vector < bool> > *q_bool_v =
  151. dynamic_cast < data<T, std::vector < bool> > * > (ptr_);
  152. if (q_bool_v) {
  153. return common::event::Value(o->*(q_bool_v->value_));
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }
  162. assert(false);
  163. return common::event::Value();
  164. }
  165. template<typename T>
  166. std::string to_string(const T *o) const {
  167. if (ptr_) {
  168. data<T, double> *q_double =
  169. dynamic_cast < data<T, double> * >(ptr_);
  170. if (q_double) {
  171. return std::to_string(o->*(q_double->value_));
  172. } else {
  173. data<T, unsigned int> *q_uint =
  174. dynamic_cast < data<T, unsigned int> * >(ptr_);
  175. if (q_uint) {
  176. return std::to_string(o->*(q_uint->value_));
  177. } else {
  178. data<T, int> *q_int =
  179. dynamic_cast < data<T, int> * >(ptr_);
  180. if (q_int) {
  181. return std::to_string(o->*(q_int->value_));
  182. } else {
  183. data<T, bool> *q_bool =
  184. dynamic_cast < data<T, bool> * >(ptr_);
  185. if (q_bool) {
  186. return o->*(q_bool->value_) ? "true" : "false";
  187. } else {
  188. data<T, std::vector < double> > *q_double_v =
  189. dynamic_cast < data<T, std::vector <
  190. double> > * > (ptr_);
  191. if (q_double_v) {
  192. return "";
  193. } else {
  194. data<T, std::vector < int> > *q_int_v =
  195. dynamic_cast < data<T, std::vector <
  196. int> > * > (ptr_);
  197. if (q_int_v) {
  198. return "";
  199. } else {
  200. data<T, std::vector < bool> > *q_bool_v =
  201. dynamic_cast < data<T, std::vector <
  202. bool> > * > (ptr_);
  203. if (q_bool_v) {
  204. return "";
  205. } else {
  206. return "NA";
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. }
  214. } else {
  215. return "NA";
  216. }
  217. }
  218. };
  219. }
  220. #endif