Any.hpp 6.8 KB

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