Commit e83f6d6a authored by neverlord's avatar neverlord

documentation

parent f6038a61
...@@ -303,7 +303,7 @@ inline actor_ptr spawn(F&& what, const Args&... args) ...@@ -303,7 +303,7 @@ inline actor_ptr spawn(F&& what, const Args&... args)
} }
/** /**
* @copydoc context::quit(std::uint32_t) * @copydoc local_actor::quit()
* *
* Alias for <tt>self()->quit(reason);</tt> * Alias for <tt>self()->quit(reason);</tt>
*/ */
......
...@@ -12,7 +12,7 @@ namespace cppa { ...@@ -12,7 +12,7 @@ namespace cppa {
class object; class object;
/** /**
* @ingroup Serialize * @ingroup TypeSystem
* @brief Technology-independent deserialization interface. * @brief Technology-independent deserialization interface.
*/ */
class deserializer class deserializer
...@@ -72,7 +72,7 @@ class deserializer ...@@ -72,7 +72,7 @@ class deserializer
* @brief Reads a tuple of primitive values from the data * @brief Reads a tuple of primitive values from the data
* source of the types @p ptypes. * source of the types @p ptypes.
* @param num The size of the tuple. * @param num The size of the tuple.
* @param ptype Array of expected primitive data types. * @param ptypes Array of expected primitive data types.
* @param storage Array of size @p num, storing the result of this function. * @param storage Array of size @p num, storing the result of this function.
*/ */
virtual void read_tuple(size_t num, virtual void read_tuple(size_t num,
......
...@@ -24,6 +24,7 @@ class local_actor : public actor ...@@ -24,6 +24,7 @@ class local_actor : public actor
* Cause this actor to send an exit signal to all of its * Cause this actor to send an exit signal to all of its
* linked actors, sets its state to @c exited and throws * linked actors, sets its state to @c exited and throws
* {@link actor_exited} to cleanup the stack. * {@link actor_exited} to cleanup the stack.
* @param reason Exit reason that will be send to linked actors.
* @throws actor_exited * @throws actor_exited
*/ */
virtual void quit(std::uint32_t reason) = 0; virtual void quit(std::uint32_t reason) = 0;
......
...@@ -4,16 +4,30 @@ ...@@ -4,16 +4,30 @@
namespace cppa { namespace cppa {
/** /**
* @brief Includes integers (signed and unsigned), floating points * @ingroup TypeSystem
* @brief Represents a type flag of {@link primitive_variant}.
*
* Includes integers (signed and unsigned), floating points
* and unicode strings (std::string, std::u16string and std::u32string). * and unicode strings (std::string, std::u16string and std::u32string).
* @relates primitive_variant
*/ */
enum primitive_type enum primitive_type
{ {
pt_int8, pt_int16, pt_int32, pt_int64, pt_int8, /**< equivalent of @p std::int8_t */
pt_uint8, pt_uint16, pt_uint32, pt_uint64, pt_int16, /**< equivalent of @p std::int16_t */
pt_float, pt_double, pt_long_double, pt_int32, /**< equivalent of @p std::int32_t */
pt_u8string, pt_u16string, pt_u32string, pt_int64, /**< equivalent of @p std::int64_t */
pt_null pt_uint8, /**< equivalent of @p std::uint8_t */
pt_uint16, /**< equivalent of @p std::uint16_t */
pt_uint32, /**< equivalent of @p std::uint32_t */
pt_uint64, /**< equivalent of @p std::uint64_t */
pt_float, /**< equivalent of @p float */
pt_double, /**< equivalent of @p double */
pt_long_double, /**< equivalent of <tt>long double</tt> */
pt_u8string, /**< equivalent of @p std::string */
pt_u16string, /**< equivalent of @p std::u16string */
pt_u32string, /**< equivalent of @p std::u32string */
pt_null /**< equivalent of @p void */
}; };
constexpr const char* primitive_type_names[] = constexpr const char* primitive_type_names[] =
...@@ -25,6 +39,12 @@ constexpr const char* primitive_type_names[] = ...@@ -25,6 +39,12 @@ constexpr const char* primitive_type_names[] =
"pt_null" "pt_null"
}; };
/**
* @ingroup TypeSystem
* @brief Maps a #primitive_type value to its name.
* @param ptype Requestet @p primitive_type.
* @returns A C-string representation of @p ptype.
*/
constexpr const char* primitive_type_name(primitive_type ptype) constexpr const char* primitive_type_name(primitive_type ptype)
{ {
return primitive_type_names[static_cast<int>(ptype)]; return primitive_type_names[static_cast<int>(ptype)];
......
...@@ -31,13 +31,30 @@ void ptv_set(primitive_type& lhs_type, T& lhs, V&& rhs, ...@@ -31,13 +31,30 @@ void ptv_set(primitive_type& lhs_type, T& lhs, V&& rhs,
namespace cppa { namespace cppa {
class primitive_variant;
template<typename T>
const T& get(const primitive_variant& pv);
template<typename T>
T& get_ref(primitive_variant& pv);
/** /**
* @brief A stack-based union container for * @ingroup TypeSystem
* {@link primitive_type primitive data types}. * @brief An union container for primitive data types.
*/ */
class primitive_variant class primitive_variant
{ {
friend bool operator==(const primitive_variant& lhs,
const primitive_variant& rhs);
template<typename T>
friend const T& get(const primitive_variant& pv);
template<typename T>
friend T& get_ref(primitive_variant& pv);
primitive_type m_ptype; primitive_type m_ptype;
union union
...@@ -58,7 +75,7 @@ class primitive_variant ...@@ -58,7 +75,7 @@ class primitive_variant
std::u32string s32; std::u32string s32;
}; };
// use static call dispatching to select member variable // use static call dispatching to select member
inline decltype(i8)& get(util::pt_token<pt_int8>) { return i8; } inline decltype(i8)& get(util::pt_token<pt_int8>) { return i8; }
inline decltype(i16)& get(util::pt_token<pt_int16>) { return i16; } inline decltype(i16)& get(util::pt_token<pt_int16>) { return i16; }
inline decltype(i32)& get(util::pt_token<pt_int32>) { return i32; } inline decltype(i32)& get(util::pt_token<pt_int32>) { return i32; }
...@@ -103,18 +120,6 @@ class primitive_variant ...@@ -103,18 +120,6 @@ class primitive_variant
if (m_ptype != PT) throw std::logic_error("type check failed"); if (m_ptype != PT) throw std::logic_error("type check failed");
} }
public:
template<typename V>
void set(V&& value)
{
static constexpr primitive_type ptype = detail::type_to_ptype<V>::ptype;
util::pt_token<ptype> token;
static_assert(ptype != pt_null, "T is not a primitive type");
destroy();
detail::ptv_set<ptype>(m_ptype, get(token), std::forward<V>(value));
}
template<primitive_type PT> template<primitive_type PT>
typename detail::ptype_to_type<PT>::type& get_as() typename detail::ptype_to_type<PT>::type& get_as()
{ {
...@@ -133,21 +138,7 @@ class primitive_variant ...@@ -133,21 +138,7 @@ class primitive_variant
return const_cast<primitive_variant*>(this)->get(token); return const_cast<primitive_variant*>(this)->get(token);
} }
template<typename T> public:
explicit operator T()
{
static constexpr primitive_type ptype = detail::type_to_ptype<T>::ptype;
static_assert(ptype != pt_null, "V is not a primitive type");
return get_as<ptype>();
}
template<typename T>
explicit operator T() const
{
static constexpr primitive_type ptype = detail::type_to_ptype<T>::ptype;
static_assert(ptype != pt_null, "V is not a primitive type");
return get_as<ptype>();
}
template<typename Fun> template<typename Fun>
void apply(Fun&& f) void apply(Fun&& f)
...@@ -162,8 +153,17 @@ class primitive_variant ...@@ -162,8 +153,17 @@ class primitive_variant
applier<const primitive_variant, Fun>(this, f)); applier<const primitive_variant, Fun>(this, f));
} }
/**
* @brief Creates an empty variant.
* @post <tt>ptype() == pt_null && type() == typeid(void)</tt>.
*/
primitive_variant(); primitive_variant();
/**
* @brief Creates a variant from @p value.
* @param value A primitive value.
* @pre @p value does have a primitive type.
*/
template<typename V> template<typename V>
primitive_variant(V&& value) : m_ptype(pt_null) primitive_variant(V&& value) : m_ptype(pt_null)
{ {
...@@ -174,88 +174,162 @@ class primitive_variant ...@@ -174,88 +174,162 @@ class primitive_variant
std::forward<V>(value)); std::forward<V>(value));
} }
primitive_variant(primitive_type ptype); /**
* @brief Creates a variant with type pt.
* @param pt Requestet type.
* @post <tt>ptype() == pt</tt>.
*/
primitive_variant(primitive_type pt);
/**
* @brief Creates a copy from @p other.
* @param other A primitive variant.
*/
primitive_variant(const primitive_variant& other); primitive_variant(const primitive_variant& other);
/**
* @brief Creates a new variant and move the value from @p other to it.
* @param other A primitive variant rvalue.
*/
primitive_variant(primitive_variant&& other); primitive_variant(primitive_variant&& other);
/**
* @brief Moves @p value to this variant if @p value is an rvalue;
* otherwise copies the value of @p value.
* @param value A primitive value.
* @returns <tt>*this</tt>.
*/
template<typename V> template<typename V>
primitive_variant& operator=(V&& value) primitive_variant& operator=(V&& value)
{ {
static constexpr primitive_type ptype = detail::type_to_ptype<V>::ptype; static constexpr primitive_type ptype = detail::type_to_ptype<V>::ptype;
static_assert(ptype != pt_null, "V is not a primitive type"); static_assert(ptype != pt_null, "V is not a primitive type");
util::pt_token<ptype> token;
if (ptype == m_ptype) if (ptype == m_ptype)
{ {
util::pt_token<ptype> token;
get(token) = std::forward<V>(value); get(token) = std::forward<V>(value);
} }
else else
{ {
set(std::forward<V>(value)); destroy();
detail::ptv_set<ptype>(m_ptype, get(token), std::forward<V>(value));
//set(std::forward<V>(value));
} }
return *this; return *this;
} }
/**
* @brief Copies the content of @p other to @p this.
* @param other A primitive variant.
* @returns <tt>*this</tt>.
*/
primitive_variant& operator=(const primitive_variant& other); primitive_variant& operator=(const primitive_variant& other);
/**
* @brief Moves the content of @p other to @p this.
* @param other A primitive variant rvalue.
* @returns <tt>*this</tt>.
*/
primitive_variant& operator=(primitive_variant&& other); primitive_variant& operator=(primitive_variant&& other);
bool operator==(const primitive_variant& other) const; /**
* @brief Gets the {@link primitive_type type} of @p this.
inline bool operator!=(const primitive_variant& other) const * @returns The {@link primitive_type type} of @p this.
{ */
return !(*this == other);
}
inline primitive_type ptype() const { return m_ptype; } inline primitive_type ptype() const { return m_ptype; }
/**
* @brief Gets the RTTI type of @p this.
* @returns <tt>typeid(void)</tt> if <tt>ptype() == pt_null</tt>;
* otherwise typeid(T) is returned, where T is the C++ type
* of @p this.
*/
const std::type_info& type() const; const std::type_info& type() const;
~primitive_variant(); ~primitive_variant();
}; };
/**
* @ingroup TypeSystem
* @brief Casts a primitive variant to its C++ type.
* @relates primitive_variant
* @param pv A primitive variant of type @p T.
* @returns A const reference to the value of @p pv of type @p T.
* @throws <tt>std::logic_error</tt> if @p pv is not of type @p T.
*/
template<typename T> template<typename T>
T& get(primitive_variant& pv) const T& get(const primitive_variant& pv)
{ {
static const primitive_type ptype = detail::type_to_ptype<T>::ptype; static const primitive_type ptype = detail::type_to_ptype<T>::ptype;
return pv.get_as<ptype>(); return pv.get_as<ptype>();
} }
/**
* @ingroup TypeSystem
* @brief Casts a non-const primitive variant to its C++ type.
* @relates primitive_variant
* @param pv A primitive variant of type @p T.
* @returns A reference to the value of @p pv of type @p T.
* @throws <tt>std::logic_error</tt> if @p pv is not of type @p T.
*/
template<typename T> template<typename T>
const T& get(const primitive_variant& pv) T& get_ref(primitive_variant& pv)
{ {
static const primitive_type ptype = detail::type_to_ptype<T>::ptype; static const primitive_type ptype = detail::type_to_ptype<T>::ptype;
return pv.get_as<ptype>(); return pv.get_as<ptype>();
} }
#ifdef CPPA_DOCUMENTATION
/**
* @ingroup TypeSystem
* @brief Casts a primitive variant to its C++ type.
* @relates primitive_variant
* @tparam T C++ type equivalent of @p PT.
* @param pv A primitive variant of type @p T.
* @returns A const reference to the value of @p pv of type @p T.
*/
template<primitive_type PT>
const T& get_ref(const primitive_variant& pv);
/**
* @ingroup TypeSystem
* @brief Casts a non-const primitive variant to its C++ type.
* @relates primitive_variant
* @tparam T C++ type equivalent of @p PT.
* @param pv A primitive variant of type @p T.
* @returns A reference to the value of @p pv of type @p T.
*/
template<primitive_type PT> template<primitive_type PT>
typename detail::ptype_to_type<PT>::type& get(primitive_variant& pv) T& get_ref(primitive_variant& pv);
#else
template<primitive_type PT>
inline const typename detail::ptype_to_type<PT>::type&
get(const primitive_variant& pv)
{ {
static_assert(PT != pt_null, "PT == pt_null"); static_assert(PT != pt_null, "PT == pt_null");
return pv.get_as<PT>(); return get<typename detail::ptype_to_type<PT>::type>(pv);
} }
template<primitive_type PT> template<primitive_type PT>
const typename detail::ptype_to_type<PT>::type& get(const primitive_variant& pv) inline typename detail::ptype_to_type<PT>::type&
get_ref(primitive_variant& pv)
{ {
static_assert(PT != pt_null, "PT == pt_null"); static_assert(PT != pt_null, "PT == pt_null");
return pv.get_as<PT>(); return get_ref<typename detail::ptype_to_type<PT>::type>(pv);
} }
template<typename T> #endif
T& get_ref(primitive_variant& pv)
{
static const primitive_type ptype = detail::type_to_ptype<T>::ptype;
return pv.get_as<ptype>();
}
template<primitive_type PT> bool operator==(const primitive_variant& lhs, const primitive_variant& rhs);
typename detail::ptype_to_type<PT>::type& get_ref(primitive_variant& pv)
inline
bool operator!=(const primitive_variant& lhs, const primitive_variant& rhs)
{ {
static_assert(PT != pt_null, "PT == pt_null"); return !(lhs == rhs);
return pv.get_as<PT>();
} }
template<typename T> template<typename T>
...@@ -263,7 +337,7 @@ typename util::enable_if<util::is_primitive<T>, bool>::type ...@@ -263,7 +337,7 @@ typename util::enable_if<util::is_primitive<T>, bool>::type
operator==(const T& lhs, const primitive_variant& rhs) operator==(const T& lhs, const primitive_variant& rhs)
{ {
static constexpr primitive_type ptype = detail::type_to_ptype<T>::ptype; static constexpr primitive_type ptype = detail::type_to_ptype<T>::ptype;
static_assert(ptype != pt_null, "T couldn't be mapped to an ptype"); static_assert(ptype != pt_null, "T is an incompatible type");
return (rhs.ptype() == ptype) ? lhs == get<ptype>(rhs) : false; return (rhs.ptype() == ptype) ? lhs == get<ptype>(rhs) : false;
} }
......
...@@ -9,15 +9,11 @@ ...@@ -9,15 +9,11 @@
namespace cppa { namespace cppa {
/**
* @defgroup Serialize Serialization of custom data types.
*/
// forward declaration // forward declaration
class primitive_variant; class primitive_variant;
/** /**
* @ingroup Serialize * @ingroup TypeSystem
* @brief Technology-independent serialization interface. * @brief Technology-independent serialization interface.
*/ */
class serializer class serializer
......
...@@ -97,8 +97,6 @@ const uniform_type_info* uniform_typeid(const std::type_info&); ...@@ -97,8 +97,6 @@ const uniform_type_info* uniform_typeid(const std::type_info&);
* an unsupported data structure, you have to implement serialize/deserialize * an unsupported data structure, you have to implement serialize/deserialize
* by yourself. {@link announce_example_5.cpp Example 5} shows, how to * by yourself. {@link announce_example_5.cpp Example 5} shows, how to
* announce a tree data structure to the @p libcppa type system. * announce a tree data structure to the @p libcppa type system.
* Make sure to read the @ref Serialize "serialization section" of
* this documentation before.
*/ */
/** /**
......
...@@ -57,7 +57,7 @@ struct comparator ...@@ -57,7 +57,7 @@ struct comparator
{ {
if (rhs.ptype() == PT) if (rhs.ptype() == PT)
{ {
result = (lhs.get_as<PT>() == rhs.get_as<PT>()); result = (get<PT>(lhs) == get<PT>(rhs));
} }
} }
}; };
...@@ -72,7 +72,7 @@ struct initializer ...@@ -72,7 +72,7 @@ struct initializer
inline void operator()(util::pt_token<PT>) inline void operator()(util::pt_token<PT>)
{ {
typedef typename detail::ptype_to_type<PT>::type T; typedef typename detail::ptype_to_type<PT>::type T;
lhs.set(T()); lhs = T();
} }
}; };
...@@ -85,7 +85,7 @@ struct setter ...@@ -85,7 +85,7 @@ struct setter
template<typename T> template<typename T>
inline void operator()(const T& rhs) inline void operator()(const T& rhs)
{ {
lhs.set(rhs); lhs = rhs;
} }
}; };
...@@ -98,7 +98,7 @@ struct mover ...@@ -98,7 +98,7 @@ struct mover
template<typename T> template<typename T>
inline void operator()(T& rhs) inline void operator()(T& rhs)
{ {
lhs.set(std::move(rhs)); lhs = std::move(rhs);
} }
}; };
...@@ -135,10 +135,10 @@ primitive_variant& primitive_variant::operator=(primitive_variant&& other) ...@@ -135,10 +135,10 @@ primitive_variant& primitive_variant::operator=(primitive_variant&& other)
return *this; return *this;
} }
bool primitive_variant::operator==(const primitive_variant& other) const bool operator==(const primitive_variant& lhs, const primitive_variant& rhs)
{ {
comparator cmp(*this, other); comparator cmp(lhs, rhs);
util::pt_dispatch(m_ptype, cmp); util::pt_dispatch(lhs.m_ptype, cmp);
return cmp.result; return cmp.result;
} }
...@@ -146,7 +146,7 @@ const std::type_info& primitive_variant::type() const ...@@ -146,7 +146,7 @@ const std::type_info& primitive_variant::type() const
{ {
type_reader tr; type_reader tr;
apply(tr); apply(tr);
return (tr.tinfo) ? *tr.tinfo : typeid(void); return (tr.tinfo == nullptr) ? typeid(void) : *tr.tinfo;
} }
primitive_variant::~primitive_variant() primitive_variant::~primitive_variant()
......
...@@ -7,13 +7,14 @@ using namespace cppa; ...@@ -7,13 +7,14 @@ using namespace cppa;
size_t test__primitive_variant() size_t test__primitive_variant()
{ {
CPPA_TEST(test__primitive_variant); CPPA_TEST(test__primitive_variant);
std::uint32_t forty_two = 42; std::uint32_t forty_two = 42;
primitive_variant v1(forty_two); primitive_variant v1(forty_two);
primitive_variant v2(pt_uint32); primitive_variant v2(pt_uint32);
// type checking // type checking
CPPA_CHECK_EQUAL(v1.ptype(), pt_uint32); CPPA_CHECK_EQUAL(v1.ptype(), pt_uint32);
CPPA_CHECK_EQUAL(v2.ptype(), pt_uint32); CPPA_CHECK_EQUAL(v2.ptype(), pt_uint32);
get<std::uint32_t&>(v2) = forty_two; get_ref<std::uint32_t&>(v2) = forty_two;
CPPA_CHECK_EQUAL(v1, v2); CPPA_CHECK_EQUAL(v1, v2);
CPPA_CHECK_EQUAL(v1, forty_two); CPPA_CHECK_EQUAL(v1, forty_two);
CPPA_CHECK_EQUAL(forty_two, v2); CPPA_CHECK_EQUAL(forty_two, v2);
...@@ -23,7 +24,7 @@ size_t test__primitive_variant() ...@@ -23,7 +24,7 @@ size_t test__primitive_variant()
CPPA_CHECK_EQUAL(v1.ptype(), pt_u8string); CPPA_CHECK_EQUAL(v1.ptype(), pt_u8string);
v2 = "Hello"; v2 = "Hello";
CPPA_CHECK_EQUAL(v2.ptype(), pt_u8string); CPPA_CHECK_EQUAL(v2.ptype(), pt_u8string);
get<std::string>(v2) += " world"; get_ref<std::string>(v2) += " world";
CPPA_CHECK_EQUAL(v1, v2); CPPA_CHECK_EQUAL(v1, v2);
v2 = u"Hello World"; v2 = u"Hello World";
CPPA_CHECK(v1 != v2); CPPA_CHECK(v1 != v2);
......
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