/usr/include/caf/match_case.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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | /******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| 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_MATCH_CASE_HPP
#define CAF_MATCH_CASE_HPP
#include "caf/none.hpp"
#include "caf/optional.hpp"
#include "caf/skip_message.hpp"
#include "caf/match_case.hpp"
#include "caf/detail/int_list.hpp"
#include "caf/detail/try_match.hpp"
#include "caf/detail/type_list.hpp"
#include "caf/detail/tuple_zip.hpp"
#include "caf/detail/apply_args.hpp"
#include "caf/detail/type_traits.hpp"
#include "caf/detail/pseudo_tuple.hpp"
#include "caf/detail/left_or_right.hpp"
#include "caf/detail/optional_message_visitor.hpp"
namespace caf {
class match_case {
public:
enum result {
fall_through,
no_match,
match
};
match_case(bool has_wildcard, uint32_t token);
match_case(match_case&&) = default;
match_case(const match_case&) = default;
virtual ~match_case();
virtual result invoke(optional<message>&, message&) = 0;
inline uint32_t type_token() const {
return m_token;
}
inline bool has_wildcard() const {
return m_has_wildcard;
}
private:
bool m_has_wildcard;
uint32_t m_token;
};
struct match_case_zipper {
template <class F, typename T>
auto operator()(const F& fun, T& arg) -> decltype(fun(arg)) const {
return fun(arg);
}
// forward everything as reference if no guard/transformation is set
template <class T>
auto operator()(const unit_t&, T& arg) const -> decltype(std::ref(arg)) {
return std::ref(arg);
}
};
template <class T>
T& unopt(T& v) {
return v;
}
template <class T>
T& unopt(optional<T>& v) {
return *v;
}
struct has_none {
inline bool operator()() const {
return false;
}
template <class T, class... Ts>
bool operator()(const T&, const Ts&... xs) const {
return (*this)(xs...);
}
template <class T, class... Ts>
bool operator()(const optional<T>& x, const Ts&... xs) const {
return !x || (*this)(xs...);
}
};
template <bool IsVoid, class F>
class lfinvoker {
public:
lfinvoker(F& fun) : m_fun(fun) {
// nop
}
template <class... Ts>
typename detail::get_callable_trait<F>::result_type operator()(Ts&&... xs) {
return m_fun(unopt(std::forward<Ts>(xs))...);
}
private:
F& m_fun;
};
template <class F>
class lfinvoker<true, F> {
public:
lfinvoker(F& fun) : m_fun(fun) {
// nop
}
template <class... Ts>
unit_t operator()(Ts&&... xs) {
m_fun(unopt(std::forward<Ts>(xs))...);
return unit;
}
private:
F& m_fun;
};
template <class T>
struct projection_result {
using type = typename detail::get_callable_trait<T>::result_type;
};
template <>
struct projection_result<unit_t> {
using type = unit_t;
};
template <class F>
class trivial_match_case : public match_case {
public:
using fun_trait = typename detail::get_callable_trait<F>::type;
using plain_result_type = typename fun_trait::result_type;
using result_type =
typename std::conditional<
std::is_reference<plain_result_type>::value,
plain_result_type,
typename std::remove_const<plain_result_type>::type
>::type;
using arg_types = typename fun_trait::arg_types;
static constexpr bool is_manipulator =
detail::tl_exists<
arg_types,
detail::is_mutable_ref
>::value;
using pattern =
typename detail::tl_map<
arg_types,
std::decay
>::type;
using intermediate_tuple =
typename detail::tl_apply<
pattern,
detail::pseudo_tuple
>::type;
trivial_match_case(F f)
: match_case(false, detail::make_type_token_from_list<pattern>()),
m_fun(std::move(f)) {
// nop
}
match_case::result invoke(optional<message>& res, message& msg) override {
intermediate_tuple it;
detail::meta_elements<pattern> ms;
// check if try_match() reports success
if (!detail::try_match(msg, ms.arr.data(), ms.arr.size(), it.data)) {
return match_case::no_match;
}
// detach msg before invoking m_fun if needed
if (is_manipulator) {
msg.force_detach();
// update pointers in our intermediate tuple
for (size_t i = 0; i < msg.size(); ++i) {
// msg is guaranteed to be detached, hence we don't need to
// check this condition over and over again via mutable_at
it[i] = const_cast<void*>(msg.at(i));
}
}
lfinvoker<std::is_same<result_type, void>::value, F> fun{m_fun};
detail::optional_message_visitor omv;
auto funres = apply_args(fun, detail::get_indices(it), it);
res = omv(funres);
return match_case::match;
}
protected:
F m_fun;
};
template <class F>
class catch_all_match_case : public match_case {
public:
using plain_result_type = typename detail::get_callable_trait<F>::result_type;
using result_type =
typename std::conditional<
std::is_reference<plain_result_type>::value,
plain_result_type,
typename std::remove_const<plain_result_type>::type
>::type;
using arg_types = detail::type_list<>;
catch_all_match_case(F f)
: match_case(true, detail::make_type_token<>()),
m_fun(std::move(f)) {
// nop
}
match_case::result invoke(optional<message>& res, message&) override {
lfinvoker<std::is_same<result_type, void>::value, F> fun{m_fun};
auto fun_res = fun();
detail::optional_message_visitor omv;
res = omv(fun_res);
return match_case::match;
}
protected:
F m_fun;
};
template <class F, class Tuple>
class advanced_match_case : public match_case {
public:
using tuple_type = Tuple;
using base_type = advanced_match_case;
using result_type = typename detail::get_callable_trait<F>::result_type;
advanced_match_case(bool hw, uint32_t tt, F f)
: match_case(hw, tt),
m_fun(std::move(f)) {
// nop
}
virtual bool prepare_invoke(message& msg, tuple_type*) = 0;
// this function could as well be implemented in `match_case_impl`;
// however, dealing with all the template parameters in a debugger
// is just dreadful; this "hack" essentially hides all the ugly
// template boilterplate types when debugging CAF applications
match_case::result invoke(optional<message>& res, message& msg) override {
struct storage {
storage() : valid(false) {
// nop
}
~storage() {
if (valid) {
data.~tuple_type();
}
}
union { tuple_type data; };
bool valid;
};
storage st;
if (prepare_invoke(msg, &st.data)) {
st.valid = true;
lfinvoker<std::is_same<result_type, void>::value, F> fun{m_fun};
detail::optional_message_visitor omv;
auto funres = apply_args(fun, detail::get_indices(st.data), st.data);
res = omv(funres);
return match_case::match;
}
return match_case::no_match;
}
protected:
F m_fun;
};
template <class Pattern>
struct pattern_has_wildcard {
static constexpr bool value =
detail::tl_find<
Pattern,
anything
>::value != -1;
};
template <class Projections>
struct projection_is_trivial {
static constexpr bool value =
detail::tl_count_not<
Projections,
detail::tbind<std::is_same, unit_t>::template type
>::value == 0;
};
/*
* A lifted functor consists of a set of projections, a plain-old
* functor and its signature. Note that the signature of the lifted
* functor might differ from the underlying functor, because
* of the projections.
*/
template <class F, class Pattern, class Projections, class Signature>
class advanced_match_case_impl : public
advanced_match_case<
F,
typename detail::tl_apply<
typename detail::tl_zip_right<
typename detail::tl_map<
Projections,
projection_result
>::type,
typename detail::get_callable_trait<F>::arg_types,
detail::left_or_right,
detail::get_callable_trait<F>::num_args
>::type,
std::tuple
>::type> {
public:
using plain_result_type = typename detail::get_callable_trait<F>::result_type;
using result_type =
typename std::conditional<
std::is_reference<plain_result_type>::value,
plain_result_type,
typename std::remove_const<plain_result_type>::type
>::type;
using pattern = Pattern;
using filtered_pattern =
typename detail::tl_filter_not_type<
Pattern,
anything
>::type;
using intermediate_tuple =
typename detail::tl_apply<
filtered_pattern,
detail::pseudo_tuple
>::type;
static constexpr uint32_t static_type_token =
detail::make_type_token_from_list<pattern>();
// Let F be "R (Ts...)" then match_case<F...> returns optional<R>
// unless R is void in which case bool is returned
using optional_result_type =
typename std::conditional<
std::is_same<result_type, void>::value,
optional<unit_t>,
optional<result_type>
>::type;
using projections_list = Projections;
using projections =
typename detail::tl_apply<
projections_list,
std::tuple
>::type;
/*
* Needed for static type checking when assigning to a typed behavior.
*/
using arg_types = Signature;
using fun_args = typename detail::get_callable_trait<F>::arg_types;
static constexpr size_t num_fun_args = detail::tl_size<fun_args>::value;
static constexpr bool is_manipulator =
detail::tl_exists<
fun_args,
detail::is_mutable_ref
>::value;
using tuple_type =
typename detail::tl_apply<
typename detail::tl_zip_right<
typename detail::tl_map<
projections_list,
projection_result
>::type,
fun_args,
detail::left_or_right,
num_fun_args
>::type,
std::tuple
>::type;
using super = advanced_match_case<F, tuple_type>;
advanced_match_case_impl() = default;
advanced_match_case_impl(const advanced_match_case_impl&) = default;
advanced_match_case_impl& operator=(advanced_match_case_impl&&) = default;
advanced_match_case_impl& operator=(const advanced_match_case_impl&) = default;
advanced_match_case_impl(F f)
: super(pattern_has_wildcard<Pattern>::value, static_type_token,
std::move(f)) {
// nop
}
advanced_match_case_impl(F f, projections ps)
: super(pattern_has_wildcard<Pattern>::value, static_type_token,
std::move(f)),
m_ps(std::move(ps)) {
// nop
}
bool prepare_invoke(message& msg, tuple_type* out) {
// detach msg before invoking m_fun if needed
if (is_manipulator) {
msg.force_detach();
}
intermediate_tuple it;
detail::meta_elements<pattern> ms;
// check if try_match() reports success
if (!detail::try_match(msg, ms.arr.data(), ms.arr.size(), it.data)) {
return false;
}
match_case_zipper zip;
using indices_type = typename detail::il_indices<intermediate_tuple>::type;
//indices_type indices;
typename detail::il_take<indices_type, detail::tl_size<projections>::value - num_fun_args>::type lefts;
typename detail::il_right<indices_type, num_fun_args>::type rights;
has_none hn;
// check if guards of discarded arguments are fulfilled
auto lhs_tup = tuple_zip(zip, lefts, m_ps, it);
if (detail::apply_args(hn, detail::get_indices(lhs_tup), lhs_tup)) {
return false;
}
// zip remaining arguments into output tuple
new (out) tuple_type(tuple_zip(zip, rights, m_ps, it));
//tuple_type rhs_tup = tuple_zip(zip, rights, m_ps, it);
// check if remaining guards are fulfilled
if (detail::apply_args(hn, detail::get_indices(*out), *out)) {
out->~tuple_type();
return false;
}
return true;
}
private:
projections m_ps;
};
struct match_case_info {
bool has_wildcard;
uint32_t type_token;
match_case* ptr;
};
inline bool operator<(const match_case_info& x, const match_case_info& y) {
return x.type_token < y.type_token;
}
template <class F>
typename std::enable_if<
!std::is_base_of<match_case, F>::value,
std::tuple<trivial_match_case<F>>
>::type
to_match_case_tuple(F fun) {
return std::make_tuple(std::move(fun));
}
template <class MatchCase>
typename std::enable_if<
std::is_base_of<match_case, MatchCase>::value,
std::tuple<MatchCase&>
>::type
to_match_case_tuple(MatchCase& x) {
return std::tie(x);
}
template <class... Ts>
std::tuple<Ts...>& to_match_case_tuple(std::tuple<Ts...>& x) {
static_assert(detail::conjunction<
std::is_base_of<
match_case,
Ts
>::value...
>::value,
"to_match_case_tuple received a tuple of non-match_case Ts");
return x;
}
template <class T, class U>
typename std::enable_if<
std::is_base_of<match_case, T>::value || std::is_base_of<match_case, U>::value
>::type
operator,(T, U) {
static_assert(!std::is_same<T, T>::value,
"this syntax is not supported -> you probably did "
"something like 'return (...)' instead of 'return {...}'");
}
} // namespace caf
#endif // CAF_MATCH_CASE_HPP
|