|
@@ -28,183 +28,170 @@
|
|
#define COMMON_EVENT_VALUE
|
|
#define COMMON_EVENT_VALUE
|
|
|
|
|
|
#include <boost/serialization/serialization.hpp>
|
|
#include <boost/serialization/serialization.hpp>
|
|
-#include <boost/serialization/array.hpp>
|
|
|
|
|
|
|
|
-#include <cassert>
|
|
|
|
-#include <cstring>
|
|
|
|
-#include <functional>
|
|
|
|
-#include <map>
|
|
|
|
-#include <numeric>
|
|
|
|
|
|
+#include <any>
|
|
|
|
+#include <memory>
|
|
|
|
+#include <sstream>
|
|
#include <string>
|
|
#include <string>
|
|
-#include <typeinfo>
|
|
|
|
|
|
+#include <type_traits>
|
|
|
|
+#include <valarray>
|
|
#include <vector>
|
|
#include <vector>
|
|
|
|
|
|
namespace artis::common::event {
|
|
namespace artis::common::event {
|
|
|
|
|
|
|
|
+template<typename T>
|
|
|
|
+struct HasToString {
|
|
|
|
+ template<typename U, std::string (U::*)() const>
|
|
|
|
+ struct SFINAE {
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ template<typename U>
|
|
|
|
+ static char Test(SFINAE<U, &U::to_string> *);
|
|
|
|
+
|
|
|
|
+ template<typename U>
|
|
|
|
+ static int Test(...);
|
|
|
|
+
|
|
|
|
+ static const bool Has = sizeof(Test<T>(0)) == sizeof(char);
|
|
|
|
+};
|
|
|
|
+
|
|
class Value {
|
|
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()); }
|
|
|
|
|
|
+ class Base {
|
|
|
|
+ public:
|
|
|
|
+ virtual ~Base() = default;
|
|
|
|
|
|
- template<typename T>
|
|
|
|
- Value(const T *value, size_t size) { assign(value, sizeof(T) * size, typeid(T *).hash_code()); }
|
|
|
|
|
|
+ virtual bool operator==(const Base &other) const = 0;
|
|
|
|
|
|
- template<typename T>
|
|
|
|
- Value(const std::vector<T> &value) {
|
|
|
|
- const T *v = value.data();
|
|
|
|
|
|
+ virtual std::string to_string() const = 0;
|
|
|
|
|
|
- assign(v, sizeof(T) * value.size(), typeid(T *).hash_code());
|
|
|
|
- }
|
|
|
|
|
|
+ virtual const std::type_info &type() const noexcept = 0;
|
|
|
|
+ };
|
|
|
|
|
|
- Value(const std::vector<bool> &value) {
|
|
|
|
- size_t size = sizeof(bool) * value.size();
|
|
|
|
|
|
+ template<typename T>
|
|
|
|
+ class Data : public Base, public std::any {
|
|
|
|
+ public:
|
|
|
|
+ template<typename U = T, std::enable_if_t<not std::is_same_v<U, std::string>, bool> = true>
|
|
|
|
+ Data(const U &value) : std::any(value) {}
|
|
|
|
|
|
- _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();
|
|
|
|
- }
|
|
|
|
|
|
+ Data(const std::string &value) : std::any(value) {}
|
|
|
|
|
|
- Value(void *content, size_t size) { assign(content, size, typeid(void *).hash_code()); }
|
|
|
|
|
|
+ Data(const std::valarray<T> &value) : std::any(value) {}
|
|
|
|
|
|
- Value(const char *value, unsigned int size) { assign(value, size * sizeof(char), typeid(char *).hash_code()); }
|
|
|
|
|
|
+ Data(const std::vector<T> &value) : std::any(value) {}
|
|
|
|
|
|
- Value(const Value &value) {
|
|
|
|
- if (value._content != nullptr) {
|
|
|
|
- assign(value._content, value._size, value._type_id);
|
|
|
|
- } else {
|
|
|
|
- _content = nullptr;
|
|
|
|
- _size = 0;
|
|
|
|
- _type_id = 0;
|
|
|
|
|
|
+ bool operator==(const Base &other) const override {
|
|
|
|
+ return type() == other.type() and
|
|
|
|
+ std::any_cast<const T &>(*this) == std::any_cast<const T &>(*dynamic_cast<const Data<T> *>(&other));
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- Value(Value &&value) : _content(value._content), _size(value._size), _type_id(value._type_id) {
|
|
|
|
- value._content = nullptr;
|
|
|
|
- value._size = 0;
|
|
|
|
- value._type_id = 0;
|
|
|
|
- }
|
|
|
|
|
|
+ template<typename U>
|
|
|
|
+ void operator()(U &value) const {
|
|
|
|
+ value = std::any_cast<const U &>(*this);
|
|
|
|
+ }
|
|
|
|
|
|
- virtual ~Value() {
|
|
|
|
- delete[] _content;
|
|
|
|
- }
|
|
|
|
|
|
+ std::string to_string() const override {
|
|
|
|
+ std::stringstream ss;
|
|
|
|
+
|
|
|
|
+ if constexpr (HasToString<T>::Has) {
|
|
|
|
+ if (type() == typeid(std::vector<T>)) {
|
|
|
|
+ const std::vector<T> &values = std::any_cast<const std::vector<T> &>(*this);
|
|
|
|
+
|
|
|
|
+ for (const auto &e: values) {
|
|
|
|
+ ss << std::boolalpha << e.to_string() << " ";
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ ss << std::any_cast<const T &>(*this).to_string();
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (type() == typeid(std::string)) {
|
|
|
|
+ ss << std::any_cast<const std::string &>(*this);
|
|
|
|
+ } else if (type() == typeid(std::valarray<T>)) {
|
|
|
|
+ const std::valarray<T> &values = std::any_cast<const std::valarray<T> &>(*this);
|
|
|
|
+
|
|
|
|
+ for (const auto &e: values) {
|
|
|
|
+ ss << std::boolalpha << e << " ";
|
|
|
|
+ }
|
|
|
|
+ } else if (type() == typeid(std::vector<T>)) {
|
|
|
|
+ const std::vector<T> &values = std::any_cast<const std::vector<T> &>(*this);
|
|
|
|
+
|
|
|
|
+ for (const auto &e: values) {
|
|
|
|
+ ss << std::boolalpha << e << " ";
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ ss << std::boolalpha << std::any_cast<const T &>(*this);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return ss.str();
|
|
|
|
+ }
|
|
|
|
|
|
- bool is_null() const { return _content == nullptr; }
|
|
|
|
|
|
+ const std::type_info &type() const noexcept override { return std::any::type(); }
|
|
|
|
|
|
- template<typename Z>
|
|
|
|
- bool is_type() const { return _type_id == typeid(Z).hash_code(); }
|
|
|
|
|
|
+ private:
|
|
|
|
+ using _type = T;
|
|
|
|
+ };
|
|
|
|
|
|
- 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 {
|
|
|
|
|
|
+public:
|
|
|
|
+ Value() : _data(nullptr) {}
|
|
|
|
|
|
- assert(_type_id == typeid(T).hash_code());
|
|
|
|
|
|
+ Value(const Value& value) : _data(value._data) {}
|
|
|
|
|
|
- value = *(T *) (_content);
|
|
|
|
- }
|
|
|
|
|
|
+ template<typename T, std::enable_if_t<not HasToString<T>::Has, bool> = true>
|
|
|
|
+ Value(const T &value) : _data(new Data<T>(value)) {}
|
|
|
|
|
|
template<typename T>
|
|
template<typename T>
|
|
- void operator()(T *&value) const {
|
|
|
|
|
|
+ Value(const std::vector<T> &value) : _data(new Data<T>(value)) {}
|
|
|
|
|
|
- assert(_type_id == typeid(T *).hash_code());
|
|
|
|
|
|
+ template<typename T, std::enable_if_t<HasToString<T>::Has, bool> = true>
|
|
|
|
+ Value(const T &value) : _data(new Data<T>(value)) {}
|
|
|
|
|
|
- value = (T *) (_content);
|
|
|
|
- }
|
|
|
|
|
|
+ template<typename T, size_t size, std::enable_if_t<std::is_same<T, char>::value, bool> = true>
|
|
|
|
+ Value(const T (&value)[size]) : _data(new Data<T>(std::string(value, size))) {}
|
|
|
|
|
|
template<typename T>
|
|
template<typename T>
|
|
- void operator()(std::vector<T> &value) const {
|
|
|
|
- assert(_type_id == typeid(T *).hash_code());
|
|
|
|
|
|
+ Value(const T *value, size_t size) : _data(new Data<T>(std::valarray<T>(value, size))) {}
|
|
|
|
|
|
- size_t size = _size / sizeof(T);
|
|
|
|
|
|
+ bool is_null() const { return _data.get() == nullptr; }
|
|
|
|
|
|
- for (size_t i = 0; i < size; ++i) {
|
|
|
|
- value.push_back(((T *) (_content))[i]);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ template<typename Z>
|
|
|
|
+ bool is_type() const { return _data->type() == typeid(Z); }
|
|
|
|
|
|
- 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;
|
|
|
|
- }
|
|
|
|
|
|
+ Value& operator=(const Value &other) {
|
|
|
|
+ _data = other._data;
|
|
return *this;
|
|
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;
|
|
|
|
|
|
+ bool operator==(const Value &other) const {
|
|
|
|
+ return _data->operator==(*other._data);
|
|
}
|
|
}
|
|
|
|
|
|
- template<typename Z>
|
|
|
|
- size_t size() const {
|
|
|
|
|
|
+ template<typename T>
|
|
|
|
+ void operator()(T &value) const {
|
|
|
|
+ dynamic_cast<Data<T> *>(_data.get())->operator()(value);
|
|
|
|
+ }
|
|
|
|
|
|
- assert(is_type<Z *>());
|
|
|
|
|
|
+ template<typename T>
|
|
|
|
+ void operator()(std::vector<T> &value) const {
|
|
|
|
+ dynamic_cast<Data<T> *>(_data.get())->operator()(value);
|
|
|
|
+ }
|
|
|
|
|
|
- return _size / sizeof(Z);
|
|
|
|
|
|
+ template<typename T>
|
|
|
|
+ void operator()(std::valarray<T> &value) const {
|
|
|
|
+ dynamic_cast<Data<T> *>(_data.get())->operator()(value);
|
|
}
|
|
}
|
|
|
|
|
|
std::string to_string() const {
|
|
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>";
|
|
|
|
|
|
+ return _data->to_string();
|
|
}
|
|
}
|
|
|
|
|
|
private:
|
|
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;
|
|
friend class boost::serialization::access;
|
|
|
|
|
|
template<typename Archive>
|
|
template<typename Archive>
|
|
void serialize(Archive &ar, const unsigned int version) {
|
|
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;
|
|
|
|
|
|
+ // TODO
|
|
}
|
|
}
|
|
|
|
|
|
- char *_content = nullptr;
|
|
|
|
- size_t _size = 0;
|
|
|
|
- size_t _type_id = 0;
|
|
|
|
|
|
+ std::shared_ptr<Base> _data;
|
|
};
|
|
};
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& o, const artis::common::event::Value& value);
|
|
std::ostream& operator<<(std::ostream& o, const artis::common::event::Value& value);
|