123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- /**
- * @file artis/kernel/Any.hpp
- * @author See the AUTHORS file
- */
- /*
- * Copyright (C) 2012-2017 ULCO http://www.univ-littoral.fr
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef __ARTIS_KERNEL_ANY_HPP
- #define __ARTIS_KERNEL_ANY_HPP
- #include <artis/context/Value.hpp>
- #include <string>
- #include <vector>
- namespace artis { namespace kernel {
- enum ValueType { DOUBLE, INT, BOOL };
- class Any
- {
- private:
- struct base
- {
- virtual ~base() { }
- virtual base* clone() const = 0;
- };
- template < typename T, typename V >
- struct data : base
- {
- data(V T::* const& value) : value_(value)
- { }
- base* clone() const { return new data < T, V >(*this); }
- V T::* value_;
- };
- base* ptr_;
- public:
- Any() : ptr_(nullptr)
- { }
- template < typename T, typename V >
- Any(V T::* const& value) : ptr_(new data < T, V >(value))
- { }
- Any(Any const& other) : ptr_(other.ptr_? other.ptr_->clone() : nullptr)
- { }
- virtual ~Any()
- { if (ptr_) delete ptr_; }
- void operator=(Any const& other)
- { Any(other).swap(*this); }
- void swap(Any& other)
- { std::swap(ptr_, other.ptr_); }
- template < typename T, typename V >
- V T::* get() const
- { return dynamic_cast < data < T, V >* >(ptr_)->value_; }
- bool is_null() const
- { return ptr_ == nullptr; }
- template < typename T, typename V >
- void put(T* o, const V& value )
- {
- V T::* p = dynamic_cast <
- data < T, V >* >(ptr_)->value_;
- o->*(p) = value;
- }
- template < typename T >
- void restore(T* o, const context::Value& value)
- {
- if (value.is_double()) {
- double v;
- value.get_content(v);
- put(o, v);
- } else if (value.is_int()) {
- int v;
- value.get_content(v);
- put(o, v);
- } else if (value.is_bool()) {
- bool v;
- value.get_content(v);
- put(o, v);
- } else if (value.is_double_vector()) {
- std::vector < double > v;
- value.get_content(v);
- put(o, v);
- } else if (value.is_int_vector()) {
- std::vector < int > v;
- value.get_content(v);
- put(o, v);
- } else if (value.is_bool_vector()) {
- std::vector < bool > v;
- value.get_content(v);
- put(o, v);
- } else {
- assert(false);
- }
- }
- template < typename T >
- context::Value save(const T* o) const
- {
- if (ptr_) {
- data < T, double >* q_double =
- dynamic_cast < data < T, double >* >(ptr_);
- if (q_double) {
- return context::Value(o->*(q_double->value_));
- } else {
- data < T, int >* q_int =
- dynamic_cast < data < T, int >* >(ptr_);
- if (q_int) {
- return context::Value(o->*(q_int->value_));
- } else {
- data < T, bool >* q_bool =
- dynamic_cast < data < T, bool >* >(ptr_);
- if (q_bool) {
- return context::Value(o->*(q_bool->value_));
- } else {
- data < T, std::vector < double > >* q_double_v =
- dynamic_cast < data < T, std::vector <
- double > >* >(ptr_);
- if (q_double_v) {
- return context::Value(o->*(q_double_v->value_));
- } else {
- data < T, std::vector < int > >* q_int_v =
- dynamic_cast < data < T, std::vector <
- int > >* >(ptr_);
- if (q_int_v) {
- return context::Value(o->*(q_int_v->value_));
- } else {
- data < T, std::vector < bool > >* q_bool_v =
- dynamic_cast < data < T, std::vector <
- bool > >* >(ptr_);
- if (q_bool_v) {
- return context::Value(
- o->*(q_bool_v->value_));
- }
- }
- }
- }
- }
- }
- }
- assert(false);
- }
- template < typename T >
- std::string to_string(const T* o) const
- {
- if (ptr_) {
- data < T, double >* q_double =
- dynamic_cast < data < T, double >* >(ptr_);
- if (q_double) {
- return std::to_string(o->*(q_double->value_));
- } else {
- data < T, int >* q_int =
- dynamic_cast < data < T, int >* >(ptr_);
- if (q_int) {
- return std::to_string(o->*(q_int->value_));
- } else {
- data < T, bool >* q_bool =
- dynamic_cast < data < T, bool >* >(ptr_);
- if (q_bool) {
- return o->*(q_bool->value_) ? "true": "false";
- } else {
- data < T, std::vector < double > >* q_double_v =
- dynamic_cast < data < T, std::vector <
- double > >* >(ptr_);
- if (q_double_v) {
- return "";
- } else {
- data < T, std::vector < int > >* q_int_v =
- dynamic_cast < data < T, std::vector <
- int > >* >(ptr_);
- if (q_int_v) {
- return "";
- } else {
- data < T, std::vector < bool > >* q_bool_v =
- dynamic_cast < data < T, std::vector <
- bool > >* >(ptr_);
- if (q_bool_v) {
- return "";
- } else {
- return "NA";
- }
- }
- }
- }
- }
- }
- } else {
- return "NA";
- }
- }
- };
- } }
- #endif
|