Commit 4a354707 authored by neverlord's avatar neverlord

maintenance and documentation

parent a0de91c7
...@@ -57,7 +57,7 @@ class any_tuple ...@@ -57,7 +57,7 @@ class any_tuple
const cow_ptr<detail::abstract_tuple>& vals() const; const cow_ptr<detail::abstract_tuple>& vals() const;
bool equal_to(const any_tuple& other) const; bool equals(const any_tuple& other) const;
any_tuple tail(size_t offset = 1) const; any_tuple tail(size_t offset = 1) const;
...@@ -76,7 +76,7 @@ class any_tuple ...@@ -76,7 +76,7 @@ class any_tuple
inline bool operator==(const any_tuple& lhs, const any_tuple& rhs) inline bool operator==(const any_tuple& lhs, const any_tuple& rhs)
{ {
return lhs.equal_to(rhs); return lhs.equals(rhs);
} }
inline bool operator!=(const any_tuple& lhs, const any_tuple& rhs) inline bool operator!=(const any_tuple& lhs, const any_tuple& rhs)
......
...@@ -20,7 +20,7 @@ struct abstract_tuple : ref_counted ...@@ -20,7 +20,7 @@ struct abstract_tuple : ref_counted
virtual const void* at(size_t pos) const = 0; virtual const void* at(size_t pos) const = 0;
virtual const uniform_type_info& utype_info_at(size_t pos) const = 0; virtual const uniform_type_info& utype_info_at(size_t pos) const = 0;
virtual bool equal_to(const abstract_tuple& other) const; virtual bool equals(const abstract_tuple& other) const;
}; };
......
...@@ -57,7 +57,7 @@ class decorated_tuple : public abstract_tuple ...@@ -57,7 +57,7 @@ class decorated_tuple : public abstract_tuple
return m_decorated->utype_info_at(m_mappings[pos]); return m_decorated->utype_info_at(m_mappings[pos]);
} }
virtual bool equal_to(const abstract_tuple&) const virtual bool equals(const abstract_tuple&) const
{ {
return false; return false;
} }
......
...@@ -12,7 +12,7 @@ struct empty_tuple : cppa::detail::abstract_tuple ...@@ -12,7 +12,7 @@ struct empty_tuple : cppa::detail::abstract_tuple
void* mutable_at(size_t); void* mutable_at(size_t);
abstract_tuple* copy() const; abstract_tuple* copy() const;
const void* at(size_t) const; const void* at(size_t) const;
bool equal_to(const abstract_tuple& other) const; bool equals(const abstract_tuple& other) const;
const uniform_type_info& utype_info_at(size_t) const; const uniform_type_info& utype_info_at(size_t) const;
}; };
......
...@@ -36,7 +36,7 @@ class object_array : public detail::abstract_tuple ...@@ -36,7 +36,7 @@ class object_array : public detail::abstract_tuple
const void* at(size_t pos) const; const void* at(size_t pos) const;
bool equal_to(const cppa::detail::abstract_tuple&) const; bool equals(const cppa::detail::abstract_tuple&) const;
const uniform_type_info& utype_info_at(size_t pos) const; const uniform_type_info& utype_info_at(size_t pos) const;
......
...@@ -232,7 +232,7 @@ bool do_match(pattern_arg& pargs, tuple_iterator_arg<VectorType>& targs) ...@@ -232,7 +232,7 @@ bool do_match(pattern_arg& pargs, tuple_iterator_arg<VectorType>& targs)
{ {
// compare values if needed // compare values if needed
if ( pargs.has_value() == false if ( pargs.has_value() == false
|| pargs.type()->equal(pargs.value(), targs.value())) || pargs.type()->equals(pargs.value(), targs.value()))
{ {
targs.push_mapping(); targs.push_mapping();
// ok, go to next iteration // ok, go to next iteration
......
...@@ -74,7 +74,7 @@ class tuple_vals : public abstract_tuple ...@@ -74,7 +74,7 @@ class tuple_vals : public abstract_tuple
return m_types.at(pos); return m_types.at(pos);
} }
bool equal_to(const abstract_tuple& other) const bool equals(const abstract_tuple& other) const
{ {
if (size() != other.size()) return false; if (size() != other.size()) return false;
const tuple_vals* o = dynamic_cast<const tuple_vals*>(&other); const tuple_vals* o = dynamic_cast<const tuple_vals*>(&other);
...@@ -82,7 +82,7 @@ class tuple_vals : public abstract_tuple ...@@ -82,7 +82,7 @@ class tuple_vals : public abstract_tuple
{ {
return m_data == (o->m_data); return m_data == (o->m_data);
} }
return abstract_tuple::equal_to(other); return abstract_tuple::equals(other);
} }
}; };
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <string> #include <string>
#include <cstdint> #include <cstdint>
#include <exception> #include <exception>
#include <stdexcept>
namespace cppa { namespace cppa {
...@@ -13,7 +14,15 @@ namespace cppa { ...@@ -13,7 +14,15 @@ namespace cppa {
class exception : public std::exception class exception : public std::exception
{ {
std::string m_what; public:
~exception() throw();
/**
* @brief Returns the error message.
* @returns The error message as C-string.
*/
const char* what() const throw();
protected: protected:
...@@ -29,58 +38,70 @@ class exception : public std::exception ...@@ -29,58 +38,70 @@ class exception : public std::exception
*/ */
exception(const std::string& what_str); exception(const std::string& what_str);
public: private:
~exception() throw(); std::string m_what;
/**
* @brief Returns the error message.
* @returns The error message as C-string.
*/
const char* what() const throw();
}; };
/** /**
* @brief Thrown if an Actor finished execution. * @brief Thrown if an actor finished execution.
*/ */
class actor_exited : public exception class actor_exited : public exception
{ {
std::uint32_t m_reason;
public: public:
actor_exited(std::uint32_t exit_reason); actor_exited(std::uint32_t exit_reason);
/** /**
* * @brief Gets the exit reason.
* @returns The exit reason of the terminating actor either set via
* {@link quit} or by a special (exit) message.
*/ */
inline std::uint32_t reason() const throw(); inline std::uint32_t reason() const throw();
private:
std::uint32_t m_reason;
}; };
class network_exception : public exception /**
* @brief Thrown to indicate that either an actor publishing failed or
* @p libcppa was unable to connect to a remote host.
*/
class network_error : public exception
{ {
public: public:
network_exception(std::string&& what_str); network_error(std::string&& what_str);
network_exception(const std::string& what_str); network_error(const std::string& what_str);
}; };
class bind_failure : public network_exception /**
* @brief Thrown to indicate that an actor publishing failed because
* the requested port could not be used.
*/
class bind_failure : public network_error
{ {
int m_errno;
public: public:
bind_failure(int bind_errno); bind_failure(int bind_errno);
/**
* @brief Gets the socket API error code.
* @returns The errno set by <tt>bind()</tt>.
*/
inline int error_code() const throw(); inline int error_code() const throw();
private:
int m_errno;
}; };
inline std::uint32_t actor_exited::reason() const throw() inline std::uint32_t actor_exited::reason() const throw()
......
...@@ -64,6 +64,7 @@ inline void local_actor::trap_exit(bool new_value) ...@@ -64,6 +64,7 @@ inline void local_actor::trap_exit(bool new_value)
/** /**
* @brief Get a pointer to the current active context. * @brief Get a pointer to the current active context.
* @returns A pointer that identifies the calling actor.
*/ */
local_actor* self(); local_actor* self();
......
...@@ -6,44 +6,53 @@ ...@@ -6,44 +6,53 @@
#include <stdexcept> #include <stdexcept>
#include <type_traits> #include <type_traits>
#include "cppa/exception.hpp"
#include "cppa/util/rm_ref.hpp"
#include "cppa/util/disjunction.hpp" #include "cppa/util/disjunction.hpp"
#include "cppa/detail/implicit_conversions.hpp"
namespace cppa { namespace cppa {
// forward declarations // forward declarations
class object; class object;
bool operator==(const object& lhs, const object& rhs);
class uniform_type_info; class uniform_type_info;
const uniform_type_info* uniform_typeid(const std::type_info&); const uniform_type_info* uniform_typeid(const std::type_info&);
namespace detail { template<typename T> struct object_caster; }
bool operator==(const uniform_type_info& lhs, const std::type_info& rhs); bool operator==(const uniform_type_info& lhs, const std::type_info& rhs);
/** /**
* @brief A wrapper around a void pointer that stores type informations * @brief Grants mutable access to the stored value of @p obj.
* and provides copy, move and comparsion operations. * @param obj {@link object Object} with <tt>obj.type() == typeid(T)</tt>.
* @returns A mutable reference to the value stored in @p obj.
* @relates object
* @throws std::invalid_argument if <tt>obj.type() != typeid(T)</tt>
*/ */
class object template<typename T>
{ T& get_ref(object& obj);
friend class uniform_type_info;
template<typename T>
friend struct detail::object_caster;
void* m_value;
const uniform_type_info* m_type;
void swap(object& other); /**
* @brief Grants const access to the stored value of @p obj.
* @param obj {@link object Object} with <tt>obj.type() == typeid(T)</tt>.
* @returns A const reference to the value stored in @p obj.
* @relates object
* @throws std::invalid_argument if <tt>obj.type() != typeid(T)</tt>
*/
template<typename T>
const T& get(const object& obj);
/* /**
* @brief Copies this object. * @brief An abstraction class that stores an instance of
* @returns A (deep) copy of this object. * an announced type.
*/ */
object copy() const; class object
{
static void* new_instance(const uniform_type_info* type, friend bool operator==(const object& lhs, const object& rhs);
const void* from);
public: public:
~object();
/** /**
* @brief Creates an object of type @p utinfo with value @p val. * @brief Creates an object of type @p utinfo with value @p val.
...@@ -54,70 +63,81 @@ public: ...@@ -54,70 +63,81 @@ public:
/** /**
* @brief Creates an empty object. * @brief Creates an empty object.
* @post {@code empty() && type() == *uniform_typeid<util::void_type>()} * @post {type() == *uniform_typeid<util::void_type>()}
*/ */
object(); object();
/**
* @brief Creates an object with a copy of @p what.
* @post {@code empty() == false && type() == *uniform_typeid<T>()}
*/
template<typename T>
explicit object(const T& what);
~object();
/** /**
* @brief Creates an object and moves type and value * @brief Creates an object and moves type and value
* from @p other to @c this. * from @p other to @c this.
* @post {@code other.empty() == true}
*/ */
object(object&& other); object(object&& other);
/** /**
* @brief Creates a copy of @p other. * @brief Creates a (deep) copy of @p other.
* @post {@code type() == other.type() && equal_to(other)} * @post {*this == other}
*/ */
object(const object& other); object(const object& other);
/** /**
* @brief Moves the content from @p other to this. * @brief Moves the content from @p other to this.
* @returns @p *this * @returns @p *this
* @post {@code other.empty() == true}
*/ */
object& operator=(object&& other); object& operator=(object&& other);
/** /**
* * @brief Creates a (deep) copy of @p other and assigns it to @p this.
* @return @p *this
*/ */
object& operator=(const object& other); object& operator=(const object& other);
bool equal_to(const object& other) const; /**
* @brief Gets the RTTI of this object.
* @returns A {@link uniform_type_info} describing the current
* type of @p this.
*/
const uniform_type_info& type() const; const uniform_type_info& type() const;
/**
* @brief Gets the stored value.
* @returns A const pointer to the currently stored value.
* @see get(const object&)
*/
const void* value() const; const void* value() const;
/**
* @brief Gets the stored value.
* @returns A mutable pointer to the currently stored value.
* @see get_ref(object&)
*/
void* mutable_value(); void* mutable_value();
bool empty() const; /**
* @brief Creates an object from @p what.
* @param what Value of an announced type.
* @returns An object whose value was initialized with @p what.
* @post {@code type() == *uniform_typeid<T>()}
* @throws std::runtime_error if @p T is not announced.
*/
template<typename T>
static object from(T&& what);
private:
void* m_value;
const uniform_type_info* m_type;
void swap(object& other);
}; };
template<typename T> template<typename T>
object::object(const T& what) object object::from(T&& what)
{ {
m_type = uniform_typeid(typeid(T)); typedef typename util::rm_ref<T>::type plain_type;
if (!m_type) typedef typename detail::implicit_conversions<plain_type>::type value_type;
{ auto rtti = uniform_typeid(typeid(value_type)); // throws on error
throw std::logic_error("unknown/unannounced type"); return { new value_type(std::forward<T>(what)), rtti };
}
m_value = new_instance(m_type, &what);
}
inline bool operator==(const object& lhs, const object& rhs)
{
return lhs.equal_to(rhs);
} }
inline bool operator!=(const object& lhs, const object& rhs) inline bool operator!=(const object& lhs, const object& rhs)
...@@ -125,25 +145,16 @@ inline bool operator!=(const object& lhs, const object& rhs) ...@@ -125,25 +145,16 @@ inline bool operator!=(const object& lhs, const object& rhs)
return !(lhs == rhs); 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");
}
}
} // namespace detail
template<typename T> template<typename T>
T& get_ref(object& obj) T& get_ref(object& obj)
{ {
static_assert(util::disjunction<std::is_pointer<T>, static_assert(util::disjunction<std::is_pointer<T>,
std::is_reference<T>>::value == false, std::is_reference<T>>::value == false,
"T is a reference or a pointer type."); "T is a reference or a pointer type.");
detail::assert_type(obj, typeid(T)); if (!(obj.type() == typeid(T)))
{
throw std::invalid_argument("obj.type() != typeid(T)");
}
return *reinterpret_cast<T*>(obj.mutable_value()); return *reinterpret_cast<T*>(obj.mutable_value());
} }
...@@ -152,8 +163,11 @@ const T& get(const object& obj) ...@@ -152,8 +163,11 @@ const T& get(const object& obj)
{ {
static_assert(util::disjunction<std::is_pointer<T>, static_assert(util::disjunction<std::is_pointer<T>,
std::is_reference<T>>::value == false, std::is_reference<T>>::value == false,
"T is a reference a pointer type."); "T is a reference or a pointer type.");
detail::assert_type(obj, typeid(T)); if (!(obj.type() == typeid(T)))
{
throw std::invalid_argument("obj.type() != typeid(T)");
}
return *reinterpret_cast<const T*>(obj.value()); return *reinterpret_cast<const T*>(obj.value());
} }
......
...@@ -82,14 +82,16 @@ class scheduler ...@@ -82,14 +82,16 @@ class scheduler
}; };
/** /**
* @brief Sets the scheduler to @p sched; * @brief Sets the scheduler to @p sched.
* @param sched A user-defined scheduler implementation.
* @pre <tt>sched != nullptr</tt>.
* @throws std::runtime_error if there's already a scheduler defined. * @throws std::runtime_error if there's already a scheduler defined.
*/ */
void set_scheduler(scheduler* sched); void set_scheduler(scheduler* sched);
/** /**
* @brief Gets the actual used scheduler implementation. * @brief Gets the actual used scheduler implementation.
* @returns The active scheduler (default constructed). * @returns The active scheduler (usually default constructed).
*/ */
scheduler* get_scheduler(); scheduler* get_scheduler();
......
...@@ -11,6 +11,11 @@ std::string to_string_impl(const void* what, const uniform_type_info* utype); ...@@ -11,6 +11,11 @@ std::string to_string_impl(const void* what, const uniform_type_info* utype);
} // namespace detail } // namespace detail
/**
* @brief Serializes a value to a string.
* @param what A value of an announced type.
* @returns A string representation of @p what.
*/
template<typename T> template<typename T>
std::string to_string(const T& what) std::string to_string(const T& what)
{ {
......
...@@ -43,7 +43,7 @@ const uniform_type_info* uniform_typeid(const std::type_info&); ...@@ -43,7 +43,7 @@ const uniform_type_info* uniform_typeid(const std::type_info&);
* *
* @code * @code
* // creates a signed, 32 bit integer * // creates a signed, 32 bit integer
* cppa::object i = cppa::uniform_type_info::by_uniform_name("@i32")->create(); * cppa::object i = cppa::uniform_type_info::by_name("@i32")->create();
* @endcode * @endcode
* *
* However, you should rarely if ever need to use {@link cppa::object object} * However, you should rarely if ever need to use {@link cppa::object object}
...@@ -116,48 +116,13 @@ const uniform_type_info* uniform_typeid(const std::type_info&); ...@@ -116,48 +116,13 @@ const uniform_type_info* uniform_typeid(const std::type_info&);
* e.g.: <tt>namespace { class foo { }; }</tt> is mapped to * e.g.: <tt>namespace { class foo { }; }</tt> is mapped to
* @c \@_::foo * @c \@_::foo
*/ */
class uniform_type_info : cppa::util::comparable<uniform_type_info> class uniform_type_info
{ {
friend class object; friend class object;
// needs access to by_type_info() friend bool operator==(const uniform_type_info& lhs,
friend const uniform_type_info* uniform_typeid(const std::type_info&); const uniform_type_info& rhs);
public:
class identifier : cppa::util::comparable<identifier>
{
friend class uniform_type_info;
int m_value;
identifier(int val) : m_value(val) { }
// disable assignment operators
identifier& operator=(const identifier&) = delete;
public:
// enable copy constructor (only)
identifier(const identifier&) = default;
// 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;
// uniform type name
std::string m_name;
// disable copy and move constructors // disable copy and move constructors
uniform_type_info(uniform_type_info&&) = delete; uniform_type_info(uniform_type_info&&) = delete;
...@@ -167,20 +132,25 @@ class uniform_type_info : cppa::util::comparable<uniform_type_info> ...@@ -167,20 +132,25 @@ class uniform_type_info : cppa::util::comparable<uniform_type_info>
uniform_type_info& operator=(uniform_type_info&&) = delete; uniform_type_info& operator=(uniform_type_info&&) = delete;
uniform_type_info& operator=(const uniform_type_info&) = delete; uniform_type_info& operator=(const uniform_type_info&) = delete;
static const uniform_type_info* by_type_info(const std::type_info& tinfo);
protected:
uniform_type_info(const std::string& uniform_name);
public: public:
virtual ~uniform_type_info();
/** /**
* @brief Get instance by uniform name. * @brief Get instance by uniform name.
* @param uniform_name The @p libcppa internal name for a type. * @param uniform_name The @p libcppa internal name for a type.
* @returns The instance associated to @p uniform_name. * @returns The instance associated to @p uniform_name.
* @throws std::runtime_error if no type named @p uniform_name was found.
*/ */
static uniform_type_info* by_uniform_name(const std::string& uniform_name); static uniform_type_info* from(const std::string& uniform_name);
/**
* @brief Get instance by std::type_info.
* @param tinfo A STL RTTI object.
* @returns An instance describing the same type as @p tinfo.
* @throws std::runtime_error if @p tinfo is not an announced type.
*/
static const uniform_type_info* from(const std::type_info& tinfo);
/** /**
* @brief Get all instances. * @brief Get all instances.
...@@ -188,26 +158,12 @@ class uniform_type_info : cppa::util::comparable<uniform_type_info> ...@@ -188,26 +158,12 @@ class uniform_type_info : cppa::util::comparable<uniform_type_info>
*/ */
static std::vector<uniform_type_info*> instances(); static std::vector<uniform_type_info*> instances();
virtual ~uniform_type_info();
/** /**
* @brief Get the internal @p libcppa name for this type. * @brief Get the internal @p libcppa name for this type.
* @returns A string describing the @p libcppa internal type name. * @returns A string describing the @p libcppa internal type name.
*/ */
inline const std::string& name() const { return m_name; } inline const std::string& name() const { return m_name; }
/**
* @brief Get the unique identifier of this instance.
* @returns 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 Creates an object of this type. * @brief Creates an object of this type.
*/ */
...@@ -219,41 +175,62 @@ class uniform_type_info : cppa::util::comparable<uniform_type_info> ...@@ -219,41 +175,62 @@ class uniform_type_info : cppa::util::comparable<uniform_type_info>
object deserialize(deserializer* source) const; object deserialize(deserializer* source) const;
/** /**
* @brief Compares two instances of this type. * @brief Determines if this uniform_type_info describes the same
* type than @p tinfo.
* @returns @p true if @p tinfo describes the same type as @p this.
*/ */
virtual bool equal(const void* instance1, const void* instance2) const = 0; virtual bool equals(const std::type_info& tinfo) const = 0;
protected:
/** /**
* @brief Cast @p instance to the native type and delete it. * @brief Compares two instances of this type.
* @param instance1 Left hand operand.
* @param instance2 Right hand operand.
* @returns @p true if <tt>*instance1 == *instance2</tt>.
* @pre @p instance1 and @p instance2 have the type of @p this.
*/ */
virtual void delete_instance(void* instance) const = 0; virtual bool equals(const void* instance1, const void* instance2) const = 0;
/** /**
* @brief Creates an instance of this type, either as a copy of * @brief Serializes @p instance to @p sink.
* @p instance or initialized with the default constructor * @param instance Instance of this type.
* if @p instance @c == @c nullptr. * @param sink Target data sink.
* @pre @p instance has the type of @p this.
*/ */
virtual void* new_instance(const void* instance = nullptr) const = 0; virtual void serialize(const void* instance, serializer* sink) const = 0;
public:
/** /**
* @brief Determines if this uniform_type_info describes the same * @brief Deserializes @p instance from @p source.
* type than @p tinfo. * @param instance Instance of this type.
* @param sink Data source.
* @pre @p instance has the type of @p this.
*/ */
virtual bool equal(const std::type_info& tinfo) const = 0; virtual void deserialize(void* instance, deserializer* source) const = 0;
protected:
uniform_type_info(const std::string& uniform_name);
/** /**
* @brief Serializes @p instance to @p sink. * @brief Casts @p instance to the native type and deletes it.
* @param instance Instance of this type.
* @pre @p instance has the type of @p this.
*/ */
virtual void serialize(const void* instance, serializer* sink) const = 0; virtual void delete_instance(void* instance) const = 0;
/** /**
* @brief Deserializes @p instance from @p source. * @brief Creates an instance of this type, either as a copy of
* @p instance or initialized with the default constructor
* if <tt>instance == nullptr</tt>.
* @param instance Optional instance of this type.
* @returns Either a copy of @p instance or a new instance, initialized
* with the default constructor.
* @pre @p instance has the type of @p this or is set to @p nullptr.
*/ */
virtual void deserialize(void* instance, deserializer* source) const = 0; virtual void* new_instance(const void* instance = nullptr) const = 0;
private:
std::string m_name;
}; };
...@@ -263,21 +240,38 @@ inline const uniform_type_info* uniform_typeid() ...@@ -263,21 +240,38 @@ inline const uniform_type_info* uniform_typeid()
return uniform_typeid(typeid(T)); return uniform_typeid(typeid(T));
} }
bool operator==(const uniform_type_info& lhs, const std::type_info& rhs); inline bool operator==(const uniform_type_info& lhs,
const uniform_type_info& rhs)
{
// uniform_type_info instances are singletons,
// thus, equal == identical
return &lhs == &rhs;
}
inline bool operator!=(const uniform_type_info& lhs, const std::type_info& rhs) inline bool operator!=(const uniform_type_info& lhs,
const uniform_type_info& rhs)
{ {
return !(lhs == rhs); return !(lhs == rhs);
} }
inline bool operator==(const uniform_type_info& lhs, const std::type_info& rhs)
{
return lhs.equals(rhs);
}
inline bool operator!=(const uniform_type_info& lhs, const std::type_info& rhs)
{
return !(lhs.equals(rhs));
}
inline bool operator==(const std::type_info& lhs, const uniform_type_info& rhs) inline bool operator==(const std::type_info& lhs, const uniform_type_info& rhs)
{ {
return rhs == lhs; return rhs.equals(lhs);
} }
inline bool operator!=(const std::type_info& lhs, const uniform_type_info& rhs) inline bool operator!=(const std::type_info& lhs, const uniform_type_info& rhs)
{ {
return !(rhs == lhs); return !(rhs.equals(lhs));
} }
} // namespace cppa } // namespace cppa
......
...@@ -32,7 +32,7 @@ class abstract_uniform_type_info : public uniform_type_info ...@@ -32,7 +32,7 @@ class abstract_uniform_type_info : public uniform_type_info
{ {
} }
bool equal(const void* lhs, const void* rhs) const bool equals(const void* lhs, const void* rhs) const
{ {
return deref(lhs) == deref(rhs); return deref(lhs) == deref(rhs);
} }
...@@ -49,7 +49,7 @@ class abstract_uniform_type_info : public uniform_type_info ...@@ -49,7 +49,7 @@ class abstract_uniform_type_info : public uniform_type_info
public: public:
bool equal(const std::type_info& tinfo) const bool equals(const std::type_info& tinfo) const
{ {
return typeid(T) == tinfo; return typeid(T) == tinfo;
} }
......
...@@ -81,15 +81,7 @@ struct tree ...@@ -81,15 +81,7 @@ struct tree
// tree nodes are equals if values and all values of all children are equal // tree nodes are equals if values and all values of all children are equal
bool operator==(const tree_node& lhs, const tree_node& rhs) bool operator==(const tree_node& lhs, const tree_node& rhs)
{ {
if (lhs.value == rhs.value && lhs.children.size() == rhs.children.size()) return (lhs.value == rhs.value) && (lhs.children == rhs.children);
{
for (std::uint32_t i = 0; i < lhs.children.size(); ++i)
{
if (!(lhs.children[i] == rhs.children[i])) return false;
}
return true;
}
return false;
} }
bool operator==(const tree& lhs, const tree& rhs) bool operator==(const tree& lhs, const tree& rhs)
...@@ -144,7 +136,7 @@ class tree_type_info : public util::abstract_uniform_type_info<tree> ...@@ -144,7 +136,7 @@ class tree_type_info : public util::abstract_uniform_type_info<tree>
void serialize_node(const tree_node& node, serializer* sink) const void serialize_node(const tree_node& node, serializer* sink) const
{ {
// serialize { value, number of children } ... children ... // value, ... children ...
sink->write_value(node.value); sink->write_value(node.value);
sink->begin_sequence(node.children.size()); sink->begin_sequence(node.children.size());
for (const tree_node& subnode : node.children) for (const tree_node& subnode : node.children)
...@@ -156,7 +148,7 @@ class tree_type_info : public util::abstract_uniform_type_info<tree> ...@@ -156,7 +148,7 @@ class tree_type_info : public util::abstract_uniform_type_info<tree>
void deserialize_node(tree_node& node, deserializer* source) const void deserialize_node(tree_node& node, deserializer* source) const
{ {
// deserialize { value, number of children } ... children ... // value, ... children ...
auto value = get<std::uint32_t>(source->read_value(pt_uint32)); auto value = get<std::uint32_t>(source->read_value(pt_uint32));
node.value = value; node.value = value;
auto num_children = source->begin_sequence(); auto num_children = source->begin_sequence();
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace cppa { namespace detail { namespace cppa { namespace detail {
bool abstract_tuple::equal_to(const abstract_tuple &other) const bool abstract_tuple::equals(const abstract_tuple &other) const
{ {
if (this == &other) return true; if (this == &other) return true;
if (size() != other.size()) return false; if (size() != other.size()) return false;
...@@ -13,7 +13,7 @@ bool abstract_tuple::equal_to(const abstract_tuple &other) const ...@@ -13,7 +13,7 @@ bool abstract_tuple::equal_to(const abstract_tuple &other) const
auto lhs = at(i); auto lhs = at(i);
auto rhs = other.at(i); auto rhs = other.at(i);
// compare first addresses, then values // compare first addresses, then values
if (lhs != rhs && !(uti.equal(lhs, rhs))) return false; if (lhs != rhs && !(uti.equals(lhs, rhs))) return false;
} }
return true; return true;
} }
......
...@@ -111,9 +111,9 @@ const cow_ptr<detail::abstract_tuple>& any_tuple::vals() const ...@@ -111,9 +111,9 @@ const cow_ptr<detail::abstract_tuple>& any_tuple::vals() const
return m_vals; return m_vals;
} }
bool any_tuple::equal_to(const any_tuple& other) const bool any_tuple::equals(const any_tuple& other) const
{ {
return m_vals->equal_to(*other.vals()); return m_vals->equals(*other.vals());
} }
} // namespace cppa } // namespace cppa
...@@ -14,7 +14,7 @@ deserializer::~deserializer() ...@@ -14,7 +14,7 @@ deserializer::~deserializer()
deserializer& operator>>(deserializer& d, object& what) deserializer& operator>>(deserializer& d, object& what)
{ {
std::string tname = d.peek_object(); std::string tname = d.peek_object();
auto mtype = uniform_type_info::by_uniform_name(tname); auto mtype = uniform_type_info::from(tname);
if (mtype == nullptr) if (mtype == nullptr)
{ {
throw std::logic_error("no uniform type info found for " + tname); throw std::logic_error("no uniform type info found for " + tname);
......
...@@ -28,7 +28,7 @@ const uniform_type_info& empty_tuple::utype_info_at(size_t) const ...@@ -28,7 +28,7 @@ const uniform_type_info& empty_tuple::utype_info_at(size_t) const
throw std::range_error("empty_tuple::type_at()"); throw std::range_error("empty_tuple::type_at()");
} }
bool empty_tuple::equal_to(const abstract_tuple& other) const bool empty_tuple::equals(const abstract_tuple& other) const
{ {
return other.size() == 0; return other.size() == 0;
} }
......
...@@ -57,16 +57,16 @@ actor_exited::actor_exited(std::uint32_t reason) : exception(ae_what(reason)) ...@@ -57,16 +57,16 @@ actor_exited::actor_exited(std::uint32_t reason) : exception(ae_what(reason))
m_reason = reason; m_reason = reason;
} }
network_exception::network_exception(const std::string& str) : exception(str) network_error::network_error(const std::string& str) : exception(str)
{ {
} }
network_exception::network_exception(std::string&& str) network_error::network_error(std::string&& str)
: exception(std::move(str)) : exception(std::move(str))
{ {
} }
bind_failure::bind_failure(int err_code) : network_exception(be_what(err_code)) bind_failure::bind_failure(int err_code) : network_error(be_what(err_code))
{ {
m_errno = err_code; m_errno = err_code;
} }
......
...@@ -19,17 +19,6 @@ void object::swap(object& other) ...@@ -19,17 +19,6 @@ void object::swap(object& other)
std::swap(m_type, other.m_type); std::swap(m_type, other.m_type);
} }
void* object::new_instance(const uniform_type_info* type, const void* from)
{
return type->new_instance(from);
}
object object::copy() const
{
return (m_value != &s_void) ? object(m_type->new_instance(m_value), m_type)
: object();
}
object::object(void* val, const uniform_type_info* utype) object::object(void* val, const uniform_type_info* utype)
: m_value(val), m_type(utype) : m_value(val), m_type(utype)
{ {
...@@ -51,8 +40,11 @@ object::~object() ...@@ -51,8 +40,11 @@ object::~object()
object::object(const object& other) : m_value(&s_void), m_type(uniform_typeid<util::void_type>()) object::object(const object& other) : m_value(&s_void), m_type(uniform_typeid<util::void_type>())
{ {
object tmp = other.copy(); if (other.value() != &s_void)
swap(tmp); {
m_value = other.m_type->new_instance(other.m_value);
m_type = other.m_type;
}
} }
object::object(object&& other) : m_value(&s_void), m_type(uniform_typeid<util::void_type>()) object::object(object&& other) : m_value(&s_void), m_type(uniform_typeid<util::void_type>())
...@@ -69,17 +61,19 @@ object& object::operator=(object&& other) ...@@ -69,17 +61,19 @@ object& object::operator=(object&& other)
object& object::operator=(const object& other) object& object::operator=(const object& other)
{ {
object tmp = other.copy(); // use copy ctor and then swap
object tmp(other);
swap(tmp); swap(tmp);
return *this; return *this;
} }
bool object::equal_to(const object& other) const bool operator==(const object& lhs, const object& rhs)
{ {
if (m_type == other.m_type) if (lhs.type() == rhs.type())
{ {
return (m_value != &s_void) ? m_type->equal(m_value, other.m_value) // values might both point to s_void if lhs and rhs are "empty"
: true; return lhs.value() == rhs.value()
|| lhs.type().equals(lhs.value(), rhs.value());
} }
return false; return false;
} }
...@@ -89,11 +83,6 @@ const uniform_type_info& object::type() const ...@@ -89,11 +83,6 @@ const uniform_type_info& object::type() const
return *m_type; return *m_type;
} }
bool object::empty() const
{
return m_value == &s_void;
}
const void* object::value() const const void* object::value() const
{ {
return m_value; return m_value;
......
...@@ -46,7 +46,7 @@ const void* object_array::at(size_t pos) const ...@@ -46,7 +46,7 @@ const void* object_array::at(size_t pos) const
return m_elements[pos].value(); return m_elements[pos].value();
} }
bool object_array::equal_to(const cppa::detail::abstract_tuple& ut) const bool object_array::equals(const cppa::detail::abstract_tuple& ut) const
{ {
if (size() == ut.size()) if (size() == ut.size())
{ {
...@@ -55,7 +55,7 @@ bool object_array::equal_to(const cppa::detail::abstract_tuple& ut) const ...@@ -55,7 +55,7 @@ bool object_array::equal_to(const cppa::detail::abstract_tuple& ut) const
const uniform_type_info& utype = utype_info_at(i); const uniform_type_info& utype = utype_info_at(i);
if (utype == ut.utype_info_at(i)) if (utype == ut.utype_info_at(i))
{ {
if (!utype.equal(at(i), ut.at(i))) return false; if (!utype.equals(at(i), ut.at(i))) return false;
} }
else else
{ {
......
...@@ -462,7 +462,7 @@ object from_string(const std::string& what) ...@@ -462,7 +462,7 @@ object from_string(const std::string& what)
{ {
string_deserializer strd(what); string_deserializer strd(what);
std::string uname = strd.peek_object(); std::string uname = strd.peek_object();
auto utype = uniform_type_info::by_uniform_name(uname); auto utype = uniform_type_info::from(uname);
if (utype == nullptr) if (utype == nullptr)
{ {
throw std::logic_error(uname + " is not announced"); throw std::logic_error(uname + " is not announced");
......
...@@ -86,7 +86,7 @@ void publish(actor_ptr& whom, std::uint16_t port) ...@@ -86,7 +86,7 @@ void publish(actor_ptr& whom, std::uint16_t port)
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) if (sockfd < 0)
{ {
throw network_exception("could not create server socket"); throw network_error("could not create server socket");
} }
// sguard closes the socket if an exception occurs // sguard closes the socket if an exception occurs
socket_guard sguard(sockfd); socket_guard sguard(sockfd);
...@@ -97,11 +97,11 @@ void publish(actor_ptr& whom, std::uint16_t port) ...@@ -97,11 +97,11 @@ void publish(actor_ptr& whom, std::uint16_t port)
int flags = fcntl(sockfd, F_GETFL, 0); int flags = fcntl(sockfd, F_GETFL, 0);
if (flags == -1) if (flags == -1)
{ {
throw network_exception("unable to get socket flags"); throw network_error("unable to get socket flags");
} }
if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1) if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1)
{ {
throw network_exception("unable to set socket to nonblocking"); throw network_error("unable to set socket to nonblocking");
} }
if (bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) if (bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0)
{ {
...@@ -109,7 +109,7 @@ void publish(actor_ptr& whom, std::uint16_t port) ...@@ -109,7 +109,7 @@ void publish(actor_ptr& whom, std::uint16_t port)
} }
if (listen(sockfd, 10) != 0) if (listen(sockfd, 10) != 0)
{ {
throw network_exception("listen() failed"); throw network_error("listen() failed");
} }
// ok, no exceptions // ok, no exceptions
sguard.release(); sguard.release();
...@@ -129,14 +129,14 @@ actor_ptr remote_actor(const char* host, std::uint16_t port) ...@@ -129,14 +129,14 @@ actor_ptr remote_actor(const char* host, std::uint16_t port)
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) if (sockfd < 0)
{ {
throw network_exception("socket creation failed"); throw network_error("socket creation failed");
} }
server = gethostbyname(host); server = gethostbyname(host);
if (!server) if (!server)
{ {
std::string errstr = "no such host: "; std::string errstr = "no such host: ";
errstr += host; errstr += host;
throw network_exception(std::move(errstr)); throw network_error(std::move(errstr));
} }
memset(&serv_addr, 0, sizeof(serv_addr)); memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET; serv_addr.sin_family = AF_INET;
...@@ -144,7 +144,7 @@ actor_ptr remote_actor(const char* host, std::uint16_t port) ...@@ -144,7 +144,7 @@ actor_ptr remote_actor(const char* host, std::uint16_t port)
serv_addr.sin_port = htons(port); serv_addr.sin_port = htons(port);
if (connect(sockfd, (const sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) if (connect(sockfd, (const sockaddr*) &serv_addr, sizeof(serv_addr)) < 0)
{ {
throw network_exception("could not connect to host"); throw network_error("could not connect to host");
} }
auto pinf = process_information::get(); auto pinf = process_information::get();
std::uint32_t process_id = pinf->process_id(); std::uint32_t process_id = pinf->process_id();
......
...@@ -51,10 +51,6 @@ istream& operator>>(istream& i, cppa::util::void_type&) { return i; } ...@@ -51,10 +51,6 @@ istream& operator>>(istream& i, cppa::util::void_type&) { return i; }
namespace { namespace {
std::atomic<int> s_ids;
inline int next_id() { return s_ids.fetch_add(1); }
inline cppa::detail::uniform_type_info_map& uti_map() inline cppa::detail::uniform_type_info_map& uti_map()
{ {
return *(cppa::detail::singleton_manager::get_uniform_type_info_map()); return *(cppa::detail::singleton_manager::get_uniform_type_info_map());
...@@ -416,7 +412,7 @@ class any_tuple_tinfo : public util::abstract_uniform_type_info<any_tuple> ...@@ -416,7 +412,7 @@ class any_tuple_tinfo : public util::abstract_uniform_type_info<any_tuple>
for (size_t i = 0; i < tuple_size; ++i) for (size_t i = 0; i < tuple_size; ++i)
{ {
auto tname = source->peek_object(); auto tname = source->peek_object();
auto utype = uniform_type_info::by_uniform_name(tname); auto utype = uniform_type_info::from(tname);
result->push_back(utype->deserialize(source)); result->push_back(utype->deserialize(source));
} }
source->end_sequence(); source->end_sequence();
...@@ -570,7 +566,7 @@ class int_tinfo : public detail::default_uniform_type_info_impl<T> ...@@ -570,7 +566,7 @@ class int_tinfo : public detail::default_uniform_type_info_impl<T>
public: public:
bool equal(const std::type_info& tinfo) const bool equals(const std::type_info& tinfo) const
{ {
// TODO: string comparsion sucks & is slow; find a nicer solution // TODO: string comparsion sucks & is slow; find a nicer solution
auto map_iter = uti_map().int_names().find(sizeof(T)); auto map_iter = uti_map().int_names().find(sizeof(T));
...@@ -778,8 +774,7 @@ bool announce(const std::type_info& tinfo, uniform_type_info* utype) ...@@ -778,8 +774,7 @@ bool announce(const std::type_info& tinfo, uniform_type_info* utype)
return uti_map().insert({ raw_name(tinfo) }, utype); return uti_map().insert({ raw_name(tinfo) }, utype);
} }
uniform_type_info::uniform_type_info(const std::string& uname) uniform_type_info::uniform_type_info(const std::string& uname) : m_name(uname)
: m_id(next_id()), m_name(uname)
{ {
} }
...@@ -793,7 +788,7 @@ object uniform_type_info::create() const ...@@ -793,7 +788,7 @@ object uniform_type_info::create() const
} }
const uniform_type_info* const uniform_type_info*
uniform_type_info::by_type_info(const std::type_info& tinf) uniform_type_info::from(const std::type_info& tinf)
{ {
auto result = uti_map().by_raw_name(raw_name(tinf)); auto result = uti_map().by_raw_name(raw_name(tinf));
if (!result) if (!result)
...@@ -806,7 +801,7 @@ uniform_type_info::by_type_info(const std::type_info& tinf) ...@@ -806,7 +801,7 @@ uniform_type_info::by_type_info(const std::type_info& tinf)
return result; return result;
} }
uniform_type_info* uniform_type_info::by_uniform_name(const std::string& name) uniform_type_info* uniform_type_info::from(const std::string& name)
{ {
auto result = uti_map().by_uniform_name(name); auto result = uti_map().by_uniform_name(name);
if (!result) if (!result)
...@@ -830,12 +825,7 @@ std::vector<uniform_type_info*> uniform_type_info::instances() ...@@ -830,12 +825,7 @@ std::vector<uniform_type_info*> uniform_type_info::instances()
const uniform_type_info* uniform_typeid(const std::type_info& tinfo) const uniform_type_info* uniform_typeid(const std::type_info& tinfo)
{ {
return uniform_type_info::by_type_info(tinfo); return uniform_type_info::from(tinfo);
}
bool operator==(const uniform_type_info& lhs, const std::type_info& rhs)
{
return lhs.equal(rhs);
} }
} // namespace cppa } // namespace cppa
...@@ -118,8 +118,8 @@ size_t test__serialization() ...@@ -118,8 +118,8 @@ size_t test__serialization()
CPPA_TEST(test__serialization); CPPA_TEST(test__serialization);
auto oarr = new detail::object_array; auto oarr = new detail::object_array;
oarr->push_back(object(static_cast<std::uint32_t>(42))); oarr->push_back(object::from(static_cast<std::uint32_t>(42)));
oarr->push_back(object(std::string("foo"))); oarr->push_back(object::from("foo" ));
any_tuple atuple1(oarr); any_tuple atuple1(oarr);
try try
......
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