123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- /**
- * @file common/Value.hpp
- * @author The ARTIS Development Team
- * See the AUTHORS or Authors.txt file
- */
- /*
- * ARTIS - the multimodeling and simulation environment
- * This file is a part of the ARTIS environment
- *
- * Copyright (C) 2013-2021 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 COMMON_EVENT_VALUE
- #define COMMON_EVENT_VALUE
- #include <boost/serialization/serialization.hpp>
- #include <boost/serialization/array.hpp>
- #include <cassert>
- #include <cstring>
- #include <functional>
- #include <map>
- #include <numeric>
- #include <string>
- #include <typeinfo>
- #include <vector>
- namespace artis::common::event {
- class Value {
- public:
- Value()
- : _content(nullptr), _size(0), _type_id(0) {}
- template<typename T>
- Value(const T &value) { assign(&value, sizeof(T), typeid(T).hash_code()); }
- template<typename T>
- Value(const T *value, size_t size) { assign(value, sizeof(T) * size, typeid(T *).hash_code()); }
- template<typename T>
- Value(const std::vector<T> &value) {
- const T *v = value.data();
- assign(v, sizeof(T) * value.size(), typeid(T *).hash_code());
- }
- Value(const std::vector<bool> &value) {
- size_t size = sizeof(bool) * value.size();
- _content = new char[size];
- for (size_t i = 0; i < value.size(); ++i) {
- _content[i] = value[i];
- }
- _size = size;
- _type_id = typeid(bool *).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()); }
- Value(const Value &value) {
- if (value._content != nullptr) {
- assign(value._content, value._size, value._type_id);
- } else {
- _content = nullptr;
- _size = 0;
- _type_id = 0;
- }
- }
- Value(Value &&value) : _content(value._content), _size(value._size), _type_id(value._type_id) {
- value._content = nullptr;
- value._size = 0;
- value._type_id = 0;
- }
- virtual ~Value() {
- delete[] _content;
- }
- bool is_null() const { return _content == nullptr; }
- template<typename Z>
- bool is_type() const { return _type_id == typeid(Z).hash_code(); }
- bool operator==(const Value &other) const {
- return _type_id == other._type_id and _size == other._size
- and std::memcmp(_content, other._content, _size) == 0;
- }
- template<typename T>
- void operator()(T &value) const {
- assert(_type_id == typeid(T).hash_code());
- value = *(T *) (_content);
- }
- template<typename T>
- void operator()(T *&value) const {
- assert(_type_id == typeid(T *).hash_code());
- value = (T *) (_content);
- }
- template<typename T>
- void operator()(std::vector<T> &value) const {
- assert(_type_id == typeid(T *).hash_code());
- size_t size = _size / sizeof(T);
- for (size_t i = 0; i < size; ++i) {
- value.push_back(((T *) (_content))[i]);
- }
- }
- Value &operator=(const Value &value) {
- delete[] _content;
- if (value._content != nullptr) {
- assign(value._content, value._size, value._type_id);
- } else {
- _content = nullptr;
- _size = 0;
- _type_id = 0;
- }
- return *this;
- }
- Value &operator=(Value &&value) {
- delete[] _content;
- _content = value._content;
- value._content = nullptr;
- _size = value._size;
- value._size = 0;
- _type_id = value._type_id;
- value._type_id = 0;
- return *this;
- }
- template<typename Z>
- size_t size() const {
- assert(is_type<Z *>());
- return _size / sizeof(Z);
- }
- std::string to_string() const {
- if (is_null()) {
- return "<null>";
- }
- auto it = convert.find(_type_id);
- if (it != convert.cend()) {
- return it->second(*this);
- }
- return "<unstringify>";
- }
- private:
- typedef std::map<unsigned long, std::function<std::string(const artis::common::event::Value &)>> ConvertFunctions;
- static const ConvertFunctions convert;
- 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<typename 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 = nullptr;
- size_t _size = 0;
- size_t _type_id = 0;
- };
- std::ostream& operator<<(std::ostream& o, const Value& value) {
- o << value.to_string();
- return o;
- }
- const Value::ConvertFunctions Value::convert = {
- {
- typeid(double).hash_code(), [](const Value &value) {
- double v;
- value.operator()(v);
- return std::to_string(v);
- }},
- {
- typeid(int).hash_code(), [](const Value &value) {
- int v;
- value.operator()(v);
- return std::to_string(v);
- }},
- {
- typeid(unsigned int).hash_code(), [](const Value &value) {
- unsigned int v;
- value.operator()(v);
- return std::to_string(v);
- }},
- {
- typeid(bool).hash_code(), [](const Value &value) {
- bool v;
- value.operator()(v);
- return v ? "true" : "false";
- }},
- {
- typeid(char).hash_code(), [](const Value &value) {
- char v;
- value.operator()(v);
- return std::string(&v, 1);
- }},
- {
- typeid(double *).hash_code(), [](const Value &value) {
- double *v;
- std::string str;
- size_t size = value._size / sizeof(double);
- value.operator()(v);
- for (size_t i = 0; i < size; ++i) {
- str += std::to_string(v[i]) + std::string(" ");
- }
- return str;
- }},
- {
- typeid(int *).hash_code(), [](const Value &value) {
- int *v;
- std::string str;
- size_t size = value._size / sizeof(int);
- value.operator()(v);
- for (size_t i = 0; i < size; ++i) {
- str += std::to_string(v[i]) + std::string(" ");
- }
- return str;
- }},
- {
- typeid(unsigned int *).hash_code(), [](const Value &value) {
- unsigned int *v;
- std::string str;
- size_t size = value._size / sizeof(unsigned int);
- value.operator()(v);
- for (size_t i = 0; i < size; ++i) {
- str += std::to_string(v[i]) + std::string(" ");
- }
- return str;
- }},
- {
- typeid(bool *).hash_code(), [](const Value &value) {
- bool *v;
- std::string str;
- size_t size = value._size / sizeof(bool);
- value.operator()(v);
- for (size_t i = 0; i < size; ++i) {
- str += (v[i] ? "true" : "false") + std::string(" ");
- }
- return str;
- }},
- {
- typeid(char *).hash_code(), [](const Value &value) {
- char *v;
- value.operator()(v);
- return std::string(v, value._size / sizeof(char));
- }
- }};
- } // namespace artis common
- #endif
|