Any.hpp 7.0 KB

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