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