Any.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 < double> > * > (ptr_);
  161. if (q_double_v) {
  162. return common::Value(o->*(q_double_v->value_));
  163. } else {
  164. data<T, std::vector < int> > *q_int_v =
  165. dynamic_cast < data<T, std::vector < int> > * > (ptr_);
  166. if (q_int_v) {
  167. return common::Value(o->*(q_int_v->value_));
  168. } else {
  169. data<T, std::vector < bool> > *q_bool_v =
  170. dynamic_cast < data<T, std::vector < bool> > * > (ptr_);
  171. if (q_bool_v) {
  172. return common::Value(o->*(q_bool_v->value_));
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. assert(false);
  182. return common::Value();
  183. }
  184. template<typename T>
  185. std::string to_string(const T *o) const
  186. {
  187. if (ptr_) {
  188. data<T, double> *q_double =
  189. dynamic_cast < data<T, double> * >(ptr_);
  190. if (q_double) {
  191. return std::to_string(o->*(q_double->value_));
  192. } else {
  193. data<T, unsigned int> *q_uint =
  194. dynamic_cast < data<T, unsigned int> * >(ptr_);
  195. if (q_uint) {
  196. return std::to_string(o->*(q_uint->value_));
  197. } else {
  198. data<T, int> *q_int =
  199. dynamic_cast < data<T, int> * >(ptr_);
  200. if (q_int) {
  201. return std::to_string(o->*(q_int->value_));
  202. } else {
  203. data<T, bool> *q_bool =
  204. dynamic_cast < data<T, bool> * >(ptr_);
  205. if (q_bool) {
  206. return o->*(q_bool->value_) ? "true" : "false";
  207. } else {
  208. data<T, std::vector < double> > *q_double_v =
  209. dynamic_cast < data<T, std::vector <
  210. double> > * > (ptr_);
  211. if (q_double_v) {
  212. return "";
  213. } else {
  214. data<T, std::vector < int> > *q_int_v =
  215. dynamic_cast < data<T, std::vector <
  216. int> > * > (ptr_);
  217. if (q_int_v) {
  218. return "";
  219. } else {
  220. data<T, std::vector < bool> > *q_bool_v =
  221. dynamic_cast < data<T, std::vector <
  222. bool> > * > (ptr_);
  223. if (q_bool_v) {
  224. return "";
  225. } else {
  226. return "NA";
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. } else {
  235. return "NA";
  236. }
  237. }
  238. };
  239. }
  240. }
  241. #endif