File: make_message.hpp

package info (click to toggle)
actor-framework 0.17.6-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 77,684; sh: 674; python: 309; makefile: 13
file content (128 lines) | stat: -rw-r--r-- 4,673 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2018 Dominik Charousset                                     *
 *                                                                            *
 * 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.                                      *
 ******************************************************************************/

#pragma once

#include <tuple>
#include <sstream>
#include <type_traits>

#include "caf/allowed_unsafe_message_type.hpp"
#include "caf/detail/build_config.hpp"
#include "caf/detail/is_complete.hpp"
#include "caf/detail/tuple_vals.hpp"
#include "caf/detail/type_traits.hpp"
#include "caf/message.hpp"
#include "caf/type_id.hpp"

namespace caf {

/// Unboxes atom constants, i.e., converts `atom_constant<V>` to `V`.
/// @relates message
template <class T, int IsPlaceholderRes = std::is_placeholder<T>::value>
struct unbox_message_element {
  using type = index_mapping;
};

template <class T>
struct unbox_message_element<T, 0> {
  using type = T;
};

template <atom_value V>
struct unbox_message_element<atom_constant<V>, 0> {
  using type = atom_value;
};

template <>
struct unbox_message_element<actor_control_block*, 0> {
  using type = strong_actor_ptr;
};

/// @private
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...)`.
/// @relates message
template <class T, class... Ts>
typename std::enable_if<
  !std::is_same<message, typename std::decay<T>::type>::value
  || (sizeof...(Ts) > 0),
  message
>::type
make_message(T&& x, Ts&&... xs) {
  using namespace caf::detail;
  using stored_types =
    type_list<
      typename unbox_message_element<
        typename strip_and_convert<T>::type
      >::type,
      typename unbox_message_element<
        typename strip_and_convert<Ts>::type
      >::type...
    >;
  static_assert(tl_forall<stored_types, is_serializable_or_whitelisted>::value,
                "at least one type is neither inspectable via "
                "inspect(Inspector&, T&) nor serializable via "
                "'serialize(Processor&, T&, const unsigned int)' or "
                "`T::serialize(Processor&, const unsigned int)`; "
                "you can whitelist individual types by "
                "specializing `caf::allowed_unsafe_message_type<T>` "
                "or using the macro CAF_ALLOW_UNSAFE_MESSAGE_TYPE");
#ifdef CAF_ENABLE_TYPE_ID_CHECKS
  static_assert(tl_forall<stored_types, has_type_id>::value,
                "at least one type has no type ID: please assign type IDs "
                "to all of your types via CAF_ADD_TYPE_ID (this check was "
                "enabled via CAF_ENABLE_TYPE_ID_CHECKS)");
#endif
  using storage = typename tl_apply<stored_types, tuple_vals>::type;
  auto ptr = make_counted<storage>(std::forward<T>(x), std::forward<Ts>(xs)...);
  return message{detail::message_data::cow_ptr{std::move(ptr)}};
}

/// Returns a copy of @p other.
/// @relates message
inline message make_message(message other) {
  return other;
}

/// Returns an empty `message`.
/// @relates message
inline message make_message() {
  return message{};
}

struct message_factory {
  template <class... Ts>
  message operator()(Ts&&... xs) const {
    return make_message(std::forward<Ts>(xs)...);
  }
};

/// Converts the tuple `xs` to a message.
template <class... Ts>
message make_message_from_tuple(std::tuple<Ts...> xs) {
  message_factory f;
  return detail::apply_moved_args(f, detail::get_indices(xs), xs);
}

} // namespace caf