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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| 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 <new>
#include <functional>
#include <utility>
#include "caf/expected.hpp"
#include "caf/typed_actor.hpp"
#include "caf/scoped_actor.hpp"
#include "caf/response_type.hpp"
namespace caf {
template <class T>
class function_view_storage {
public:
using type = function_view_storage;
function_view_storage(T& storage) : storage_(&storage) {
// nop
}
void operator()(T& x) {
*storage_ = std::move(x);
}
private:
T* storage_;
};
template <class... Ts>
class function_view_storage<std::tuple<Ts...>> {
public:
using type = function_view_storage;
function_view_storage(std::tuple<Ts...>& storage) : storage_(&storage) {
// nop
}
void operator()(Ts&... xs) {
*storage_ = std::forward_as_tuple(std::move(xs)...);
}
private:
std::tuple<Ts...>* storage_;
};
template <>
class function_view_storage<unit_t> {
public:
using type = function_view_storage;
function_view_storage(unit_t&) {
// nop
}
void operator()() {
// nop
}
};
struct function_view_storage_catch_all {
message* storage_;
function_view_storage_catch_all(message& ptr) : storage_(&ptr) {
// nop
}
result<message> operator()(message_view& xs) {
*storage_ = xs.move_content_to_message();
return message{};
}
};
template <>
class function_view_storage<message> {
public:
using type = catch_all<function_view_storage_catch_all>;
};
template <class T>
struct function_view_flattened_result {
using type = T;
};
template <class T>
struct function_view_flattened_result<std::tuple<T>> {
using type = T;
};
template <>
struct function_view_flattened_result<std::tuple<void>> {
using type = unit_t;
};
template <class T>
using function_view_flattened_result_t =
typename function_view_flattened_result<T>::type;
template <class T>
struct function_view_result {
T value;
};
template <class... Ts>
struct function_view_result<typed_actor<Ts...>> {
typed_actor<Ts...> value{nullptr};
};
/// A function view for an actor hides any messaging from the caller.
/// Internally, a function view uses a `scoped_actor` and uses
/// blocking send and receive operations.
/// @experimental
template <class Actor>
class function_view {
public:
using type = Actor;
function_view(duration rel_timeout = infinite) : timeout(rel_timeout) {
// nop
}
function_view(type impl, duration rel_timeout = infinite)
: timeout(rel_timeout),
impl_(std::move(impl)) {
new_self(impl_);
}
~function_view() {
if (impl_)
self_.~scoped_actor();
}
function_view(function_view&& x)
: timeout(x.timeout),
impl_(std::move(x.impl_)) {
if (impl_) {
new (&self_) scoped_actor(impl_.home_system()); //(std::move(x.self_));
x.self_.~scoped_actor();
}
}
function_view& operator=(function_view&& x) {
timeout = x.timeout;
assign(x.impl_);
x.reset();
return *this;
}
/// Sends a request message to the assigned actor and returns the result.
template <class... Ts,
class R =
function_view_flattened_result_t<
typename response_type<
typename type::signatures,
detail::implicit_conversions_t<
typename std::decay<Ts>::type
>...
>::tuple_type>>
expected<R> operator()(Ts&&... xs) {
if (!impl_)
return sec::bad_function_call;
error err;
function_view_result<R> result;
self_->request(impl_, timeout, std::forward<Ts>(xs)...).receive(
[&](error& x) {
err = std::move(x);
},
typename function_view_storage<R>::type{result.value}
);
if (err)
return err;
return flatten(result.value);
}
void assign(type x) {
if (!impl_ && x)
new_self(x);
if (impl_ && !x)
self_.~scoped_actor();
impl_.swap(x);
}
void reset() {
self_.~scoped_actor();
impl_ = type();
}
/// Checks whether this function view has an actor assigned to it.
explicit operator bool() const {
return static_cast<bool>(impl_);
}
/// Returns the associated actor handle.
type handle() const {
return impl_;
}
duration timeout;
private:
template <class T>
T&& flatten(T& x) {
return std::move(x);
}
template <class T>
T&& flatten(std::tuple<T>& x) {
return std::move(get<0>(x));
}
void new_self(const Actor& x) {
if (x)
new (&self_) scoped_actor(x->home_system());
}
union { scoped_actor self_; };
type impl_;
};
/// @relates function_view
template <class T>
bool operator==(const function_view<T>& x, std::nullptr_t) {
return !x;
}
/// @relates function_view
template <class T>
bool operator==(std::nullptr_t x, const function_view<T>& y) {
return y == x;
}
/// @relates function_view
template <class T>
bool operator!=(const function_view<T>& x, std::nullptr_t y) {
return !(x == y);
}
/// @relates function_view
template <class T>
bool operator!=(std::nullptr_t x, const function_view<T>& y) {
return !(y == x);
}
/// Creates a new function view for `x`.
/// @relates function_view
/// @experimental
template <class T>
function_view<T> make_function_view(const T& x, duration t = infinite) {
return {x, t};
}
} // namespace caf
|