Commit 123e61f7 authored by neverlord's avatar neverlord

serialization tests

parent cc1a1fc6
......@@ -6,5 +6,6 @@ test
*.png
*.dat
queue_test
cppa.creator.user.1.3
8threads
4threads
......@@ -2,7 +2,7 @@
<qtcreator>
<data>
<variable>GenericProjectManager.GenericProject.Toolchain</variable>
<value type="QString"></value>
<value type="QString">ProjectExplorer.ToolChain.Gcc:/opt/local/bin/g++-mp-4.6.x86-macos-generic-mach_o-64bit.</value>
</data>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
......@@ -43,7 +43,7 @@
<value key="ProjectExplorer.Target.ActiveRunConfiguration" type="int">0</value>
<valuemap key="ProjectExplorer.Target.BuildConfiguration.0" type="QVariantMap">
<value key="GenericProjectManager.GenericBuildConfiguration.BuildDirectory" type="QString">/Users/neverlord/libcppa</value>
<value key="ProjectExplorer.BuildCOnfiguration.ToolChain" type="QString">INVALID</value>
<value key="ProjectExplorer.BuildCOnfiguration.ToolChain" type="QString">ProjectExplorer.ToolChain.Gcc:/usr/bin/g++.x86-macos-generic-mach_o-64bit.</value>
<valuemap key="ProjectExplorer.BuildConfiguration.BuildStepList.0" type="QVariantMap">
<valuemap key="ProjectExplorer.BuildStepList.Step.0" type="QVariantMap">
<valuelist key="GenericProjectManager.GenericMakeStep.BuildTargets" type="QVariantList">
......@@ -77,7 +77,7 @@
<valuemap key="ProjectExplorer.Target.DeployConfiguration.0" type="QVariantMap">
<valuemap key="ProjectExplorer.BuildConfiguration.BuildStepList.0" type="QVariantMap">
<value key="ProjectExplorer.BuildStepList.StepsCount" type="int">0</value>
<value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">Deployment</value>
<value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">Deploy</value>
<value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString">Deploy</value>
<value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">ProjectExplorer.BuildSteps.Deploy</value>
</valuemap>
......@@ -115,7 +115,9 @@
<value key="ProjectExplorer.CustomExecutableRunConfiguration.BaseEnvironmentBase" type="int">2</value>
<value key="ProjectExplorer.CustomExecutableRunConfiguration.Executable" type="QString">/Users/neverlord/libcppa/test</value>
<value key="ProjectExplorer.CustomExecutableRunConfiguration.UseTerminal" type="bool">false</value>
<valuelist key="ProjectExplorer.CustomExecutableRunConfiguration.UserEnvironmentChanges" type="QVariantList"/>
<valuelist key="ProjectExplorer.CustomExecutableRunConfiguration.UserEnvironmentChanges" type="QVariantList">
<value type="QString">DYLD_LIBRARY_PATH=/Users/neverlord/libcppa</value>
</valuelist>
<value key="ProjectExplorer.CustomExecutableRunConfiguration.WorkingDirectory" type="QString">%{buildDir}</value>
<value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">Führe /Users/neverlord/libcppa/test aus</value>
<value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString">test</value>
......@@ -133,7 +135,7 @@
</data>
<data>
<variable>ProjectExplorer.Project.Updater.EnvironmentId</variable>
<value type="QString">{349f52bc-ac64-4a14-898f-d44d989b22a2}</value>
<value type="QString">{07fcd197-092d-45a0-8500-3be614e6ae31}</value>
</data>
<data>
<variable>ProjectExplorer.Project.Updater.FileVersion</variable>
......
......@@ -118,3 +118,6 @@ src/to_uniform_name.cpp
cppa/detail/default_uniform_type_info_impl.hpp
src/object.cpp
cppa/util/comparable.hpp
cppa/util/disable_if.hpp
cppa/util/if_else_type.hpp
cppa/util/wrapped_type.hpp
......@@ -47,311 +47,311 @@ uniform_type_info* uniform_typeid(const std::type_info& tinfo);
class uniform_type_info : cppa::util::comparable<uniform_type_info>
{
friend class object;
friend class object;
// needs access to by_type_info()
template<typename T>
friend uniform_type_info* uniform_typeid();
// needs access to by_type_info()
template<typename T>
friend uniform_type_info* uniform_typeid();
friend uniform_type_info* uniform_typeid(const std::type_info&);
friend uniform_type_info* uniform_typeid(const std::type_info&);
public:
class identifier : cppa::util::comparable<identifier>
{
class identifier : cppa::util::comparable<identifier>
{
friend class uniform_type_info;
friend class uniform_type_info;
int m_value;
int m_value;
identifier(int val) : m_value(val) { }
identifier(int val) : m_value(val) { }
// enable copy and move constructors
identifier(identifier&&) = default;
identifier(const identifier&) = default;
// enable copy and move constructors
identifier(identifier&&) = default;
identifier(const identifier&) = default;
// disable assignment operators
identifier& operator=(identifier&&) = delete;
identifier& operator=(const identifier&) = delete;
// disable assignment operators
identifier& operator=(identifier&&) = delete;
identifier& operator=(const identifier&) = delete;
public:
public:
// needed by cppa::detail::comparable<identifier>
inline int compare(const identifier& other) const
{
return m_value - other.m_value;
}
// needed by cppa::detail::comparable<identifier>
inline int compare(const identifier& other) const
{
return m_value - other.m_value;
}
};
};
private:
// unique identifier
identifier m_id;
// unique identifier
identifier m_id;
// uniform type name
std::string m_name;
// uniform type name
std::string m_name;
// disable copy and move constructors
uniform_type_info(uniform_type_info&&) = delete;
uniform_type_info(const uniform_type_info&) = delete;
// disable copy and move constructors
uniform_type_info(uniform_type_info&&) = delete;
uniform_type_info(const uniform_type_info&) = delete;
// disable assignment operators
uniform_type_info& operator=(uniform_type_info&&) = delete;
uniform_type_info& operator=(const uniform_type_info&) = delete;
// disable assignment operators
uniform_type_info& operator=(uniform_type_info&&) = delete;
uniform_type_info& operator=(const uniform_type_info&) = delete;
static uniform_type_info* by_type_info(const std::type_info& tinfo);
static uniform_type_info* by_type_info(const std::type_info& tinfo);
protected:
explicit uniform_type_info(const std::type_info& tinfo);
explicit uniform_type_info(const std::type_info& tinfo);
public:
/**
* @brief Get instances by its uniform name.
* @param uniform_name The libCPPA internal name for a type.
* @return The instance associated to @p uniform_name.
*/
static uniform_type_info* by_uniform_name(const std::string& uniform_name);
/**
* @brief Get all instances.
* @return A vector with all known (announced) instances.
*/
static std::vector<uniform_type_info*> instances();
virtual ~uniform_type_info();
/**
* @brief Get the internal libCPPA name for this type.
* @return A string describing the libCPPA internal type name.
*/
inline const std::string& name() const { return m_name; }
/**
* @brief Get the unique identifier of this instance.
* @return The unique identifier of this instance.
*/
inline const identifier& id() const { return m_id; }
// needed by cppa::detail::comparable<uniform_type_info>
inline int compare(const uniform_type_info& other) const
{
return id().compare(other.id());
}
/**
* @brief Add a new type mapping to the libCPPA internal type system.
* @return <code>true</code> if @p uniform_type was added as known
* instance (mapped to @p plain_type); otherwise <code>false</code>
* is returned and @p uniform_type was deleted.
*/
static bool announce(const std::type_info& plain_type,
uniform_type_info* uniform_type);
/**
* auto concept value_type<typename T>
* {
* T();
* T(const T&);
* bool operator==(const T&, const T&);
* }
*/
template<typename T,
class SerializeFun, class DeserializeFun,
class ToStringFun, class FromStringFun>
static bool announce(const SerializeFun& sf, const DeserializeFun& df,
const ToStringFun& ts, const FromStringFun& fs);
/**
* @brief Create an object of this type.
*/
virtual object create() const = 0;
virtual object from_string(const std::string& str) const = 0;
/**
* @brief Get instance by uniform name.
* @param uniform_name The libCPPA internal name for a type.
* @return The instance associated to @p uniform_name.
*/
static uniform_type_info* by_uniform_name(const std::string& uniform_name);
/**
* @brief Get all instances.
* @return A vector with all known (announced) instances.
*/
static std::vector<uniform_type_info*> instances();
virtual ~uniform_type_info();
/**
* @brief Get the internal libCPPA name for this type.
* @return A string describing the libCPPA internal type name.
*/
inline const std::string& name() const { return m_name; }
/**
* @brief Get the unique identifier of this instance.
* @return The unique identifier of this instance.
*/
inline const identifier& id() const { return m_id; }
// needed by cppa::detail::comparable<uniform_type_info>
inline int compare(const uniform_type_info& other) const
{
return id().compare(other.id());
}
/**
* @brief Add a new type mapping to the libCPPA internal type system.
* @return <code>true</code> if @p uniform_type was added as known
* instance (mapped to @p plain_type); otherwise @c false
* is returned and @p uniform_type was deleted.
*/
static bool announce(const std::type_info& plain_type,
uniform_type_info* uniform_type);
/**
* auto concept value_type<typename T>
* {
* T();
* T(const T&);
* bool operator==(const T&, const T&);
* }
*/
template<typename T,
class SerializeFun, class DeserializeFun,
class ToStringFun, class FromStringFun>
static bool announce(const SerializeFun& sf, const DeserializeFun& df,
const ToStringFun& ts, const FromStringFun& fs);
/**
* @brief Create an object of this type.
*/
virtual object create() const = 0;
virtual object from_string(const std::string& str) const = 0;
protected:
// needed to implement subclasses
inline void*& value_of(object& obj) const { return obj.m_value; }
inline const void* value_of(const object& obj) const { return obj.m_value; }
// needed to implement subclasses
inline void*& value_of(object& obj) const { return obj.m_value; }
inline const void* value_of(const object& obj) const { return obj.m_value; }
// object creation
virtual object copy(const object& what) const = 0;
// object creation
virtual object copy(const object& what) const = 0;
// object modification
virtual void destroy(object& what) const = 0;
virtual void deserialize(deserializer& d, object& what) const = 0;
// object modification
virtual void destroy(object& what) const = 0;
virtual void deserialize(deserializer& d, object& what) const = 0;
// object inspection
virtual std::string to_string(const object& obj) const = 0;
virtual bool equal(const object& lhs, const object& rhs) const = 0;
virtual void serialize(serializer& s, const object& what) const = 0;
// object inspection
virtual std::string to_string(const object& obj) const = 0;
virtual bool equal(const object& lhs, const object& rhs) const = 0;
virtual void serialize(serializer& s, const object& what) const = 0;
};
template<typename T>
uniform_type_info* uniform_typeid()
{
return uniform_type_info::by_type_info(typeid(T));
return uniform_type_info::by_type_info(typeid(T));
}
template<typename T,
class SerializeFun, class DeserializeFun,
class ToStringFun, class FromStringFun>
class SerializeFun, class DeserializeFun,
class ToStringFun, class FromStringFun>
bool uniform_type_info::announce(const SerializeFun& sf,
const DeserializeFun& df,
const ToStringFun& ts,
const FromStringFun& fs)
const DeserializeFun& df,
const ToStringFun& ts,
const FromStringFun& fs)
{
// check signature of SerializeFun::operator()
typedef
typename
util::callable_trait<decltype(&SerializeFun::operator())>::arg_types
sf_args;
// assert arg_types == { serializer&, const T& } || { serializer&, T }
static_assert(
util::disjunction<
std::is_same<sf_args, util::type_list<serializer&, const T&>>,
std::is_same<sf_args, util::type_list<serializer&, T>>
>::value,
"Invalid signature of &SerializeFun::operator()");
// check signature of DeserializeFun::operator()
typedef
typename
util::callable_trait<decltype(&DeserializeFun::operator())>::arg_types
df_args;
// assert arg_types == { deserializer&, T& }
static_assert(
std::is_same<df_args, util::type_list<deserializer&, T&>>::value,
"Invalid signature of &DeserializeFun::operator()");
// check signature of ToStringFun::operator()
typedef util::callable_trait<decltype(&ToStringFun::operator())> ts_sig;
typedef typename ts_sig::arg_types ts_args;
// assert result_type == std::string
static_assert(
std::is_same<std::string, typename ts_sig::result_type>::value,
"ToStringFun::operator() doesn't return a string");
// assert arg_types == { const T& } || { T }
static_assert(
util::disjunction<
std::is_same<ts_args, util::type_list<const T&>>,
std::is_same<ts_args, util::type_list<T>>
>::value,
"Invalid signature of &ToStringFun::operator()");
// check signature of ToStringFun::operator()
typedef util::callable_trait<decltype(&FromStringFun::operator())> fs_sig;
typedef typename fs_sig::arg_types fs_args;
// assert result_type == T*
static_assert(
std::is_same<T*, typename fs_sig::result_type>::value,
"FromStringFun::operator() doesn't return T*");
// assert arg_types == { const std::string& } || { std::string }
static_assert(
util::disjunction<
std::is_same<fs_args, util::type_list<const std::string&>>,
std::is_same<fs_args, util::type_list<std::string>>
>::value,
"Invalid signature of &FromStringFun::operator()");
// "on-the-fly" implementation of uniform_type_info
class utimpl : public uniform_type_info
{
SerializeFun m_serialize;
DeserializeFun m_deserialize;
ToStringFun m_to_string;
FromStringFun m_from_string;
inline T& to_ref(object& what) const
{
return *reinterpret_cast<T*>(this->value_of(what));
}
inline const T& to_ref(const object& what) const
{
return *reinterpret_cast<const T*>(this->value_of(what));
}
protected:
object copy(const object& what) const
{
return { new T(this->to_ref(what)), this };
}
object from_string(const std::string& str) const
{
return { m_from_string(str), this };
}
void destroy(object& what) const
{
delete reinterpret_cast<T*>(this->value_of(what));
this->value_of(what) = nullptr;
}
void deserialize(deserializer& d, object& what) const
{
m_deserialize(d, to_ref(what));
}
std::string to_string(const object& obj) const
{
return m_to_string(to_ref(obj));
}
bool equal(const object& lhs, const object& rhs) const
{
return (lhs.type() == *this && rhs.type() == *this)
? to_ref(lhs) == to_ref(rhs)
: false;
}
void serialize(serializer& s, const object& what) const
{
m_serialize(s, to_ref(what));
}
public:
utimpl(const SerializeFun& sfun, const DeserializeFun dfun,
const ToStringFun tfun, const FromStringFun ffun)
: uniform_type_info(typeid(T))
, m_serialize(sfun), m_deserialize(dfun)
, m_to_string(tfun), m_from_string(ffun)
{
}
object create() const
{
return { new T, this };
}
};
return announce(typeid(T), new utimpl(sf, df, ts, fs));
// check signature of SerializeFun::operator()
typedef
typename
util::callable_trait<decltype(&SerializeFun::operator())>::arg_types
sf_args;
// assert arg_types == { serializer&, const T& } || { serializer&, T }
static_assert(
util::disjunction<
std::is_same<sf_args, util::type_list<serializer&, const T&>>,
std::is_same<sf_args, util::type_list<serializer&, T>>
>::value,
"Invalid signature of &SerializeFun::operator()");
// check signature of DeserializeFun::operator()
typedef
typename
util::callable_trait<decltype(&DeserializeFun::operator())>::arg_types
df_args;
// assert arg_types == { deserializer&, T& }
static_assert(
std::is_same<df_args, util::type_list<deserializer&, T&>>::value,
"Invalid signature of &DeserializeFun::operator()");
// check signature of ToStringFun::operator()
typedef util::callable_trait<decltype(&ToStringFun::operator())> ts_sig;
typedef typename ts_sig::arg_types ts_args;
// assert result_type == std::string
static_assert(
std::is_same<std::string, typename ts_sig::result_type>::value,
"ToStringFun::operator() doesn't return a string");
// assert arg_types == { const T& } || { T }
static_assert(
util::disjunction<
std::is_same<ts_args, util::type_list<const T&>>,
std::is_same<ts_args, util::type_list<T>>
>::value,
"Invalid signature of &ToStringFun::operator()");
// check signature of ToStringFun::operator()
typedef util::callable_trait<decltype(&FromStringFun::operator())> fs_sig;
typedef typename fs_sig::arg_types fs_args;
// assert result_type == T*
static_assert(
std::is_same<T*, typename fs_sig::result_type>::value,
"FromStringFun::operator() doesn't return T*");
// assert arg_types == { const std::string& } || { std::string }
static_assert(
util::disjunction<
std::is_same<fs_args, util::type_list<const std::string&>>,
std::is_same<fs_args, util::type_list<std::string>>
>::value,
"Invalid signature of &FromStringFun::operator()");
// "on-the-fly" implementation of uniform_type_info
class utimpl : public uniform_type_info
{
SerializeFun m_serialize;
DeserializeFun m_deserialize;
ToStringFun m_to_string;
FromStringFun m_from_string;
inline T& to_ref(object& what) const
{
return *reinterpret_cast<T*>(this->value_of(what));
}
inline const T& to_ref(const object& what) const
{
return *reinterpret_cast<const T*>(this->value_of(what));
}
protected:
object copy(const object& what) const
{
return { new T(this->to_ref(what)), this };
}
object from_string(const std::string& str) const
{
return { m_from_string(str), this };
}
void destroy(object& what) const
{
delete reinterpret_cast<T*>(this->value_of(what));
this->value_of(what) = nullptr;
}
void deserialize(deserializer& d, object& what) const
{
m_deserialize(d, to_ref(what));
}
std::string to_string(const object& obj) const
{
return m_to_string(to_ref(obj));
}
bool equal(const object& lhs, const object& rhs) const
{
return (lhs.type() == *this && rhs.type() == *this)
? to_ref(lhs) == to_ref(rhs)
: false;
}
void serialize(serializer& s, const object& what) const
{
m_serialize(s, to_ref(what));
}
public:
utimpl(const SerializeFun& sfun, const DeserializeFun dfun,
const ToStringFun tfun, const FromStringFun ffun)
: uniform_type_info(typeid(T))
, m_serialize(sfun), m_deserialize(dfun)
, m_to_string(tfun), m_from_string(ffun)
{
}
object create() const
{
return { new T, this };
}
};
return announce(typeid(T), new utimpl(sf, df, ts, fs));
}
bool operator==(const uniform_type_info& lhs, const std::type_info& rhs);
inline bool operator!=(const uniform_type_info& lhs, const std::type_info& rhs)
{
return !(lhs == rhs);
return !(lhs == rhs);
}
inline bool operator==(const std::type_info& lhs, const uniform_type_info& rhs)
{
return rhs == lhs;
return rhs == lhs;
}
inline bool operator!=(const std::type_info& lhs, const uniform_type_info& rhs)
{
return !(rhs == lhs);
return !(rhs == lhs);
}
} // namespace cppa
......
#ifndef DISABLE_IF_HPP
#define DISABLE_IF_HPP
namespace cppa { namespace util {
template<bool Stmt, typename T>
struct disable_if_c { };
template<typename T>
struct disable_if_c<false, T>
{
typedef T type;
};
template<class Trait, typename T = void>
struct disable_if : disable_if_c<Trait::value, T>
{
};
} } // namespace cppa::util
#endif // DISABLE_IF_HPP
......@@ -4,12 +4,14 @@
namespace cppa { namespace util {
template<bool Stmt, typename T = void>
struct enable_if_c { };
struct enable_if_c
{
};
template<typename T>
struct enable_if_c<true, T>
{
typedef T type;
typedef T type;
};
template<class Trait, typename T = void>
......
#ifndef IF_ELSE_TYPE_HPP
#define IF_ELSE_TYPE_HPP
#include "cppa/util/wrapped_type.hpp"
namespace cppa { namespace util {
// if (IfStmt == true) type = T; else type = Else::type;
template<bool IfStmt, typename T, class Else>
struct if_else_type_c
{
typedef T type;
};
template<typename T, class Else>
struct if_else_type_c<false, T, Else>
{
typedef typename Else::type type;
};
// if (Stmt::value == true) type = T; else type = Else::type;
template<class Stmt, typename T, class Else>
struct if_else_type : if_else_type_c<Stmt::value, T, Else> { };
} } // namespace cppa::util
#endif // IF_ELSE_TYPE_HPP
#ifndef WRAPPED_TYPE_HPP
#define WRAPPED_TYPE_HPP
namespace cppa { namespace util {
template<typename T>
struct wrapped_type { typedef T type; };
} } // namespace cppa::util
#endif // WRAPPED_TYPE_HPP
......@@ -7,11 +7,15 @@
#include <cstring>
#include <sstream>
#include <cstdint>
#include <cstring>
#include <cassert>
#include <iterator>
#include <typeinfo>
#include <iostream>
#include <stdexcept>
#include <algorithm>
#include <functional>
#include <type_traits>
#include <cassert>
#include "test.hpp"
......@@ -23,270 +27,224 @@
#include "cppa/deserializer.hpp"
#include "cppa/untyped_tuple.hpp"
#include "cppa/util/enable_if.hpp"
#include "cppa/util/disable_if.hpp"
#include "cppa/util/if_else_type.hpp"
#include "cppa/util/wrapped_type.hpp"
#include "cppa/util.hpp"
//#include "cppa/util.hpp"
using std::cout;
using std::cerr;
using std::endl;
/**
* @brief Integers, floating points and strings.
*/
enum fundamental_type
using namespace cppa::util;
template<class C, template <typename> class... Traits>
struct apply;
template<class C>
struct apply<C>
{
ft_int8, ft_int16, ft_int32, ft_int64,
ft_uint8, ft_uint16, ft_uint32, ft_uint64,
ft_float, ft_double, ft_long_double,
ft_u8string, ft_u16string, ft_u32string,
ft_null
typedef C type;
};
constexpr const char* fundamental_type_names[] =
template<class C,
template <typename> class Trait0,
template <typename> class... Traits>
struct apply<C, Trait0, Traits...>
{
"ft_int8", "ft_int16", "ft_int32", "ft_int64",
"ft_uint8", "ft_uint16", "ft_uint32", "ft_uint64",
"ft_float", "ft_double", "ft_long_double",
"ft_u8string", "ft_u16string", "ft_u32string",
"ft_null"
typedef typename apply<typename Trait0<C>::type, Traits...>::type type;
};
constexpr const char* fundamental_type_name(fundamental_type ftype)
{
return fundamental_type_names[static_cast<int>(ftype)];
}
// achieves static call dispatch (Int-To-Type idiom)
template<fundamental_type FT>
struct ft_token { static const fundamental_type value = FT; };
template<typename T>
struct plain
{
typedef typename apply<T, std::remove_reference, std::remove_cv>::type type;
};
// if (IfStmt == true) type = T; else type = Else::type;
template<bool IfStmt, typename T, class Else>
struct if_else_type_c
/**
* @brief Integers (signed and unsigned), floating points and strings.
*/
enum primitive_type
{
typedef T type;
pt_int8, pt_int16, pt_int32, pt_int64,
pt_uint8, pt_uint16, pt_uint32, pt_uint64,
pt_float, pt_double, pt_long_double,
pt_u8string, pt_u16string, pt_u32string,
pt_null
};
template<typename T, class Else>
struct if_else_type_c<false, T, Else>
constexpr const char* primitive_type_names[] =
{
typedef typename Else::type type;
"pt_int8", "pt_int16", "pt_int32", "pt_int64",
"pt_uint8", "pt_uint16", "pt_uint32", "pt_uint64",
"pt_float", "pt_double", "pt_long_double",
"pt_u8string", "pt_u16string", "pt_u32string",
"pt_null"
};
// if (Stmt::value == true) type = T; else type = Else::type;
template<class Stmt, typename T, class Else>
struct if_else_type : if_else_type_c<Stmt::value, T, Else> { };
constexpr const char* primitive_type_name(primitive_type ptype)
{
return primitive_type_names[static_cast<int>(ptype)];
}
template<typename T>
struct wrapped_type { typedef T type; };
// achieves static call dispatch (Int-To-Type idiom)
template<primitive_type FT>
struct pt_token { static const primitive_type value = FT; };
// maps the fundamental_type FT to the corresponding type
template<fundamental_type FT>
struct ftype_to_type
: if_else_type_c<FT == ft_int8, std::int8_t,
if_else_type_c<FT == ft_int16, std::int16_t,
if_else_type_c<FT == ft_int32, std::int32_t,
if_else_type_c<FT == ft_int64, std::int64_t,
if_else_type_c<FT == ft_uint8, std::uint8_t,
if_else_type_c<FT == ft_uint16, std::uint16_t,
if_else_type_c<FT == ft_uint32, std::uint32_t,
if_else_type_c<FT == ft_uint64, std::uint64_t,
if_else_type_c<FT == ft_float, float,
if_else_type_c<FT == ft_double, double,
if_else_type_c<FT == ft_long_double, long double,
if_else_type_c<FT == ft_u8string, std::string,
if_else_type_c<FT == ft_u16string, std::u16string,
if_else_type_c<FT == ft_u32string, std::u32string,
template<primitive_type FT>
struct ptype_to_type
: if_else_type_c<FT == pt_int8, std::int8_t,
if_else_type_c<FT == pt_int16, std::int16_t,
if_else_type_c<FT == pt_int32, std::int32_t,
if_else_type_c<FT == pt_int64, std::int64_t,
if_else_type_c<FT == pt_uint8, std::uint8_t,
if_else_type_c<FT == pt_uint16, std::uint16_t,
if_else_type_c<FT == pt_uint32, std::uint32_t,
if_else_type_c<FT == pt_uint64, std::uint64_t,
if_else_type_c<FT == pt_float, float,
if_else_type_c<FT == pt_double, double,
if_else_type_c<FT == pt_long_double, long double,
if_else_type_c<FT == pt_u8string, std::string,
if_else_type_c<FT == pt_u16string, std::u16string,
if_else_type_c<FT == pt_u32string, std::u32string,
wrapped_type<void> > > > > > > > > > > > > > >
{
};
// if (IfStmt == true) ftype = FT; else ftype = Else::ftype;
template<bool IfStmt, fundamental_type FT, class Else>
struct if_else_ftype_c
// if (IfStmt == true) ptype = FT; else ptype = Else::ptype;
template<bool IfStmt, primitive_type FT, class Else>
struct if_else_ptype_c
{
static const fundamental_type ftype = FT;
static const primitive_type ptype = FT;
};
template<fundamental_type FT, class Else>
struct if_else_ftype_c<false, FT, Else>
template<primitive_type FT, class Else>
struct if_else_ptype_c<false, FT, Else>
{
static const fundamental_type ftype = Else::ftype;
static const primitive_type ptype = Else::ptype;
};
// if (Stmt::value == true) ftype = FT; else ftype = Else::ftype;
template<class Stmt, fundamental_type FT, class Else>
struct if_else_ftype : if_else_ftype_c<Stmt::value, FT, Else> { };
// if (Stmt::value == true) ptype = FT; else ptype = Else::ptype;
template<class Stmt, primitive_type FT, class Else>
struct if_else_ptype : if_else_ptype_c<Stmt::value, FT, Else> { };
template<fundamental_type FT>
struct wrapped_ftype { static const fundamental_type ftype = FT; };
template<primitive_type FT>
struct wrapped_ptype { static const primitive_type ptype = FT; };
// maps type T the the corresponding fundamental_type
template<typename T>
struct type_to_ftype
struct type_to_ptype_impl
// signed integers
: if_else_ftype<std::is_same<T, std::int8_t>, ft_int8,
if_else_ftype<std::is_same<T, std::int16_t>, ft_int16,
if_else_ftype<std::is_same<T, std::int32_t>, ft_int32,
if_else_ftype<std::is_same<T, std::int64_t>, ft_int64,
if_else_ftype<std::is_same<T, std::uint8_t>, ft_uint8,
: if_else_ptype<std::is_same<T, std::int8_t>, pt_int8,
if_else_ptype<std::is_same<T, std::int16_t>, pt_int16,
if_else_ptype<std::is_same<T, std::int32_t>, pt_int32,
if_else_ptype<std::is_same<T, std::int64_t>, pt_int64,
if_else_ptype<std::is_same<T, std::uint8_t>, pt_uint8,
// unsigned integers
if_else_ftype<std::is_same<T, std::uint16_t>, ft_uint16,
if_else_ftype<std::is_same<T, std::uint32_t>, ft_uint32,
if_else_ftype<std::is_same<T, std::uint64_t>, ft_uint64,
if_else_ptype<std::is_same<T, std::uint16_t>, pt_uint16,
if_else_ptype<std::is_same<T, std::uint32_t>, pt_uint32,
if_else_ptype<std::is_same<T, std::uint64_t>, pt_uint64,
// float / double
if_else_ftype<std::is_same<T, float>, ft_float,
if_else_ftype<std::is_same<T, double>, ft_double,
if_else_ftype<std::is_same<T, long double>, ft_long_double,
if_else_ptype<std::is_same<T, float>, pt_float,
if_else_ptype<std::is_same<T, double>, pt_double,
if_else_ptype<std::is_same<T, long double>, pt_long_double,
// strings
if_else_ftype<std::is_convertible<T, std::string>, ft_u8string,
if_else_ftype<std::is_convertible<T, std::u16string>, ft_u16string,
if_else_ftype<std::is_convertible<T, std::u32string>, ft_u32string,
wrapped_ftype<ft_null> > > > > > > > > > > > > > >
if_else_ptype<std::is_convertible<T, std::string>, pt_u8string,
if_else_ptype<std::is_convertible<T, std::u16string>, pt_u16string,
if_else_ptype<std::is_convertible<T, std::u32string>, pt_u32string,
wrapped_ptype<pt_null> > > > > > > > > > > > > > >
{
};
template<typename T>
struct type_to_ftype<T&> : type_to_ftype<T> { };
template<typename T>
struct type_to_ftype<T&&> : type_to_ftype<T> { };
struct type_to_ptype : type_to_ptype_impl<typename plain<T>::type> { };
template<typename T>
struct type_to_ftype<const T&> : type_to_ftype<T> { };
namespace trait {
template<typename T>
struct type_to_ftype<const T> : type_to_ftype<T> { };
struct value
struct is_primitive
{
fundamental_type type;
inline value(const fundamental_type& ftype) : type(ftype) { }
static const bool value = type_to_ptype<T>::ptype != pt_null;
};
struct list
template<typename T>
class is_iterable
{
fundamental_type value_type;
inline list(const fundamental_type& ftype) : value_type(ftype) { }
};
struct map
{
fundamental_type key_type;
fundamental_type value_type;
inline map(const fundamental_type& key_ft, const fundamental_type& value_ft)
: key_type(key_ft), value_type(value_ft)
template<class C>
static bool cmp_help_fun(C& arg0,
decltype((arg0.begin() == arg0.end()) &&
(*(++(arg0.begin())) == *(arg0.end())))*)
{
return true;
}
};
using cppa::util::enable_if;
template<bool Stmt, typename T>
struct disable_if_c { };
template<class C>
static void cmp_help_fun(C&, void*) { }
template<typename T>
struct disable_if_c<false, T>
{
typedef T type;
};
typedef decltype(cmp_help_fun(*static_cast<T*>(nullptr),
static_cast<bool*>(nullptr)))
result_type;
template<class Trait, typename T = void>
struct disable_if : disable_if_c<Trait::value, T> { };
template<fundamental_type FT, class T, class V>
void set_fun(fundamental_type& lhs_type, T& lhs, V&& rhs,
typename disable_if<std::is_arithmetic<T>>::type* = nullptr)
{
if (FT == lhs_type)
{
lhs = std::forward<V>(rhs);
}
else
{
new (&lhs) T(std::forward<V>(rhs));
lhs_type = FT;
}
}
template<fundamental_type FT, class T, class V>
void set_fun(fundamental_type& lhs_type, T& lhs, V&& rhs,
typename enable_if<std::is_arithmetic<T>>::type* = nullptr)
{
lhs = rhs;
lhs_type = FT;
}
public:
template<class T>
void destroy_fun(T& what,
typename disable_if<std::is_arithmetic<T>>::type* = nullptr)
{
what.~T();
}
static const bool value = !is_primitive<T>::value
&& std::is_same<bool, result_type>::value;
template<class T>
void destroy_fun(T&, typename enable_if<std::is_arithmetic<T>>::type* = nullptr)
{
// arithmetic types don't need destruction
}
};
class ft_value;
} // namespace trait
template<typename T>
T ft_value_cast(ft_value&);
class pt_value;
template<typename T>
T ft_value_cast(const ft_value&);
T pt_value_cast(pt_value&);
template<fundamental_type FT>
typename ftype_to_type<FT>::type& ft_value_cast(ft_value&);
template<fundamental_type FT>
const typename ftype_to_type<FT>::type& ft_value_cast(const ft_value&);
template<primitive_type FT>
typename ptype_to_type<FT>::type& pt_value_cast(pt_value&);
// Utility function for static call dispatching.
// Invokes pt_token<X>(), where X is the value of ptype.
// Does nothing if ptype == pt_null
template<typename Fun>
void ft_invoke(fundamental_type ftype, Fun&& f)
void pt_invoke(primitive_type ptype, Fun&& f)
{
switch (ftype)
{
case ft_int8: f(ft_token<ft_int8>()); break;
case ft_int16: f(ft_token<ft_int16>()); break;
case ft_int32: f(ft_token<ft_int32>()); break;
case ft_int64: f(ft_token<ft_int64>()); break;
case ft_uint8: f(ft_token<ft_uint8>()); break;
case ft_uint16: f(ft_token<ft_uint16>()); break;
case ft_uint32: f(ft_token<ft_uint32>()); break;
case ft_uint64: f(ft_token<ft_uint64>()); break;
case ft_float: f(ft_token<ft_float>()); break;
case ft_double: f(ft_token<ft_double>()); break;
case ft_long_double: f(ft_token<ft_long_double>()); break;
case ft_u8string: f(ft_token<ft_u8string>()); break;
case ft_u16string: f(ft_token<ft_u16string>()); break;
case ft_u32string: f(ft_token<ft_u32string>()); break;
switch (ptype)
{
case pt_int8: f(pt_token<pt_int8>()); break;
case pt_int16: f(pt_token<pt_int16>()); break;
case pt_int32: f(pt_token<pt_int32>()); break;
case pt_int64: f(pt_token<pt_int64>()); break;
case pt_uint8: f(pt_token<pt_uint8>()); break;
case pt_uint16: f(pt_token<pt_uint16>()); break;
case pt_uint32: f(pt_token<pt_uint32>()); break;
case pt_uint64: f(pt_token<pt_uint64>()); break;
case pt_float: f(pt_token<pt_float>()); break;
case pt_double: f(pt_token<pt_double>()); break;
case pt_long_double: f(pt_token<pt_long_double>()); break;
case pt_u8string: f(pt_token<pt_u8string>()); break;
case pt_u16string: f(pt_token<pt_u16string>()); break;
case pt_u32string: f(pt_token<pt_u32string>()); break;
default: break;
}
}
/**
* @brief Describes a value of a {@link fundamental_type}.
* @brief Describes a value of a {@link primitive_type primitive data type}.
*/
class ft_value
class pt_value
{
template<typename T>
friend T ft_value_cast(ft_value&);
template<typename T>
friend T ft_value_cast(const ft_value&);
friend T pt_value_cast(pt_value&);
template<fundamental_type FT>
friend typename ftype_to_type<FT>::type& ft_value_cast(ft_value&);
template<primitive_type PT>
friend typename ptype_to_type<PT>::type& pt_value_cast(pt_value&);
template<fundamental_type FT>
friend const typename ftype_to_type<FT>::type& ft_value_cast(const ft_value&);
fundamental_type m_ftype;
primitive_type m_ptype;
union
{
......@@ -307,93 +265,124 @@ class ft_value
};
// use static call dispatching to select member variable
inline decltype(i8)& get(ft_token<ft_int8>) { return i8; }
inline decltype(i16)& get(ft_token<ft_int16>) { return i16; }
inline decltype(i32)& get(ft_token<ft_int32>) { return i32; }
inline decltype(i64)& get(ft_token<ft_int64>) { return i64; }
inline decltype(u8)& get(ft_token<ft_uint8>) { return u8; }
inline decltype(u16)& get(ft_token<ft_uint16>) { return u16; }
inline decltype(u32)& get(ft_token<ft_uint32>) { return u32; }
inline decltype(u64)& get(ft_token<ft_uint64>) { return u64; }
inline decltype(fl)& get(ft_token<ft_float>) { return fl; }
inline decltype(dbl)& get(ft_token<ft_double>) { return dbl; }
inline decltype(ldbl)& get(ft_token<ft_long_double>) { return ldbl; }
inline decltype(str8)& get(ft_token<ft_u8string>) { return str8; }
inline decltype(str16)& get(ft_token<ft_u16string>) { return str16; }
inline decltype(str32)& get(ft_token<ft_u32string>) { return str32; }
inline decltype(i8)& get(pt_token<pt_int8>) { return i8; }
inline decltype(i16)& get(pt_token<pt_int16>) { return i16; }
inline decltype(i32)& get(pt_token<pt_int32>) { return i32; }
inline decltype(i64)& get(pt_token<pt_int64>) { return i64; }
inline decltype(u8)& get(pt_token<pt_uint8>) { return u8; }
inline decltype(u16)& get(pt_token<pt_uint16>) { return u16; }
inline decltype(u32)& get(pt_token<pt_uint32>) { return u32; }
inline decltype(u64)& get(pt_token<pt_uint64>) { return u64; }
inline decltype(fl)& get(pt_token<pt_float>) { return fl; }
inline decltype(dbl)& get(pt_token<pt_double>) { return dbl; }
inline decltype(ldbl)& get(pt_token<pt_long_double>) { return ldbl; }
inline decltype(str8)& get(pt_token<pt_u8string>) { return str8; }
inline decltype(str16)& get(pt_token<pt_u16string>) { return str16; }
inline decltype(str32)& get(pt_token<pt_u32string>) { return str32; }
template<primitive_type FT, class T, class V>
static void set(primitive_type& lhs_type, T& lhs, V&& rhs,
typename disable_if<std::is_arithmetic<T>>::type* = 0)
{
if (FT == lhs_type)
{
lhs = std::forward<V>(rhs);
}
else
{
new (&lhs) T(std::forward<V>(rhs));
lhs_type = FT;
}
}
// get(...) const overload
template<fundamental_type FT>
const typename ftype_to_type<FT>::type& get(ft_token<FT> token) const
template<primitive_type FT, class T, class V>
static void set(primitive_type& lhs_type, T& lhs, V&& rhs,
typename enable_if<std::is_arithmetic<T>>::type* = 0)
{
return const_cast<ft_value*>(this)->get(token);
// don't call a constructor for arithmetic types
lhs = rhs;
lhs_type = FT;
}
template<class T>
static void destroy(T& what,
typename disable_if<std::is_arithmetic<T>>::type* = 0)
{
what.~T();
}
template<class T>
static void destroy(T&,
typename enable_if<std::is_arithmetic<T>>::type* = 0)
{
// arithmetic types don't need destruction
}
struct destroyer
{
ft_value* m_self;
inline destroyer(ft_value* self) : m_self(self) { }
template<fundamental_type FT>
inline void operator()(ft_token<FT> token) const
pt_value* m_self;
inline destroyer(pt_value* self) : m_self(self) { }
template<primitive_type FT>
inline void operator()(pt_token<FT> token) const
{
destroy_fun(m_self->get(token));
destroy(m_self->get(token));
}
};
struct initializer
{
ft_value* m_self;
inline initializer(ft_value* self) : m_self(self) { }
template<fundamental_type FT>
inline void operator()(ft_token<FT> token) const
pt_value* m_self;
inline initializer(pt_value* self) : m_self(self) { }
template<primitive_type FT>
inline void operator()(pt_token<FT> token) const
{
set_fun<FT>(m_self->m_ftype,
m_self->get(token),
typename ftype_to_type<FT>::type());
set<FT>(m_self->m_ptype,
m_self->get(token),
typename ptype_to_type<FT>::type());
}
};
struct setter
{
ft_value* m_self;
const ft_value& m_other;
inline setter(ft_value* self, const ft_value& other)
pt_value* m_self;
const pt_value& m_other;
inline setter(pt_value* self, const pt_value& other)
: m_self(self), m_other(other) { }
template<fundamental_type FT>
inline void operator()(ft_token<FT> token) const
template<primitive_type FT>
inline void operator()(pt_token<FT> token) const
{
set_fun<FT>(m_self->m_ftype,
m_self->get(token),
m_other.get(token));
set<FT>(m_self->m_ptype,
m_self->get(token),
m_other.get(token));
}
};
struct mover
{
ft_value* m_self;
const ft_value& m_other;
inline mover(ft_value* self, const ft_value& other)
pt_value* m_self;
const pt_value& m_other;
inline mover(pt_value* self, const pt_value& other)
: m_self(self), m_other(other) { }
template<fundamental_type FT>
inline void operator()(ft_token<FT> token) const
template<primitive_type FT>
inline void operator()(pt_token<FT> token) const
{
set_fun<FT>(m_self->m_ftype,
m_self->get(token),
std::move(m_other.get(token)));
set<FT>(m_self->m_ptype,
m_self->get(token),
std::move(m_other.get(token)));
}
};
struct comparator
{
bool m_result;
const ft_value* m_self;
const ft_value& m_other;
inline comparator(const ft_value* self, const ft_value& other)
const pt_value* m_self;
const pt_value& m_other;
inline comparator(const pt_value* self, const pt_value& other)
: m_result(false), m_self(self), m_other(other) { }
template<fundamental_type FT>
inline void operator()(ft_token<FT> token)
template<primitive_type FT>
inline void operator()(pt_token<FT> token)
{
if (m_other.m_ftype == FT)
if (m_other.m_ptype == FT)
{
m_result = (m_self->get(token) == m_other.get(token));
}
......@@ -401,735 +390,1115 @@ class ft_value
inline bool result() const { return m_result; }
};
void destroy()
{
ft_invoke(m_ftype, destroyer(this));
m_ftype = ft_null;
}
template<class Self, typename Fun>
struct forwarder
struct applier
{
Self* m_self;
Fun& m_f;
forwarder(Self* self, Fun& f) : m_self(self), m_f(f) { }
template<fundamental_type FT>
inline void operator()(ft_token<FT> token)
applier(Self* self, Fun& f) : m_self(self), m_f(f) { }
template<primitive_type FT>
inline void operator()(pt_token<FT> token)
{
m_f(m_self->get(token));
}
};
struct type_reader
{
const std::type_info* tinfo;
type_reader() : tinfo(nullptr) { }
template<primitive_type FT>
inline void operator()(pt_token<FT>)
{
tinfo = &typeid(typename ptype_to_type<FT>::type);
}
};
void destroy()
{
pt_invoke(m_ptype, destroyer(this));
m_ptype = pt_null;
}
public:
// get(...) const overload
template<primitive_type FT>
const typename ptype_to_type<FT>::type& get(pt_token<FT> token) const
{
return const_cast<pt_value*>(this)->get(token);
}
template<typename Fun>
void apply(Fun&& f)
{
ft_invoke(m_ftype, forwarder<ft_value, Fun>(this, f));
pt_invoke(m_ptype, applier<pt_value, Fun>(this, f));
}
template<typename Fun>
void apply(Fun&& f) const
{
ft_invoke(m_ftype, forwarder<const ft_value, Fun>(this, f));
pt_invoke(m_ptype, applier<const pt_value, Fun>(this, f));
}
ft_value() : m_ftype(ft_null) { }
pt_value() : m_ptype(pt_null) { }
template<typename V>
ft_value(V&& value) : m_ftype(ft_null)
pt_value(V&& value) : m_ptype(pt_null)
{
static_assert(type_to_ftype<V>::ftype != ft_null,
"T is not a valid value for ft_value");
set_fun<type_to_ftype<V>::ftype>(m_ftype,
get(ft_token<type_to_ftype<V>::ftype>()),
std::forward<V>(value));
static_assert(type_to_ptype<V>::ptype != pt_null,
"T couldn't be mapped to an ptype");
set<type_to_ptype<V>::ptype>(m_ptype,
get(pt_token<type_to_ptype<V>::ptype>()),
std::forward<V>(value));
}
ft_value(fundamental_type ftype) : m_ftype(ft_null)
pt_value(primitive_type ptype) : m_ptype(pt_null)
{
ft_invoke(ftype, initializer(this));
pt_invoke(ptype, initializer(this));
}
ft_value(const ft_value& other) : m_ftype(ft_null)
pt_value(const pt_value& other) : m_ptype(pt_null)
{
//invoke(setter(other));
ft_invoke(other.m_ftype, setter(this, other));
pt_invoke(other.m_ptype, setter(this, other));
}
ft_value(ft_value&& other) : m_ftype(ft_null)
pt_value(pt_value&& other) : m_ptype(pt_null)
{
//invoke(mover(other));
ft_invoke(other.m_ftype, mover(this, other));
pt_invoke(other.m_ptype, mover(this, other));
}
ft_value& operator=(const ft_value& other)
pt_value& operator=(const pt_value& other)
{
//invoke(setter(other));
ft_invoke(other.m_ftype, setter(this, other));
pt_invoke(other.m_ptype, setter(this, other));
return *this;
}
ft_value& operator=(ft_value&& other)
pt_value& operator=(pt_value&& other)
{
//invoke(mover(other));
ft_invoke(other.m_ftype, mover(this, other));
pt_invoke(other.m_ptype, mover(this, other));
return *this;
}
bool operator==(const ft_value& other) const
bool operator==(const pt_value& other) const
{
comparator cmp(this, other);
ft_invoke(m_ftype, cmp);
pt_invoke(m_ptype, cmp);
return cmp.result();
}
bool operator!=(const ft_value& other) const
inline bool operator!=(const pt_value& other) const
{
return !(*this == other);
}
inline fundamental_type type() const { return m_ftype; }
inline primitive_type ptype() const { return m_ptype; }
const std::type_info& type() const
{
type_reader tr;
pt_invoke(m_ptype, tr);
return (tr.tinfo) ? *tr.tinfo : typeid(void);
}
~ft_value() { destroy(); }
~pt_value() { destroy(); }
};
template<typename T>
typename cppa::util::enable_if<std::is_arithmetic<T>, bool>::type
operator==(const T& lhs, const ft_value& rhs)
typename enable_if<trait::is_primitive<T>, bool>::type
operator==(const T& lhs, const pt_value& rhs)
{
return (rhs.type() == type_to_ftype<T>::ftype)
? lhs == ft_value_cast<const T&>(rhs)
: false;
constexpr primitive_type ptype = type_to_ptype<T>::ptype;
static_assert(ptype != pt_null, "T couldn't be mapped to an ptype");
return (rhs.ptype() == ptype) ? lhs == pt_value_cast<ptype>(rhs) : false;
}
template<typename T>
typename cppa::util::enable_if<std::is_arithmetic<T>, bool>::type
operator==(const ft_value& lhs, const T& rhs)
typename enable_if<trait::is_primitive<T>, bool>::type
operator==(const pt_value& lhs, const T& rhs)
{
return (lhs.type() == type_to_ftype<T>::ftype)
? ft_value_cast<const T&>(lhs) == rhs
: false;
return (rhs == lhs);
}
template<typename T>
typename cppa::util::enable_if<std::is_arithmetic<T>, bool>::type
operator!=(const T& lhs, const ft_value& rhs)
typename enable_if<trait::is_primitive<T>, bool>::type
operator!=(const pt_value& lhs, const T& rhs)
{
return !(lhs == rhs);
}
template<typename T>
typename cppa::util::enable_if<std::is_arithmetic<T>, bool>::type
operator!=(const ft_value& lhs, const T& rhs)
typename enable_if<trait::is_primitive<T>, bool>::type
operator!=(const T& lhs, const pt_value& rhs)
{
return !(lhs == rhs);
}
template<fundamental_type FT>
typename ftype_to_type<FT>::type& ft_value_cast(ft_value& v)
template<primitive_type FT>
typename ptype_to_type<FT>::type& pt_value_cast(pt_value& v)
{
if (v.type() != FT) throw std::bad_cast();
return v.get(ft_token<FT>());
if (v.ptype() != FT) throw std::bad_cast();
return v.get(pt_token<FT>());
}
template<fundamental_type FT>
const typename ftype_to_type<FT>::type& ft_value_cast(const ft_value& v)
template<primitive_type FT>
const typename ptype_to_type<FT>::type& pt_value_cast(const pt_value& v)
{
if (v.type() != FT) throw std::bad_cast();
return v.get(ft_token<FT>());
if (v.ptype() != FT) throw std::bad_cast();
return v.get(pt_token<FT>());
}
template<typename T>
T ft_value_cast(ft_value& v)
T pt_value_cast(pt_value& v)
{
static const fundamental_type ftype = type_to_ftype<T>::ftype;
if (v.type() != ftype) throw std::bad_cast();
return v.get(ft_token<ftype>());
static const primitive_type ptype = type_to_ptype<T>::ptype;
if (v.ptype() != ptype) throw std::bad_cast();
return v.get(pt_token<ptype>());
}
template<typename T>
T ft_value_cast(const ft_value& v)
T pt_value_cast(const pt_value& v)
{
typedef typename std::remove_reference<T>::type plain_t;
static_assert(!std::is_reference<T>::value || std::is_const<plain_t>::value,
"Could not get a non-const reference from const ft_value&");
static const fundamental_type ftype = type_to_ftype<T>::ftype;
if (v.type() != ftype) throw std::bad_cast();
return v.get(ft_token<ftype>());
"Could not get a non-const reference from const pt_value&");
static const primitive_type ptype = type_to_ptype<T>::ptype;
if (v.ptype() != ptype) throw std::bad_cast();
return v.get(pt_token<ptype>());
}
class value_property
struct getter_setter_pair
{
public:
std::function<pt_value (void*)> getter;
std::function<void (void*, pt_value&&)> setter;
getter_setter_pair(getter_setter_pair&&) = default;
getter_setter_pair(const getter_setter_pair&) = default;
template<class C, typename T>
getter_setter_pair(T C::*member_ptr)
{
getter = [=] (void* self) -> pt_value {
return *reinterpret_cast<C*>(self).*member_ptr;
};
setter = [=] (void* self, pt_value&& value) {
*reinterpret_cast<C*>(self).*member_ptr = std::move(pt_value_cast<T&>(value));
};
}
virtual ~value_property() { }
virtual void set(ft_value&& what) = 0;
virtual void get(ft_value& storage) const = 0;
virtual fundamental_type type() const = 0;
template<class C, typename GT, typename ST>
getter_setter_pair(GT (C::*get_memfn)() const, void (C::*set_memfn)(ST))
{
getter = [=] (void* self) -> pt_value {
return (*reinterpret_cast<C*>(self).*get_memfn)();
};
setter = [=] (void* self, pt_value&& value) {
(*reinterpret_cast<C*>(self).*set_memfn)(std::move(pt_value_cast<typename plain<ST>::type&>(value)));
};
}
};
class list_property
class serializer
{
public:
class iterator
{
virtual void begin_object(const std::string& type_name) = 0;
virtual void end_object() = 0;
public:
virtual void begin_list(size_t size) = 0;
virtual void end_list() = 0;
virtual ~iterator() { }
virtual void next() = 0;
virtual bool at_end() const = 0;
virtual ft_value get() const = 0;
virtual void write_value(const pt_value& value) = 0;
};
};
class serializer;
class deserializer;
virtual ~list_property() { }
virtual size_t size() const = 0;
virtual iterator* begin() const = 0;
virtual fundamental_type value_type() const = 0;
virtual void push_back(ft_value&& what) = 0;
class meta_type
{
public:
virtual ~meta_type() { }
/**
* @brief Create an instance of this type, initialized
* with its default constructor.
*/
virtual void* default_constructed() = 0;
virtual void delete_instance(void*) = 0;
virtual void serialize(void*, serializer*) = 0;
virtual void deserialize(void*, deserializer*) = 0;
};
class map_property
std::map<std::string, meta_type*> s_meta_types;
/**
*
*/
class deserializer
{
public:
class iterator
{
/**
* @brief Seek the beginning of the next object and return its type name.
*/
virtual std::string seek_object() = 0;
public:
/**
* @brief Seek the beginning of the next object and return its type name,
* but don't modify the internal in-stream position.
*/
virtual std::string peek_object() = 0;
virtual ~iterator() { }
virtual void next() = 0;
virtual bool at_end() const = 0;
virtual ft_value key() const = 0;
virtual ft_value value() const = 0;
virtual void begin_object(const std::string& type_name) = 0;
virtual void end_object() = 0;
};
virtual size_t begin_list(primitive_type value_type) = 0;
virtual void end_list() = 0;
virtual ~map_property() { }
virtual size_t size() const = 0;
virtual iterator* begin() const = 0;
virtual fundamental_type key_type() const = 0;
virtual fundamental_type value_type() const = 0;
virtual void insert(ft_value&& key, ft_value&& val) = 0;
virtual pt_value read_value(primitive_type ptype) = 0;
};
template<typename Getter, typename Setter, fundamental_type FT>
class value_property_impl : public value_property
class root_object
{
Getter m_get;
Setter m_set;
// T* m_ptr;
public:
// value_property_impl(T* ptr) : m_ptr(ptr) { }
std::pair<void*, meta_type*> deserialize(deserializer* d)
{
void* result;
std::string tname = d->peek_object();
auto i = s_meta_types.find(tname);
if (i == s_meta_types.end())
{
throw std::logic_error("no meta object found for " + tname);
}
auto mobj = i->second;
if (mobj == nullptr)
{
throw std::logic_error("mobj == nullptr");
}
result = mobj->default_constructed();
if (result == nullptr)
{
throw std::logic_error("result == nullptr");
}
try
{
mobj->deserialize(result, d);
}
catch (...)
{
mobj->delete_instance(result);
return { nullptr, nullptr };
}
return { result, mobj };
}
};
value_property_impl(Getter g, Setter s) : m_get(g), m_set(s) { }
template<typename T>
class meta_value_property : public meta_type
{
void set(ft_value&& what)
static constexpr primitive_type ptype = type_to_ptype<T>::ptype;
static_assert(ptype != pt_null, "T is not a primitive type");
public:
meta_value_property() { }
pt_value get(void* obj)
{
// *m_ptr = std::move(ft_value_cast<FT>(what));
m_set(std::move(ft_value_cast<FT>(what)));
return *reinterpret_cast<T*>(obj);
}
void get(ft_value& storage) const
void set(void* obj, pt_value&& value)
{
// ft_value_cast<FT>(storage) = *m_ptr;
ft_value_cast<FT>(storage) = m_get();
*reinterpret_cast<T*>(obj) = std::move(pt_value_cast<T&>(value));
}
fundamental_type type() const
void* default_constructed()
{
return FT;
return new T();
}
};
template<class List, fundamental_type FT>
class list_property_impl : public list_property
{
class iterator : public list_property::iterator
void delete_instance(void* ptr)
{
delete reinterpret_cast<T*>(ptr);
}
typedef typename List::const_iterator native_iterator;
native_iterator pos;
native_iterator end;
public:
iterator(native_iterator bg, native_iterator nd) : pos(bg), end(nd) { }
void serialize(void* obj, serializer* s)
{
s->write_value(get(obj));
}
bool at_end() const { return pos == end; }
void deserialize(void* obj, deserializer* d)
{
set(obj, d->read_value(ptype));
}
void next() { ++pos; }
};
ft_value get() const { return *pos; }
// std::vector or std::list
template<typename List>
class meta_list_property : public meta_type
{
};
typedef typename List::value_type value_type;
static constexpr primitive_type vptype = type_to_ptype<value_type>::ptype;
List* m_list;
static_assert(vptype != pt_null, "T doesn't have a primitive value_type");
public:
list_property_impl(List* native_list) : m_list(native_list) { }
meta_list_property() { }
size_t size() const { return m_list->size(); }
void serialize(void* obj, serializer* s)
{
auto& ls = *reinterpret_cast<List*>(obj);
s->begin_list(ls.size());
for (const auto& val : ls)
{
s->write_value(val);
}
s->end_list();
}
list_property::iterator* begin() const
void deserialize(void* obj, deserializer* d)
{
return new iterator(m_list->begin(), m_list->end());
auto& ls = *reinterpret_cast<List*>(obj);
size_t ls_size = d->begin_list(vptype);
for (size_t i = 0; i < ls_size; ++i)
{
pt_value val = d->read_value(vptype);
ls.push_back(std::move(pt_value_cast<value_type&>(val)));
}
d->end_list();
}
void push_back(ft_value&& what)
void* default_constructed()
{
m_list->push_back(std::move(ft_value_cast<FT>(what)));
return new List();
}
fundamental_type value_type() const
void delete_instance(void* ptr)
{
return FT;
delete reinterpret_cast<List*>(ptr);
}
};
template<class Map, fundamental_type KeyType, fundamental_type ValueType>
class map_property_impl : public map_property
template<class Object>
class meta_object : public meta_type
{
class iterator : public map_property::iterator
class member
{
typedef typename Map::const_iterator native_iterator;
native_iterator pos;
native_iterator end;
meta_type* m_meta;
std::function<void* (void*)> m_deref;
public:
iterator(native_iterator bg, native_iterator nd) : pos(bg), end(nd) { }
template<typename T, class C>
member(meta_type* mtptr, T C::*mem_ptr) : m_meta(mtptr)
{
m_deref = [mem_ptr] (void* obj) -> void*
{
return &(*reinterpret_cast<C*>(obj).*mem_ptr);
};
}
void next() { ++pos; }
member(meta_type* pptr, std::function<void* (void*)>&& gpm)
: m_meta(pptr), m_deref(std::move(gpm))
{
}
member(member&&) = default;
bool at_end() const { return pos == end; }
member(const member&) = default;
ft_value key() const { return pos->first; }
inline void serialize(void* parent, serializer* s)
{
m_meta->serialize(m_deref(parent), s);
}
ft_value value() const { return pos->second; }
inline void deserialize(void* parent, deserializer* s)
{
m_meta->deserialize(m_deref(parent), s);
}
};
Map* m_map;
std::string class_name;
std::vector<member> m_members;
public:
// terminates recursion
inline void push_back() { }
map_property_impl(Map* ptr) : m_map(ptr) { }
template<typename T, class C, typename... Args>
void push_back(std::pair<T C::*, meta_object<T>*> pr, const Args&... args)
{
m_members.push_back({ pr.second, pr.first });
push_back(args...);
}
size_t size() const
template<class C, typename T, typename... Args>
typename enable_if<trait::is_primitive<T> >::type
push_back(T C::*mem_ptr, const Args&... args)
{
return m_map->size();
m_members.push_back({ new meta_value_property<T>(), mem_ptr });
push_back(args...);
}
iterator* begin() const
template<class C, typename T, typename... Args>
typename enable_if<trait::is_iterable<T> >::type
push_back(T C::*mem_ptr, const Args&... args)
{
return new iterator(m_map->begin(), m_map->end());
m_members.push_back({ new meta_list_property<T>(), mem_ptr });
push_back(args...);
}
void insert(ft_value&& k, ft_value&& v)
template<class C, typename T, typename... Args>
typename disable_if_c< trait::is_primitive<T>::value
|| trait::is_iterable<T>::value, void>::type
push_back(T C::*mem_ptr, const Args&... args)
{
m_map->insert(std::make_pair(
std::move(ft_value_cast<KeyType>(k)),
std::move(ft_value_cast<ValueType>(v))));
static_assert(trait::is_primitive<T>::value,
"T is neither a primitive type nor an iterable type");
}
fundamental_type key_type() const
public:
template<typename... Args>
meta_object(const Args&... args) : class_name(cppa::detail::to_uniform_name(cppa::detail::demangle(typeid(Object).name())))
{
push_back(args...);
}
void serialize(void* obj, serializer* s)
{
s->begin_object(class_name);
for (auto& m : m_members)
{
m.serialize(obj, s);
}
s->end_object();
}
void deserialize(void* obj, deserializer* d)
{
std::string cname = d->seek_object();
if (cname != class_name)
{
throw std::logic_error("wrong type name found");
}
d->begin_object(class_name);
for (auto& m : m_members)
{
m.deserialize(obj, d);
}
d->end_object();
}
void* default_constructed()
{
return KeyType;
return new Object();
}
fundamental_type value_type() const
void delete_instance(void* ptr)
{
return ValueType;
delete reinterpret_cast<Object*>(ptr);
}
};
template<fundamental_type FT, typename Getter, typename Setter>
value_property* as_value_property(Getter getter, Setter setter)
struct struct_a
{
return new value_property_impl<Getter, Setter, FT>(getter, setter);
}
int x;
int y;
};
using cppa::util::enable_if;
using cppa::util::conjunction;
bool operator==(const struct_a& lhs, const struct_a& rhs)
{
return lhs.x == rhs.x && lhs.y == rhs.y;
}
template<fundamental_type FT, class C, typename Getter, typename Setter>
typename enable_if<conjunction<std::is_member_function_pointer<Getter>,
std::is_member_function_pointer<Setter>>,
value_property*>::type
as_value_property(C* self, Getter getter, Setter setter)
bool operator!=(const struct_a& lhs, const struct_a& rhs)
{
typedef cppa::util::callable_trait<Getter> getter_trait;
typedef cppa::util::callable_trait<Setter> setter_trait;
typedef typename getter_trait::result_type getter_result;
typedef typename ftype_to_type<FT>::type setter_arg;
return as_value_property<FT>(
[=]() -> getter_result { return (*self.*getter)(); },
[=](setter_arg&& val) { (*self.*setter)(std::move(val)); });
return !(lhs == rhs);
}
template<typename T, fundamental_type FT = type_to_ftype<T>::ftype>
value_property* as_value_property(T* ptr)
struct struct_b
{
struct_a a;
int z;
std::list<int> ints;
};
bool operator==(const struct_b& lhs, const struct_b& rhs)
{
return as_value_property<FT>([ptr]() -> const T& { return *ptr; },
[ptr](T&& val) { *ptr = std::move(val); });
return lhs.a == rhs.a && lhs.z == rhs.z && lhs.ints == rhs.ints;
}
template<typename List,
fundamental_type FT = type_to_ftype<typename List::value_type>::ftype>
list_property* as_list_property(List* ptr)
bool operator!=(const struct_b& lhs, const struct_b& rhs)
{
return new list_property_impl<List, FT>(ptr);
return !(lhs == rhs);
}
template<typename Map,
fundamental_type KT = type_to_ftype<typename Map::key_type>::ftype,
fundamental_type VT = type_to_ftype<typename Map::mapped_type>::ftype>
map_property* as_map_property(Map* ptr)
template<class C, class Parent, typename... Args>
std::pair<C Parent::*, meta_object<C>*> compound_member(C Parent::*c_ptr, const Args&... args)
{
return new map_property_impl<Map, KT, VT>(ptr);
return std::make_pair(c_ptr, new meta_object<C>(args...));
}
struct property_ptr
class string_serializer : public serializer
{
enum flag_type { is_null, is_vp, is_lp, is_mp } m_flag;
std::ostream& out;
union
struct pt_writer
{
value_property* m_vp;
list_property* m_lp;
map_property* m_mp;
};
void set(value_property* vp)
{
m_flag = is_vp;
m_vp = vp;
}
std::ostream& out;
void set(list_property* lp)
{
m_flag = is_lp;
m_lp = lp;
}
pt_writer(std::ostream& mout) : out(mout) { }
void set(map_property* mp)
{
m_flag = is_mp;
m_mp = mp;
}
template<typename T>
void operator()(const T& value)
{
out << value;
}
void destroy()
{
switch (m_flag)
void operator()(const std::string& str)
{
case is_vp: delete m_vp; break;
case is_lp: delete m_lp; break;
case is_mp: delete m_mp; break;
default: break;
out << "\"" << str << "\"";
}
m_flag = is_null;
}
void move_from(property_ptr& other)
void operator()(const std::u16string&) { }
void operator()(const std::u32string&) { }
};
int m_open_objects;
bool m_after_value;
inline void clear()
{
m_flag = other.m_flag;
switch (other.m_flag)
if (m_after_value)
{
case is_vp: m_vp = other.m_vp; break;
case is_lp: m_lp = other.m_lp; break;
case is_mp: m_mp = other.m_mp; break;
default: break;
out << ", ";
m_after_value = false;
}
other.m_flag = is_null;
}
value_property* get(std::true_type, std::false_type, std::false_type)
public:
string_serializer(std::ostream& mout)
: out(mout), m_open_objects(0), m_after_value(false) { }
void begin_object(const std::string& type_name)
{
if (m_flag != is_vp) throw std::bad_cast();
return m_vp;
clear();
++m_open_objects;
out << type_name << " ( ";
}
list_property* get(std::false_type, std::true_type, std::false_type)
void end_object()
{
if (m_flag != is_lp) throw std::bad_cast();
return m_lp;
out << " )";
}
map_property* get(std::false_type, std::false_type, std::true_type)
void begin_list(size_t)
{
if (m_flag != is_mp) throw std::bad_cast();
return m_mp;
clear();
out << "{ ";
}
public:
void end_list()
{
if (!m_after_value)
{
out << "}";
}
else
{
out << " }";
}
}
property_ptr(value_property* ptr) { set(ptr); }
void write_value(const pt_value& value)
{
clear();
value.apply(pt_writer(out));
m_after_value = true;
}
property_ptr(list_property* ptr) { set(ptr); }
};
property_ptr(map_property* ptr) { set(ptr); }
class xml_serializer : public serializer
{
property_ptr() : m_flag(is_null) { }
std::ostream& out;
std::string indentation;
property_ptr(property_ptr&& other)
inline void inc_indentation()
{
move_from(other);
indentation.resize(indentation.size() + 4, ' ');
}
property_ptr& operator=(property_ptr&& other)
inline void dec_indentation()
{
destroy();
move_from(other);
return *this;
auto isize = indentation.size();
indentation.resize((isize > 4) ? (isize - 4) : 0);
}
~property_ptr()
struct pt_writer
{
destroy();
}
property_ptr(const property_ptr&) = delete;
std::ostream& out;
const std::string& indentation;
property_ptr& operator=(const property_ptr&) = delete;
pt_writer(std::ostream& ostr, const std::string& indent)
: out(ostr), indentation(indent)
{
}
inline bool is_value_property() const { return m_flag == is_vp; }
template<typename T>
void operator()(const T& value)
{
out << indentation << "<value type=\""
<< primitive_type_name(type_to_ptype<T>::ptype)
<< "\">" << value << "</value>\n";
}
void operator()(const std::u16string&) { }
void operator()(const std::u32string&) { }
};
inline bool is_list_property() const { return m_flag == is_lp; }
public:
inline bool is_map_property() const { return m_flag == is_mp; }
xml_serializer(std::ostream& ostr) : out(ostr), indentation("") { }
inline explicit operator bool() const { return m_flag != is_null; }
void begin_object(const std::string& type_name)
{
out << indentation << "<object type=\"" << type_name << "\">\n";
inc_indentation();
}
void end_object()
{
dec_indentation();
out << indentation << "</object>\n";
}
inline bool operator==(const std::nullptr_t&)
void begin_list(size_t)
{
return m_flag == is_null;
out << indentation << "<list>\n";
inc_indentation();
}
inline bool operator!=(const std::nullptr_t&)
void end_list()
{
return m_flag != is_null;
dec_indentation();
out << indentation << "</list>\n";
}
template<typename T>
T* as()
void write_value(const pt_value& value)
{
return get(std::is_same<T, value_property>(),
std::is_same<T, list_property>(),
std::is_same<T, map_property>());
value.apply(pt_writer(out, indentation));
}
};
/**
* @brief
*/
class abstract_object
class binary_serializer : public serializer
{
inline void push_back() { }
typedef char* buf_type;
buf_type m_buf;
size_t m_wr_pos;
template<typename T0, typename... Tn>
void push_back(T0* ptr0, Tn*... ptrs)
struct pt_writer
{
static_assert( std::is_same<T0, value_property>::value
|| std::is_same<T0, list_property>::value
|| std::is_same<T0, map_property>::value,
"invalid type");
m_properties.push_back(property_ptr(ptr0));
push_back(ptrs...);
}
public:
buf_type& m_buf;
size_t& m_wr_pos;
typedef std::vector<property_ptr> pptr_vector;
pt_writer(buf_type& buf, size_t& pos) : m_buf(buf), m_wr_pos(pos) { }
abstract_object(pptr_vector&& pvec) : m_properties(std::move(pvec)) { }
template<typename T>
void operator()(const T& value)
{
memcpy(m_buf + m_wr_pos, &value, sizeof(T));
m_wr_pos += sizeof(T);
}
void operator()(const std::u16string&) { }
void operator()(const std::u32string&) { }
template<typename T0, typename... Tn>
abstract_object(T0* ptr0, Tn*... ptrs)
};
template<typename T>
void write(const T& value)
{
push_back(ptr0, ptrs...);
memcpy(m_buf + m_wr_pos, &value, sizeof(T));
m_wr_pos += sizeof(T);
}
abstract_object() = default;
abstract_object(abstract_object&&) = default;
abstract_object& operator=(abstract_object&&) = default;
void write(const std::string& str)
{
write(static_cast<std::uint32_t>(str.size()));
memcpy(m_buf + m_wr_pos, str.c_str(), str.size());
m_wr_pos += str.size();
}
public:
abstract_object(const abstract_object&) = delete;
abstract_object& operator=(const abstract_object&) = delete;
binary_serializer(char* buf) : m_buf(buf), m_wr_pos(0) { }
size_t properties()
void begin_object(const std::string& tname)
{
return m_properties.size();
write(tname);
}
property_ptr& property(size_t pos)
void end_object() { }
void begin_list(size_t list_size)
{
return m_properties[pos];
write(static_cast<std::uint32_t>(list_size));
}
private:
void end_list() { }
pptr_vector m_properties;
void write_value(const pt_value& value)
{
value.apply(pt_writer(m_buf, m_wr_pos));
}
};
class sink
class binary_deserializer : public deserializer
{
public:
typedef char* buf_type;
virtual void write(abstract_object&) = 0;
buf_type m_buf;
size_t m_rd_pos;
size_t m_buf_size;
};
void range_check(size_t read_size)
{
if (m_rd_pos + read_size >= m_buf_size)
{
std::out_of_range("binary_deserializer::read()");
}
}
struct xml_sink_helper
{
std::ostringstream& ostr;
const std::string& indent;
xml_sink_helper(std::ostringstream& mostr, const std::string& idn) : ostr(mostr), indent(idn) { }
template<typename T>
void operator()(const T& what)
void read(T& storage, bool modify_rd_pos = true)
{
static const fundamental_type ftype = type_to_ftype<T>::ftype;
ostr << indent
<< "<" << fundamental_type_name(ftype) << ">"
<< what
<< "</" << fundamental_type_name(ftype) << ">\n";
range_check(sizeof(T));
memcpy(&storage, m_buf + m_rd_pos, sizeof(T));
if (modify_rd_pos) m_rd_pos += sizeof(T);
}
void operator()(const std::u16string&) { }
void operator()(const std::u32string&) { }
};
class xml_sink
{
static const char br = '\n';
void read(std::string& str, bool modify_rd_pos = true)
{
std::uint32_t str_size;
read(str_size, modify_rd_pos);
char* cpy_begin;
if (modify_rd_pos)
{
range_check(str_size);
cpy_begin = m_buf + m_rd_pos;
}
else
{
range_check(sizeof(std::uint32_t) + str_size);
cpy_begin = m_buf + m_rd_pos + sizeof(std::uint32_t);
}
str.clear();
str.reserve(str_size);
char* cpy_end = cpy_begin + str_size;
std::copy(cpy_begin, cpy_end, std::back_inserter(str));
if (modify_rd_pos) m_rd_pos += str_size;
}
std::ostringstream ostr;
template<typename CharType, typename StringType>
void read_unicode_string(StringType& str)
{
std::uint32_t str_size;
read(str_size);
str.reserve(str_size);
for (size_t i = 0; i < str_size; ++i)
{
CharType c;
read(c);
str_size += static_cast<typename StringType::value_type>(c);
}
}
void append(const std::string& indentation, const ft_value& what)
void read(std::u16string& str)
{
what.apply(xml_sink_helper(ostr, indentation));
// char16_t is guaranteed to has *at least* 16 bytes,
// but not to have *exactly* 16 bytes; thus use uint16_t
read_unicode_string<std::uint16_t>(str);
}
public:
void read(std::u32string& str)
{
// char32_t is guaranteed to has *at least* 32 bytes,
// but not to have *exactly* 32 bytes; thus use uint32_t
read_unicode_string<std::uint32_t>(str);
}
void write(abstract_object& obj)
struct pt_reader
{
ostr << "<object>" << br;
for (size_t i = 0; i < obj.properties(); ++i)
binary_deserializer* self;
inline pt_reader(binary_deserializer* mself) : self(mself) { }
template<typename T>
inline void operator()(T& value)
{
property_ptr& pptr = obj.property(i);
if (pptr.is_value_property())
{
auto vp = pptr.as<value_property>();
ft_value val(vp->type());
vp->get(val);
append(" ", val);
}
self->read(value);
}
ostr << "</object>";
};
public:
binary_deserializer(buf_type buf, size_t buf_size)
: m_buf(buf), m_rd_pos(0), m_buf_size(buf_size)
{
}
std::string str() const { return ostr.str(); }
std::string seek_object()
{
std::string result;
read(result);
return result;
}
};
std::string peek_object()
{
std::string result;
read(result, false);
return result;
}
class source
{
void begin_object(const std::string&)
{
}
public:
void end_object()
{
}
virtual void read(abstract_object&) = 0;
size_t begin_list(primitive_type)
{
std::uint32_t size;
read(size);
return size;
}
};
void end_list()
{
}
pt_value read_value(primitive_type ptype)
{
pt_value val(ptype);
val.apply(pt_reader(this));
return val;
}
struct point_struct
{
std::uint32_t x, y, z;
};
class point_class
class string_deserializer : public deserializer
{
std::uint32_t m_x, m_y, m_z;
std::string m_str;
std::string::iterator m_pos;
size_t m_obj_count;
public:
void skip_space_and_comma()
{
while (*m_pos == ' ' || *m_pos == ',') ++m_pos;
}
point_class() : m_x(0), m_y(0), m_z(0) { }
void throw_malformed(const std::string& error_msg)
{
throw std::logic_error("malformed string: " + error_msg);
}
point_class(std::uint32_t mx, std::uint32_t my, std::uint32_t mz)
: m_x(mx), m_y(my), m_z(mz)
void consume(char c)
{
skip_space_and_comma();
if (*m_pos != c)
{
std::string error;
error += "expected '";
error += c;
error += "' found '";
error += *m_pos;
error += "'";
throw_malformed(error);
}
++m_pos;
}
std::uint32_t x() const { return m_x; }
inline std::string::iterator next_delimiter()
{
return std::find_if(m_pos, m_str.end(), [] (char c) -> bool {
switch (c)
{
case '(':
case ')':
case '{':
case '}':
case ' ':
case ',': return true;
default : return false;
}
});
}
std::uint32_t y() const { return m_y; }
public:
std::uint32_t z() const { return m_z; }
string_deserializer(const std::string& str) : m_str(str)
{
m_pos = m_str.begin();
m_obj_count = 0;
}
void set_x(std::uint32_t value) { m_x = value; }
string_deserializer(std::string&& str) : m_str(std::move(str))
{
m_pos = m_str.begin();
m_obj_count = 0;
}
void set_y(std::uint32_t value) { m_y = value; }
std::string seek_object()
{
skip_space_and_comma();
auto substr_end = next_delimiter();
// next delimiter must eiter be '(' or "\w+\("
if (substr_end == m_str.end() || *substr_end != '(')
{
auto peeker = substr_end;
while (peeker != m_str.end() && *peeker == ' ') ++peeker;
if (peeker == m_str.end() || *peeker != '(')
{
throw_malformed("type name not followed by '('");
}
}
std::string result(m_pos, substr_end);
m_pos = substr_end;
return result;
}
void set_z(std::uint32_t value) { m_z = value; }
std::string peek_object()
{
std::string result = seek_object();
// restore position in stream
m_pos -= result.size();
return result;
}
};
void begin_object(const std::string&)
{
++m_obj_count;
skip_space_and_comma();
consume('(');
}
void plot(value_property* vp)
{
ft_value v(vp->type());
vp->get(v);
switch (v.type())
void end_object()
{
case ft_uint32: cout << ft_value_cast<ft_uint32>(v) << " "; break;
default: break;
consume(')');
if (--m_obj_count == 0)
{
skip_space_and_comma();
if (m_pos != m_str.end())
{
throw_malformed("expected end of of string");
}
}
}
}
void plot(property_ptr& pptr)
{
if (pptr.is_value_property())
size_t begin_list(primitive_type)
{
plot(pptr.as<value_property>());
consume('{');
auto list_end = std::find(m_pos, m_str.end(), '}');
return std::count(m_pos, list_end, ',') + 1;
}
}
void plot(abstract_object& what, const std::string& what_name)
{
cout << what_name << " (" << what.properties() << " properties): ";
for (size_t i = 0; i < what.properties(); ++i)
void end_list()
{
plot(what.property(i));
consume('}');
}
cout << endl;
struct from_string
{
const std::string& str;
from_string(const std::string& s) : str(s) { }
template<typename T>
void operator()(T& what)
{
std::istringstream s(str);
s >> what;
}
void operator()(std::string& what)
{
what = str;
}
void operator()(std::u16string&) { }
void operator()(std::u32string&) { }
};
pt_value read_value(primitive_type ptype)
{
skip_space_and_comma();
auto substr_end = std::find_if(m_pos, m_str.end(), [] (char c) -> bool {
switch (c)
{
case ')':
case '}':
case ' ':
case ',': return true;
default : return false;
}
});
std::string substr(m_pos, substr_end);
pt_value result(ptype);
result.apply(from_string(substr));
m_pos += substr.size();
return result;
}
};
template<typename T>
std::string to_string(T* what, meta_object<T>* mobj)
{
std::ostringstream osstr;
string_serializer strs(osstr);
mobj->serialize(what, &strs);
return osstr.str();
}
std::size_t test__serialization()
......@@ -1137,150 +1506,84 @@ std::size_t test__serialization()
CPPA_TEST(test__serialization);
CPPA_CHECK_EQUAL((trait::is_iterable<int>::value), false);
// std::string is primitive and thus not identified by is_iterable
CPPA_CHECK_EQUAL((trait::is_iterable<std::string>::value), false);
CPPA_CHECK_EQUAL((trait::is_iterable<std::list<int>>::value), true);
CPPA_CHECK_EQUAL((trait::is_iterable<std::map<int,int>>::value), true);
// test pt_value implementation
{
ft_value v1(42);
ft_value v2(42);
pt_value v1(42);
pt_value v2(42);
CPPA_CHECK_EQUAL(v1, v2);
CPPA_CHECK_EQUAL(v1, 42);
CPPA_CHECK_EQUAL(42, v2);
// type mismatch => unequal
CPPA_CHECK(v2 != static_cast<std::int8_t>(42));
}
auto manipulate_point = [&](abstract_object& pt) {
CPPA_CHECK_EQUAL(pt.properties(), 3);
if (pt.properties() == 3)
{
for (size_t i = 0; i < 3; ++i)
{
property_ptr& pptr = pt.property(i);
bool is_value_property = pptr.is_value_property();
CPPA_CHECK(is_value_property);
if (is_value_property)
{
CPPA_CHECK_EQUAL(pptr.as<value_property>()->type(),
ft_uint32);
if (i == 1)
{
auto vptr = pptr.as<value_property>();
// value must be 2
ft_value val(ft_uint32);
vptr->get(val);
CPPA_CHECK_EQUAL(ft_value_cast<ft_uint32>(val), 2);
vptr->set(ft_value(static_cast<std::uint32_t>(22)));
vptr->get(val);
CPPA_CHECK_EQUAL(ft_value_cast<ft_uint32>(val), 22);
}
}
}
}
};
root_object root;
// test as_value_property with direct member access
// test serializers / deserializers
{
point_struct pt = { 1, 2, 3 };
abstract_object abstract_pt { as_value_property(&pt.x),
as_value_property(&pt.y),
as_value_property(&pt.z) };
manipulate_point(abstract_pt);
CPPA_CHECK_EQUAL(pt.x, 1);
CPPA_CHECK_EQUAL(pt.y, 22);
CPPA_CHECK_EQUAL(pt.z, 3);
xml_sink xs;
xs.write(abstract_pt);
cout << "XML:" << endl << xs.str() << endl;
}
// test as_value_property with direct getter / setter implementations
{
point_class pt = { 1, 2, 4 };
abstract_object abstract_pt = {
as_value_property<ft_uint32>(&pt, &point_class::x, &point_class::set_x),
as_value_property<ft_uint32>(&pt, &point_class::y, &point_class::set_y),
as_value_property<ft_uint32>(&pt, &point_class::z, &point_class::set_z)
meta_object<struct_b> meta_b {
compound_member(&struct_b::a, &struct_a::x, &struct_a::y),
&struct_b::z,
&struct_b::ints
};
manipulate_point(abstract_pt);
CPPA_CHECK_EQUAL(pt.x(), 1);
CPPA_CHECK_EQUAL(pt.y(), 22);
CPPA_CHECK_EQUAL(pt.z(), 4);
}
{
std::string str = "Hello World";
std::unique_ptr<value_property> p(as_value_property(&str));
p->set(std::string("foobar"));
CPPA_CHECK_EQUAL(str, "foobar");
}
// "announce" meta types
s_meta_types["struct_b"] = &meta_b;
{
std::vector<std::int32_t> ints;
std::unique_ptr<list_property> p(as_list_property(&ints));
p->push_back(static_cast<std::int32_t>(1));
p->push_back(static_cast<std::int32_t>(2));
p->push_back(static_cast<std::int32_t>(3));
for (std::unique_ptr<list_property::iterator> i(p->begin()); !i->at_end(); i->next())
struct_b b1 = { { 1, 2 }, 3, { 4, 5, 6, 7, 8, 9, 10 } };
struct_b b2;
struct_b b3;
auto b1str = "struct_b ( struct_a ( 1, 2 ), 3, "
"{ 4, 5, 6, 7, 8, 9, 10 } )";
CPPA_CHECK_EQUAL((to_string(&b1, &meta_b)), b1str);
char buf[512];
// serialize b1 to buf
{
cout << ft_value_cast<ft_int32>(i->get()) << " ";
binary_serializer bs(buf);
meta_b.serialize(&b1, &bs);
}
cout << endl;
CPPA_CHECK_EQUAL(ints.size(), 3);
CPPA_CHECK_EQUAL(ints, (std::vector<std::int32_t>({1, 2, 3})));
}
{
std::map<std::int32_t, std::string> strings;
std::unique_ptr<map_property> p(as_map_property(&strings));
p->insert(static_cast<std::int32_t>(2), std::string("two"));
p->insert(static_cast<std::int32_t>(1), std::string("one"));
p->insert(static_cast<std::int32_t>(4), std::string("four"));
std::map<std::int32_t, std::string> verification_map = {
{ 1, "one" },
{ 2, "two" },
{ 4, "four" }
};
CPPA_CHECK_EQUAL(strings.size(), 3);
CPPA_CHECK_EQUAL(strings, verification_map);
// also check equality by iterators
if (strings.size() == verification_map.size())
// deserialize b2 from buf
{
auto siter = strings.begin();
auto send = strings.end();
std::unique_ptr<map_property::iterator> viter(p->begin());
while (siter != send)
binary_deserializer bd(buf, 512);
auto res = root.deserialize(&bd);
CPPA_CHECK_EQUAL(res.second, &meta_b);
if (res.second == &meta_b)
{
CPPA_CHECK_EQUAL(ft_value_cast<ft_int32>(viter->key()),
siter->first);
CPPA_CHECK_EQUAL(ft_value_cast<ft_u8string>(viter->value()),
siter->second);
viter->next();
++siter;
b2 = *reinterpret_cast<struct_b*>(res.first);
}
res.second->delete_instance(res.first);
}
}
ft_value v1("Hello World");
CPPA_CHECK_EQUAL(b1, b2);
CPPA_CHECK_EQUAL((to_string(&b2, &meta_b)), b1str);
auto plot_objects = [] (const std::vector<cppa::object>& objs)
{
cout << "{ ";
bool first = true;
for (const cppa::object& o : objs)
// deserialize b3 from string, using string_deserializer
{
if (first) first = false;
else cout << ", ";
cout << o.type().name() << "(";
if (o.type() == typeid(std::string))
cout << "\"" << o.to_string() << "\"";
else
cout << o.to_string();
cout << ")";
string_deserializer strd(b1str);
auto res = root.deserialize(&strd);
CPPA_CHECK_EQUAL(res.second, &meta_b);
if (res.second == &meta_b)
{
b3 = *reinterpret_cast<struct_b*>(res.first);
}
res.second->delete_instance(res.first);
}
cout << " }" << endl;
};
plot_objects({
cppa::uniform_typeid<int>()->create(),
cppa::uniform_typeid<std::string>()->create()
});
CPPA_CHECK_EQUAL(b1, b3);
}
return CPPA_TEST_RESULT;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment