Any.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**
  2. * @file artis/kernel/Any.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2017 ULCO http://www.univ-littoral.fr
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef __ARTIS_KERNEL_ANY_HPP
  22. #define __ARTIS_KERNEL_ANY_HPP
  23. #include <artis/context/Value.hpp>
  24. #include <string>
  25. #include <tuple>
  26. #include <utility>
  27. #include <vector>
  28. #include <iostream>
  29. namespace artis { namespace kernel {
  30. enum ValueTypeID { DOUBLE, INT, BOOL, DOUBLE_VECTOR, INT_VECTOR, BOOL_VECTOR,
  31. STRING, USER };
  32. // template < typename H, typename T >
  33. // struct typelist
  34. // {
  35. // typedef H head;
  36. // typedef T tail;
  37. // };
  38. // struct null_typelist
  39. // { };
  40. // template < typename Fun >
  41. // struct cons;
  42. // template < typename T1 >
  43. // struct cons < void (*)(T1)>
  44. // {
  45. // typedef typelist < T1, null_typelist > type;
  46. // };
  47. // template < typename T1, typename T2 >
  48. // struct cons < void (*)(T1, T2)>
  49. // {
  50. // typedef typelist < T1, typelist < T2, null_typelist > > type;
  51. // };
  52. // template < typename T1, typename T2, typename T3 >
  53. // struct cons < void (*)(T1, T2, T3)>
  54. // {
  55. // typedef typelist < T1, typelist < T2, typelist < T3,
  56. // null_typelist > > > type;
  57. // };
  58. // #define TYPELIST(a) cons < void (*)a >::type
  59. // typedef TYPELIST((double, int, bool)) value_types;
  60. class Any
  61. {
  62. private:
  63. struct base
  64. {
  65. virtual ~base() { }
  66. virtual base* clone() const = 0;
  67. };
  68. template < typename T, typename V >
  69. struct data : base
  70. {
  71. data(V T::* const& value) : value_(value)
  72. { }
  73. base* clone() const { return new data < T, V >(*this); }
  74. V T::* value_;
  75. };
  76. base* ptr_;
  77. template < typename V >
  78. friend struct ValueType;
  79. public:
  80. Any() : ptr_(nullptr)
  81. { }
  82. template < typename T, typename V >
  83. Any(V T::* const& value) : ptr_(new data < T, V >(value))
  84. { }
  85. Any(Any const& other) : ptr_(other.ptr_? other.ptr_->clone() : nullptr)
  86. { }
  87. virtual ~Any()
  88. { if (ptr_) delete ptr_; }
  89. void operator=(Any const& other)
  90. { Any(other).swap(*this); }
  91. void swap(Any& other)
  92. { std::swap(ptr_, other.ptr_); }
  93. template < typename T, typename V >
  94. V T::* get() const
  95. { return dynamic_cast < data < T, V >* >(ptr_)->value_; }
  96. bool is_null() const
  97. { return ptr_ == nullptr; }
  98. template < typename T, typename V >
  99. void put(T* o, const V& value )
  100. {
  101. V T::* p = dynamic_cast <
  102. data < T, V >* >(ptr_)->value_;
  103. o->*(p) = value;
  104. }
  105. template < typename T >
  106. void restore(T* o, const context::Value& value)
  107. {
  108. if (value.is_type < double >()) {
  109. double v;
  110. value.get_content(v);
  111. put(o, v);
  112. } else if (value.is_type < int >()) {
  113. int v;
  114. value.get_content(v);
  115. put(o, v);
  116. } else if (value.is_type < bool >()) {
  117. bool v;
  118. value.get_content(v);
  119. put(o, v);
  120. } else if (value.is_type < std::vector < double > >()) {
  121. std::vector < double > v;
  122. value.get_content(v);
  123. put(o, v);
  124. } else if (value.is_type < std::vector < int > >()) {
  125. std::vector < int > v;
  126. value.get_content(v);
  127. put(o, v);
  128. } else if (value.is_type < std::vector < bool > >()) {
  129. std::vector < bool > v;
  130. value.get_content(v);
  131. put(o, v);
  132. } else {
  133. assert(false);
  134. }
  135. }
  136. template < typename T >
  137. context::Value save(const T* o) const
  138. {
  139. if (ptr_) {
  140. data < T, double >* q_double =
  141. dynamic_cast < data < T, double >* >(ptr_);
  142. if (q_double) {
  143. return context::Value(o->*(q_double->value_));
  144. } else {
  145. data < T, int >* q_int =
  146. dynamic_cast < data < T, int >* >(ptr_);
  147. if (q_int) {
  148. return context::Value(o->*(q_int->value_));
  149. } else {
  150. data < T, bool >* q_bool =
  151. dynamic_cast < data < T, bool >* >(ptr_);
  152. if (q_bool) {
  153. return context::Value(o->*(q_bool->value_));
  154. } else {
  155. data < T, std::vector < double > >* q_double_v =
  156. dynamic_cast < data < T, std::vector <
  157. double > >* >(ptr_);
  158. if (q_double_v) {
  159. return context::Value(o->*(q_double_v->value_));
  160. } else {
  161. data < T, std::vector < int > >* q_int_v =
  162. dynamic_cast < data < T, std::vector <
  163. int > >* >(ptr_);
  164. if (q_int_v) {
  165. return context::Value(o->*(q_int_v->value_));
  166. } else {
  167. data < T, std::vector < bool > >* q_bool_v =
  168. dynamic_cast < data < T, std::vector <
  169. bool > >* >(ptr_);
  170. if (q_bool_v) {
  171. return context::Value(
  172. o->*(q_bool_v->value_));
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. assert(false);
  181. }
  182. template < typename T >
  183. std::string to_string(const T* o) const
  184. {
  185. if (ptr_) {
  186. data < T, double >* q_double =
  187. dynamic_cast < data < T, double >* >(ptr_);
  188. if (q_double) {
  189. return std::to_string(o->*(q_double->value_));
  190. } else {
  191. data < T, int >* q_int =
  192. dynamic_cast < data < T, int >* >(ptr_);
  193. if (q_int) {
  194. return std::to_string(o->*(q_int->value_));
  195. } else {
  196. data < T, bool >* q_bool =
  197. dynamic_cast < data < T, bool >* >(ptr_);
  198. if (q_bool) {
  199. return o->*(q_bool->value_) ? "true": "false";
  200. } else {
  201. data < T, std::vector < double > >* q_double_v =
  202. dynamic_cast < data < T, std::vector <
  203. double > >* >(ptr_);
  204. if (q_double_v) {
  205. return "";
  206. } else {
  207. data < T, std::vector < int > >* q_int_v =
  208. dynamic_cast < data < T, std::vector <
  209. int > >* >(ptr_);
  210. if (q_int_v) {
  211. return "";
  212. } else {
  213. data < T, std::vector < bool > >* q_bool_v =
  214. dynamic_cast < data < T, std::vector <
  215. bool > >* >(ptr_);
  216. if (q_bool_v) {
  217. return "";
  218. } else {
  219. return "NA";
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. } else {
  227. return "NA";
  228. }
  229. }
  230. };
  231. // template < typename C,
  232. // typename T,
  233. // typename Seq = std::make_integer_sequence
  234. // < int, std::tuple_size < T >::value > >
  235. // struct TupleIterator;
  236. // template < typename C,
  237. // typename T,
  238. // int... S >
  239. // struct TupleIterator < C, T, std::integer_sequence < int, S... > >
  240. // {
  241. // TupleIterator(C caller, const T& val)
  242. // { result = std::make_tuple(caller(std::get < S >(val))...); }
  243. // std::tuple < context::Value, context::Value, context::Value > result;
  244. // };
  245. // template < typename C, typename T >
  246. // TupleIterator < C, T > tuple_iterator(C caller, const T& val)
  247. // {
  248. // return TupleIterator < C, T >(caller, val);
  249. // }
  250. // template < typename T >
  251. // struct ToValueCaller
  252. // {
  253. // ToValueCaller(const Any& any, const T* o) : _any(any), _object(o)
  254. // { }
  255. // template < typename Z >
  256. // context::Value operator()(const Z& data) const
  257. // { return data.to_value(_any, _object); }
  258. // const Any& _any;
  259. // const T* _object;
  260. // };
  261. // template < typename V >
  262. // struct ValueType
  263. // {
  264. // template < typename T >
  265. // context::Value to_value(const Any& any, const T* o) const
  266. // {
  267. // Any::data < T, V >* q = dynamic_cast < Any::data < T, V >* >(any.ptr_);
  268. // if (q) {
  269. // return context::Value(o->*(q->value_));
  270. // } else {
  271. // return context::Value();
  272. // }
  273. // }
  274. // };
  275. // template < typename Tuple, typename F, size_t... Is >
  276. // constexpr auto apply_impl(Tuple t, F f, std::index_sequence < Is... >)
  277. // { return f(std::get < Is >(t)...); }
  278. // template < typename Tuple, typename F >
  279. // constexpr auto apply(Tuple t, F f)
  280. // { return apply_impl(t, f, std::make_index_sequence <
  281. // std::tuple_size < Tuple >{}>{}); }
  282. // struct find_not_null_value
  283. // {
  284. // context::Value operator()(const context::Value& left,
  285. // const context::Value& right) const;
  286. // };
  287. // template < typename T >
  288. // static context::Value to_value(const Any& any, const T* o)
  289. // {
  290. // auto value_types =
  291. // std::make_tuple(ValueType < double >(),
  292. // ValueType < int >(),
  293. // ValueType < bool >());
  294. // auto list = tuple_iterator(ToValueCaller < T >(any, o), value_types);
  295. // auto result = apply(list.result, find_not_null_value{});
  296. // std::cout << result.to_string() << std::endl;
  297. // assert(false);
  298. // }
  299. } }
  300. #endif