Commit 60c07ea1 authored by Dominik Charousset's avatar Dominik Charousset

Force all types to be serializable or whiteslisted

Relates #388
parent d17d2b03
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2015 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_ALLOWED_UNSAFE_MESSAGE_TYPE_HPP
#define CAF_ALLOWED_UNSAFE_MESSAGE_TYPE_HPP
#include <type_traits>
namespace caf {
///
template <class T>
struct allowed_unsafe_message_type : std::false_type {};
} // namespace caf
#define CAF_ALLOW_UNSAFE_MESSAGE_TYPE(type_name) \
namespace caf { \
template <> \
struct allowed_unsafe_message_type<type_name> : std::true_type {}; \
}
#endif // CAF_ALLOWED_UNSAFE_MESSAGE_TYPE_HPP
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "caf/make_counted.hpp" #include "caf/make_counted.hpp"
#include "caf/skip_message.hpp" #include "caf/skip_message.hpp"
#include "caf/index_mapping.hpp" #include "caf/index_mapping.hpp"
#include "caf/allowed_unsafe_message_type.hpp"
#include "caf/detail/int_list.hpp" #include "caf/detail/int_list.hpp"
#include "caf/detail/apply_args.hpp" #include "caf/detail/apply_args.hpp"
...@@ -383,27 +384,41 @@ struct unbox_message_element<atom_constant<V>, 0> { ...@@ -383,27 +384,41 @@ struct unbox_message_element<atom_constant<V>, 0> {
using type = atom_value; using type = atom_value;
}; };
///
template <class T>
struct is_serializable_or_whitelisted {
static constexpr bool value = detail::is_serializable<T>::value
|| allowed_unsafe_message_type<T>::value;
};
/// Returns a new `message` containing the values `(x, xs...)`. /// Returns a new `message` containing the values `(x, xs...)`.
/// @relates message /// @relates message
template <class V, class... Ts> template <class T, class... Ts>
typename std::enable_if< typename std::enable_if<
! std::is_same<message, typename std::decay<V>::type>::value ! std::is_same<message, typename std::decay<T>::type>::value
|| (sizeof...(Ts) > 0), || (sizeof...(Ts) > 0),
message message
>::type >::type
make_message(V&& x, Ts&&... xs) { make_message(T&& x, Ts&&... xs) {
using namespace caf::detail; using namespace caf::detail;
using stored_types = using stored_types =
type_list< type_list<
typename unbox_message_element< typename unbox_message_element<
typename strip_and_convert<V>::type typename strip_and_convert<T>::type
>::type, >::type,
typename unbox_message_element< typename unbox_message_element<
typename strip_and_convert<Ts>::type typename strip_and_convert<Ts>::type
>::type... >::type...
>; >;
static_assert(tl_forall<stored_types, is_serializable_or_whitelisted>::value,
"at least one type is not serializable via free "
"'serialize(InOrOut&, T&, const unsigned int)' or "
"`T::serialize(InOrOut&, const unsigned int)` "
"member function; you can whitelist individual types by "
"specializing `caf::allowed_unsafe_message_type<T>` "
"or using the macro CAF_ALLOW_UNSAFE_MESSAGE_TYPE");
using storage = typename tl_apply<stored_types, tuple_vals>::type; using storage = typename tl_apply<stored_types, tuple_vals>::type;
auto ptr = make_counted<storage>(std::forward<V>(x), std::forward<Ts>(xs)...); auto ptr = make_counted<storage>(std::forward<T>(x), std::forward<Ts>(xs)...);
return message{detail::message_data::cow_ptr{std::move(ptr)}}; return message{detail::message_data::cow_ptr{std::move(ptr)}};
} }
......
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