/usr/include/caf/spawn.hpp is in libcaf-dev 0.13.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | /******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| 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_SPAWN_HPP
#define CAF_SPAWN_HPP
#include <type_traits>
#include "caf/spawn_fwd.hpp"
#include "caf/typed_actor.hpp"
#include "caf/make_counted.hpp"
#include "caf/spawn_options.hpp"
#include "caf/typed_event_based_actor.hpp"
#include "caf/detail/logging.hpp"
#include "caf/detail/type_traits.hpp"
#include "caf/detail/typed_actor_util.hpp"
#include "caf/detail/implicit_conversions.hpp"
namespace caf {
class execution_unit;
/**
* Returns a newly spawned instance of type `C` using `xs...` as constructor
* arguments. The instance will be added to the job list of `host`. However,
* before the instance is launched, `before_launch_fun` will be called, e.g.,
* to join a group before the actor is running.
*/
template <class C, spawn_options Os, class BeforeLaunch, class... Ts>
intrusive_ptr<C> spawn_impl(execution_unit* host,
BeforeLaunch before_launch_fun, Ts&&... xs) {
static_assert(!std::is_base_of<blocking_actor, C>::value
|| has_blocking_api_flag(Os),
"C is derived from blocking_actor but "
"spawned without blocking_api_flag");
static_assert(is_unbound(Os),
"top-level spawns cannot have monitor or link flag");
CAF_LOGF_TRACE("");
auto ptr = make_counted<C>(std::forward<Ts>(xs)...);
CAF_LOGF_DEBUG("spawned actor with ID " << ptr->id());
CAF_PUSH_AID(ptr->id());
if (has_priority_aware_flag(Os)) {
ptr->is_priority_aware(true);
}
if (has_detach_flag(Os) || has_blocking_api_flag(Os)) {
ptr->is_detached(true);
}
before_launch_fun(ptr.get());
ptr->launch(host, has_lazy_init_flag(Os), has_hide_flag(Os));
return ptr;
}
/**
* Converts `scoped_actor` and pointers to actors to handles of type `actor`
* but simply forwards any other argument in the same way `std::forward` does.
*/
template <class T>
typename std::conditional<
is_convertible_to_actor<typename std::decay<T>::type>::value,
actor,
T&&
>::type
spawn_fwd(typename std::remove_reference<T>::type& arg) noexcept {
return static_cast<T&&>(arg);
}
/**
* Converts `scoped_actor` and pointers to actors to handles of type `actor`
* but simply forwards any other argument in the same way `std::forward` does.
*/
template <class T>
typename std::conditional<
is_convertible_to_actor<typename std::decay<T>::type>::value,
actor,
T&&
>::type
spawn_fwd(typename std::remove_reference<T>::type&& arg) noexcept {
static_assert(!std::is_lvalue_reference<T>::value,
"silently converting an lvalue to an rvalue");
return static_cast<T&&>(arg);
}
/**
* Called by `spawn` when used to create a class-based actor (usually
* should not be called by users of the library). This function
* simply forwards its arguments to `spawn_impl` using `spawn_fwd`.
*/
template <class C, spawn_options Os, typename BeforeLaunch, class... Ts>
intrusive_ptr<C> spawn_class(execution_unit* host,
BeforeLaunch before_launch_fun, Ts&&... xs) {
return spawn_impl<C, Os>(host, before_launch_fun,
spawn_fwd<Ts>(xs)...);
}
/**
* Called by `spawn` when used to create a functor-based actor (usually
* should not be called by users of the library). This function
* selects a proper implementation class and then delegates to `spawn_class`.
*/
template <spawn_options Os, typename BeforeLaunch, typename F, class... Ts>
actor spawn_functor(execution_unit* eu, BeforeLaunch cb, F fun, Ts&&... xs) {
using trait = typename detail::get_callable_trait<F>::type;
using arg_types = typename trait::arg_types;
using first_arg = typename detail::tl_head<arg_types>::type;
using base_class =
typename std::conditional<
std::is_pointer<first_arg>::value,
typename std::remove_pointer<first_arg>::type,
typename std::conditional<
has_blocking_api_flag(Os),
blocking_actor,
event_based_actor
>::type
>::type;
constexpr bool has_blocking_base =
std::is_base_of<blocking_actor, base_class>::value;
static_assert(has_blocking_base || !has_blocking_api_flag(Os),
"blocking functor-based actors "
"need to be spawned using the blocking_api flag");
static_assert(!has_blocking_base || has_blocking_api_flag(Os),
"non-blocking functor-based actors "
"cannot be spawned using the blocking_api flag");
using impl_class = typename base_class::functor_based;
return spawn_class<impl_class, Os>(eu, cb, fun, std::forward<Ts>(xs)...);
}
/**
* @ingroup ActorCreation
* @{
*/
/**
* Returns a new actor of type `C` using `xs...` as constructor
* arguments. The behavior of `spawn` can be modified by setting `Os`, e.g.,
* to opt-out of the cooperative scheduling.
*/
template <class C, spawn_options Os = no_spawn_options, class... Ts>
actor spawn(Ts&&... xs) {
return spawn_class<C, Os>(nullptr, empty_before_launch_callback{},
std::forward<Ts>(xs)...);
}
/**
* Returns a new functor-based actor. The first argument must be the functor,
* the remainder of `xs...` is used to invoke the functor.
* The behavior of `spawn` can be modified by setting `Os`, e.g.,
* to opt-out of the cooperative scheduling.
*/
template <spawn_options Os = no_spawn_options, class... Ts>
actor spawn(Ts&&... xs) {
static_assert(sizeof...(Ts) > 0, "too few arguments provided");
return spawn_functor<Os>(nullptr, empty_before_launch_callback{},
std::forward<Ts>(xs)...);
}
/**
* Returns a new actor that immediately, i.e., before this function
* returns, joins `grp` of type `C` using `xs` as constructor arguments
*/
template <class C, spawn_options Os = no_spawn_options, class... Ts>
actor spawn_in_group(const group& grp, Ts&&... xs) {
return spawn_class<C, Os>(nullptr, group_subscriber{grp},
std::forward<Ts>(xs)...);
}
/**
* Returns a new actor that immediately, i.e., before this function
* returns, joins `grp`. The first element of `xs` must
* be the functor, the remaining arguments its arguments.
*/
template <spawn_options Os = no_spawn_options, class... Ts>
actor spawn_in_group(const group& grp, Ts&&... xs) {
static_assert(sizeof...(Ts) > 0, "too few arguments provided");
return spawn_functor<Os>(nullptr, group_subscriber{grp},
std::forward<Ts>(xs)...);
}
/**
* Base class for strongly typed actors using a functor-based implementation.
*/
template <class... Sigs>
class functor_based_typed_actor : public typed_event_based_actor<Sigs...> {
public:
/**
* Base class for actors using given interface.
*/
using base = typed_event_based_actor<Sigs...>;
/**
* Pointer to the base class.
*/
using pointer = base*;
/**
* Behavior with proper type information.
*/
using behavior_type = typename base::behavior_type;
/**
* First valid functor signature.
*/
using no_arg_fun = std::function<behavior_type()>;
/**
* Second valid functor signature.
*/
using one_arg_fun1 = std::function<behavior_type(pointer)>;
/**
* Third (and last) valid functor signature.
*/
using one_arg_fun2 = std::function<void(pointer)>;
/**
* Creates a new instance from given functor, binding `xs...`
* to the functor.
*/
template <class F, class... Ts>
functor_based_typed_actor(F fun, Ts&&... xs) {
using trait = typename detail::get_callable_trait<F>::type;
using arg_types = typename trait::arg_types;
using result_type = typename trait::result_type;
constexpr bool returns_behavior =
std::is_same<result_type, behavior_type>::value;
constexpr bool uses_first_arg = std::is_same<
typename detail::tl_head<arg_types>::type, pointer>::value;
std::integral_constant<bool, returns_behavior> token1;
std::integral_constant<bool, uses_first_arg> token2;
set(token1, token2, std::move(fun), std::forward<Ts>(xs)...);
}
protected:
behavior_type make_behavior() override {
return m_fun(this);
}
private:
template <class F>
void set(std::true_type, std::true_type, F&& fun) {
// behavior_type (pointer)
m_fun = std::forward<F>(fun);
}
template <class F>
void set(std::false_type, std::true_type, F fun) {
// void (pointer)
m_fun = [fun](pointer ptr) {
fun(ptr);
return behavior_type{};
};
}
template <class F>
void set(std::true_type, std::false_type, F fun) {
// behavior_type ()
m_fun = [fun](pointer) { return fun(); };
}
// (false_type, false_type) is an invalid functor for typed actors
template <class Token, typename F, typename T0, class... Ts>
void set(Token t1, std::true_type t2, F fun, T0&& arg0, Ts&&... xs) {
set(t1, t2,
std::bind(fun, std::placeholders::_1, std::forward<T0>(arg0),
std::forward<Ts>(xs)...));
}
template <class Token, typename F, typename T0, class... Ts>
void set(Token t1, std::false_type t2, F fun, T0&& arg0, Ts&&... xs) {
set(t1, t2,
std::bind(fun, std::forward<T0>(arg0), std::forward<Ts>(xs)...));
}
// we convert any of the three accepted signatures to this one
one_arg_fun1 m_fun;
};
/**
* Infers the appropriate base class for a functor-based typed actor
* from the result and the first argument of the functor.
*/
template <class Result, class FirstArg>
struct infer_typed_actor_base;
template <class... Sigs, class FirstArg>
struct infer_typed_actor_base<typed_behavior<Sigs...>, FirstArg> {
using type = functor_based_typed_actor<Sigs...>;
};
template <class... Sigs>
struct infer_typed_actor_base<void, typed_event_based_actor<Sigs...>*> {
using type = functor_based_typed_actor<Sigs...>;
};
/**
* Returns a new typed actor of type `C` using `xs...` as
* constructor arguments.
*/
template <class C, spawn_options Os = no_spawn_options, class... Ts>
typename actor_handle_from_signature_list<typename C::signatures>::type
spawn_typed(Ts&&... xs) {
return spawn_class<C, Os>(nullptr, empty_before_launch_callback{},
std::forward<Ts>(xs)...);
}
/**
* Spawns a typed actor from a functor .
*/
template <spawn_options Os, typename BeforeLaunch, typename F, class... Ts>
typename infer_typed_actor_handle<
typename detail::get_callable_trait<F>::result_type,
typename detail::tl_head<
typename detail::get_callable_trait<F>::arg_types
>::type
>::type
spawn_typed_functor(execution_unit* eu, BeforeLaunch bl, F fun, Ts&&... xs) {
using impl =
typename infer_typed_actor_base<
typename detail::get_callable_trait<F>::result_type,
typename detail::tl_head<
typename detail::get_callable_trait<F>::arg_types
>::type
>::type;
return spawn_class<impl, Os>(eu, bl, fun, std::forward<Ts>(xs)...);
}
/**
* Returns a new typed actor from a functor. The first element
* of `xs` must be the functor, the remaining arguments are used to
* invoke the functor. This function delegates its arguments to
* `spawn_typed_functor`.
*/
template <spawn_options Os = no_spawn_options, typename F, class... Ts>
typename infer_typed_actor_handle<
typename detail::get_callable_trait<F>::result_type,
typename detail::tl_head<
typename detail::get_callable_trait<F>::arg_types
>::type
>::type
spawn_typed(F fun, Ts&&... xs) {
return spawn_typed_functor<Os>(nullptr, empty_before_launch_callback{},
std::move(fun), std::forward<Ts>(xs)...);
}
/** @} */
} // namespace caf
#endif // CAF_SPAWN_HPP
|