Commit cc1a1fc6 authored by neverlord's avatar neverlord

serialization testing

parent 97aff6bf
This diff is collapsed.
......@@ -72,7 +72,7 @@ class default_uniform_type_info_impl : public uniform_type_info
object create() const
{
return { new T, this };
return { new T(), this };
}
};
......
......@@ -4,17 +4,17 @@
#include <string>
#include <typeinfo>
#include <stdexcept>
#include <type_traits>
#include "cppa/util/disjunction.hpp"
namespace cppa {
// forward declarations
class object;
class uniform_type_info;
template<typename T>
T& object_cast(object& obj);
template<typename T>
const T& object_cast(const object& obj);
namespace detail { template<typename T> struct object_caster; }
bool operator==(const uniform_type_info& lhs, const std::type_info& rhs);
/**
* @brief foobar.
......@@ -25,10 +25,7 @@ class object
friend class uniform_type_info;
template<typename T>
friend T& object_cast(object& obj);
template<typename T>
friend const T& object_cast(const object& obj);
friend struct detail::object_caster;
void* m_value;
const uniform_type_info* m_type;
......@@ -80,40 +77,102 @@ public:
};
// forward declaration of the == operator
bool operator==(const uniform_type_info& lhs, const std::type_info& rhs);
inline bool operator==(const object& lhs, const object& rhs)
{
return lhs.equal(rhs);
}
template<typename T>
T& object_cast(object& obj)
inline bool operator!=(const object& lhs, const object& rhs)
{
// if (!(obj.type() == typeid(T)))
if (!operator==(obj.type(), typeid(T)))
return !(lhs == rhs);
}
namespace detail {
inline void assert_type(const object& obj, const std::type_info& tinfo)
{
if (!(obj.type() == tinfo))
{
throw std::logic_error("object type doesnt match T");
}
return *reinterpret_cast<T*>(obj.m_value);
}
// get a const reference
template<typename T>
const T& object_cast(const object& obj)
struct object_caster<const T&>
{
if (!(obj.type() == typeid(T)))
static const T& _(const object& obj)
{
throw std::logic_error("object type doesnt match T");
assert_type(obj, typeid(T));
return *reinterpret_cast<const T*>(obj.m_value);
}
return *reinterpret_cast<const T*>(obj.m_value);
}
};
} // namespace cppa
// get a mutable reference
template<typename T>
struct object_caster<T&>
{
static T& _(object& obj)
{
assert_type(obj, typeid(T));
return *reinterpret_cast<T*>(obj.m_value);
}
};
inline bool operator==(const cppa::object& lhs, const cppa::object& rhs)
// get a const pointer
template<typename T>
struct object_caster<const T*>
{
return lhs.equal(rhs);
static const T* _(const object& obj)
{
assert_type(obj, typeid(T));
return reinterpret_cast<const T*>(obj.m_value);
}
};
// get a mutable pointer
template<typename T>
struct object_caster<T*>
{
static T* _(object& obj)
{
assert_type(obj, typeid(T));
return reinterpret_cast<T*>(obj.m_value);
}
};
template<typename T>
struct is_const_reference : std::false_type { };
template<typename T>
struct is_const_reference<const T&> : std::true_type { };
template<typename T>
struct is_const_pointer : std::false_type { };
template<typename T>
struct is_const_pointer<const T*> : std::true_type { };
}
inline bool operator!=(const cppa::object& lhs, const cppa::object& rhs)
template<typename T>
T object_cast(object& obj)
{
return !(lhs == rhs);
static_assert(util::disjunction<std::is_pointer<T>,
std::is_reference<T>>::value,
"T is neither a reference nor a pointer type.");
return detail::object_caster<T>::_(obj);
}
template<typename T>
const T& object_cast(const object& obj)
{
static_assert(util::disjunction<detail::is_const_pointer<T>,
detail::is_const_reference<T>>::value,
"T is neither a const reference nor a const pointer type.");
return detail::object_caster<T>::_(obj);
}
} // namespace cppa
#endif // OBJECT_HPP
......@@ -8,32 +8,32 @@ namespace cppa { namespace util {
template<typename Signature>
struct callable_trait;
template<class C, typename Result, typename... Args>
struct callable_trait<Result (C::*)(Args...) const>
template<class C, typename ResultType, typename... Args>
struct callable_trait<ResultType (C::*)(Args...) const>
{
typedef Result result_type;
typedef type_list<Args...> arg_types;
typedef ResultType result_type;
typedef type_list<Args...> arg_types;
};
template<class C, typename Result, typename... Args>
struct callable_trait<Result (C::*)(Args...)>
template<class C, typename ResultType, typename... Args>
struct callable_trait<ResultType (C::*)(Args...)>
{
typedef Result result_type;
typedef type_list<Args...> arg_types;
typedef ResultType result_type;
typedef type_list<Args...> arg_types;
};
template<typename Result, typename... Args>
struct callable_trait<Result (Args...)>
template<typename ResultType, typename... Args>
struct callable_trait<ResultType (Args...)>
{
typedef Result result_type;
typedef type_list<Args...> arg_types;
typedef ResultType result_type;
typedef type_list<Args...> arg_types;
};
template<typename Result, typename... Args>
struct callable_trait<Result (*)(Args...)>
template<typename ResultType, typename... Args>
struct callable_trait<ResultType (*)(Args...)>
{
typedef Result result_type;
typedef type_list<Args...> arg_types;
typedef ResultType result_type;
typedef type_list<Args...> arg_types;
};
} } // namespace cppa::util
......
......@@ -3,7 +3,7 @@
namespace cppa { namespace util {
template<bool Stmt, typename T>
template<bool Stmt, typename T = void>
struct enable_if_c { };
template<typename T>
......@@ -12,7 +12,7 @@ struct enable_if_c<true, T>
typedef T type;
};
template<class Trait, typename T>
template<class Trait, typename T = void>
struct enable_if : enable_if_c<Trait::value, T>
{
};
......
......@@ -314,7 +314,7 @@ uniform_type_info_map& s_uniform_type_info_map()
return s_utimap;
}
} } } // namespace detail::<anonymous>
} } } // namespace cppa::detail::<anonymous>
namespace {
......
This diff is collapsed.
......@@ -60,7 +60,6 @@ std::size_t test__type_list()
++i;
CPPA_CHECK((i == ifc.end()));
return CPPA_TEST_RESULT;
}
......@@ -25,9 +25,6 @@
using std::cout;
using std::endl;
using cppa::object;
using cppa::uniform_type_info;
namespace {
struct foo
......@@ -43,14 +40,16 @@ bool operator==(const foo& lhs, const foo& rhs)
} // namespace <anonymous>
using namespace cppa;
namespace {
static bool unused1 =
cppa::uniform_type_info::announce<foo>(
[] (cppa::serializer& s, const foo& f) {
uniform_type_info::announce<foo>(
[] (serializer& s, const foo& f) {
s << f.value;
},
[] (cppa::deserializer& d, foo& f) {
[] (deserializer& d, foo& f) {
d >> f.value;
},
[] (const foo& f) -> std::string {
......@@ -65,9 +64,9 @@ static bool unused1 =
return new foo(tmp);
}
);
bool unused2 = false;// = cppa::uniform_type_info::announce(typeid(foo), new uti_impl<foo>);
bool unused3 = false;//= cppa::uniform_type_info::announce(typeid(foo), new uti_impl<foo>);
bool unused4 = false;//= cppa::uniform_type_info::announce(typeid(foo), new uti_impl<foo>);
bool unused2 = false;// = uniform_type_info::announce(typeid(foo), new uti_impl<foo>);
bool unused3 = false;//= uniform_type_info::announce(typeid(foo), new uti_impl<foo>);
bool unused4 = false;//= uniform_type_info::announce(typeid(foo), new uti_impl<foo>);
} // namespace <anonymous>
......@@ -77,18 +76,21 @@ std::size_t test__uniform_type()
{
//bar.create_object();
object obj1 = cppa::uniform_typeid<foo>()->create();
object obj1 = uniform_typeid<foo>()->create();
object obj2(obj1);
CPPA_CHECK_EQUAL(obj1, obj2);
}
{
object wstr_obj1 = cppa::uniform_typeid<std::wstring>()->create();
cppa::object_cast<std::wstring>(wstr_obj1) = L"hello wstring!";
object wstr_obj2 = cppa::uniform_typeid<std::wstring>()->from_string("hello wstring!");
object wstr_obj1 = uniform_typeid<std::wstring>()->create();
object_cast<std::wstring&>(wstr_obj1) = L"hello wstring!";
object wstr_obj2 = uniform_typeid<std::wstring>()->from_string("hello wstring!");
CPPA_CHECK_EQUAL(wstr_obj1, wstr_obj2);
const object& obj2_ref = wstr_obj2;
CPPA_CHECK_EQUAL(object_cast<std::wstring&>(wstr_obj1),
object_cast<const std::wstring&>(obj2_ref));
// couldn't be converted to ASCII
cppa::object_cast<std::wstring>(wstr_obj1) = L"hello wstring\x05D4";
object_cast<std::wstring&>(wstr_obj1) = L"hello wstring\x05D4";
std::string narrowed = wstr_obj1.to_string();
CPPA_CHECK_EQUAL(narrowed, "hello wstring?");
}
......@@ -102,15 +104,15 @@ std::size_t test__uniform_type()
// test foo_object implementation
/*
obj_ptr o = cppa::uniform_typeid<foo>()->create();
obj_ptr o = uniform_typeid<foo>()->create();
o->from_string("123");
CPPA_CHECK_EQUAL(o->to_string(), "123");
int val = reinterpret_cast<const foo*>(o->value())->value;
CPPA_CHECK_EQUAL(val, 123);
*/
// these types (and only those) are present if the uniform_type_info
// implementation is correct
// these types (and only those) are present if
// the uniform_type_info implementation is correct
std::set<std::string> expected =
{
"@_::foo", // name of <anonymous namespace>::foo
......@@ -118,7 +120,7 @@ std::size_t test__uniform_type()
"@u8", "@u16", "@u32", "@u64", // unsigned integer names
"@str", "@wstr", // strings
"float", "double", // floating points
"@0", // cppa::util::void_type
"@0", // util::void_type
// default announced cppa types
"cppa::any_type",
"cppa::intrusive_ptr<cppa::actor>"
......@@ -134,8 +136,8 @@ std::size_t test__uniform_type()
std::set<std::string> found;
// fetch all available type names
auto types = cppa::uniform_type_info::instances();
for (cppa::uniform_type_info* tinfo : types)
auto types = uniform_type_info::instances();
for (uniform_type_info* tinfo : types)
{
found.insert(tinfo->name());
}
......@@ -145,7 +147,23 @@ std::size_t test__uniform_type()
if (expected.size() == found.size())
{
CPPA_CHECK((std::equal(found.begin(), found.end(), expected.begin())));
bool expected_equals_found = std::equal(found.begin(),
found.end(),
expected.begin());
CPPA_CHECK(expected_equals_found);
if (!expected_equals_found)
{
cout << "found:" << endl;
for (const std::string& tname : found)
{
cout << " - " << tname << endl;
}
cout << "expected: " << endl;
for (const std::string& tname : expected)
{
cout << " - " << tname << endl;
}
}
}
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