123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- /**
- * @file artis/kernel/Value.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_VALUE_HPP
- #define __ARTIS_KERNEL_VALUE_HPP
- #include <boost/serialization/serialization.hpp>
- #include <boost/serialization/array.hpp>
- #include <algorithm>
- #include <cstring>
- #include <typeinfo>
- #include <iostream>
- 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 (this->ptr_) delete this->ptr_; }
- void operator=(Any const& other)
- { Any(other).swap(*this); }
- void swap(Any& other)
- { std::swap(this->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 >
- 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 {
- return "NA";
- }
- }
- }
- } else {
- return "NA";
- }
- }
- };
- class Value
- {
- public:
- Value() : _content(nullptr), _size(0)
- { }
- template < typename T >
- Value(T value)
- { assign(&value, sizeof(T), typeid(T).hash_code()); }
- Value(void* content, size_t size)
- { assign(content, size, typeid(void*).hash_code()); }
- Value(const char* value, unsigned int size)
- { assign(value, size * sizeof(char), typeid(char*).hash_code()); }
- virtual ~Value()
- { /* if (_content) delete[] _content; */ }
- bool empty() const
- { return _content == nullptr; }
- template < typename T >
- void get_content(T& value) const
- {
- assert(_type_id == typeid(T).hash_code());
- value = *(T*)(_content);
- }
- std::string to_string() const
- {
- return std::string();
- }
- private:
- void assign(const void* content, size_t size, size_t type_id)
- {
- _content = new char[size];
- std::memcpy(_content, content, size);
- _size = size;
- _type_id = type_id;
- }
- friend class boost::serialization::access;
- template < class Archive >
- void serialize(Archive & ar, const unsigned int version)
- {
- (void) version;
- ar & _size;
- if (Archive::is_loading::value) {
- assert(_content == nullptr);
- _content = new char[_size];
- }
- ar & boost::serialization::make_array < char >(_content, _size);
- ar & _type_id;
- }
- char* _content;
- size_t _size;
- size_t _type_id;
- };
- } }
- #endif
|