/**
* @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-2019 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 .
*/
#ifndef COMMON_VALUE
#define COMMON_VALUE
#include
#include
#include
#include
#include
#include
#include
#include
namespace artis {
namespace common {
class Value
{
public:
Value()
: _content(nullptr), _size(0)
{}
template
Value(const T &value)
{ assign(&value, sizeof(T), typeid(T).hash_code()); }
template
Value(const T *value, size_t size)
{ assign(value, sizeof(T) * size, typeid(T *).hash_code()); }
template
Value(const std::vector &value)
{
const T *v = value.data();
assign(v, sizeof(T) * value.size(), typeid(T *).hash_code());
}
Value(const std::vector &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) {
assign(value._content, value._size, value._type_id);
} else {
_content = nullptr;
_size = 0;
_type_id = 0;
}
}
virtual ~Value()
{
if (_content != nullptr)
delete[] _content;
}
bool is_null() const
{ return _content == nullptr; }
template
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
void operator()(T &value) const
{
assert(_type_id == typeid(T).hash_code());
value = *(T *) (_content);
}
template
void operator()(T *&value) const
{
assert(_type_id == typeid(T *).hash_code());
value = (T *) (_content);
}
template
void operator()(std::vector &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)
{
if (_content != nullptr) {
delete _content;
}
if (value._content) {
assign(value._content, value._size, value._type_id);
} else {
_content = nullptr;
_size = 0;
_type_id = 0;
}
return *this;
}
template
size_t size() const
{
assert(is_type());
return _size / sizeof(Z);
}
std::string to_string() const
{
if (is_null()) {
return "";
} else if (is_type()) {
double v;
operator()(v);
return std::to_string(v);
} else if (is_type()) {
int v;
operator()(v);
return std::to_string(v);
} else if (is_type()) {
unsigned int v;
operator()(v);
return std::to_string(v);
} else if (is_type()) {
bool v;
operator()(v);
return v ? "true" : "false";
} else if (is_type()) {
char v;
operator()(v);
return std::to_string(v);
} else if (is_type()) {
double *v;
std::string str;
size_t size = _size / sizeof(double);
operator()(v);
for (size_t i = 0; i < size; ++i) {
str += std::to_string(v[i]) + std::string(" ");
}
return str;
} else if (is_type()) {
int *v;
std::string str;
size_t size = _size / sizeof(int);
operator()(v);
for (size_t i = 0; i < size; ++i) {
str += std::to_string(v[i]) + std::string(" ");
}
return str;
} else if (is_type()) {
unsigned int *v;
std::string str;
size_t size = _size / sizeof(unsigned int);
operator()(v);
for (size_t i = 0; i < size; ++i) {
str += std::to_string(v[i]) + std::string(" ");
}
return str;
} else if (is_type()) {
bool *v;
std::string str;
size_t size = _size / sizeof(bool);
operator()(v);
for (size_t i = 0; i < size; ++i) {
str += std::to_string(v[i]) + std::string(" ");
}
return str;
} else if (is_type()) {
char *v;
operator()(v);
return std::string(v, _size / sizeof(char));
} else {
return "";
}
}
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
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(_content, _size);
ar & _type_id;
}
char *_content;
size_t _size;
size_t _type_id;
};
}
} // namespace artis common
#endif