policy.hpp
Go to the documentation of this file.
1 // Copyright 2024 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef BELUGA_POLICIES_POLICY_HPP
16 #define BELUGA_POLICIES_POLICY_HPP
17 
18 #include <type_traits>
19 
20 namespace beluga {
21 
23 template <class PolicyFn>
24 struct policy;
25 
26 namespace detail {
27 
29 
30 struct make_policy_fn {
31  template <class Fn>
32  constexpr policy<Fn> operator()(Fn&& fn) const {
33  return policy<Fn>{std::forward<Fn>(fn)};
34  }
35 };
36 
38 
39 } // namespace detail
40 
42 inline constexpr detail::make_policy_fn make_policy;
43 
45 struct policy_base {
47  template <class Left, class Right>
48  friend constexpr auto operator&&(policy<Left> left, policy<Right> right) {
49  return make_policy([=](const auto&... args) mutable -> bool { return left(args...) && right(args...); });
50  }
51 
53  template <class Left, class Right>
54  friend constexpr auto operator&(policy<Left> left, policy<Right> right) {
55  return make_policy([=](const auto&... args) mutable -> bool {
56  const bool first = left(args...);
57  const bool second = right(args...);
58  return first && second;
59  });
60  }
61 
63  template <class Left, class Right>
64  friend constexpr auto operator||(policy<Left> left, policy<Right> right) {
65  return make_policy([=](const auto&... args) mutable -> bool { return left(args...) || right(args...); });
66  }
67 
69  template <class Left, class Right>
70  friend constexpr auto operator|(policy<Left> left, policy<Right> right) {
71  return make_policy([=](const auto&... args) mutable -> bool {
72  const bool first = left(args...);
73  const bool second = right(args...);
74  return first || second;
75  });
76  }
77 
79  template <class Fn>
80  friend constexpr auto operator!(policy<Fn> fn) {
81  return make_policy([=](const auto&... args) mutable -> bool { return !fn(args...); });
82  }
83 };
84 
86 
101 template <class PolicyFn>
102 struct policy : public policy_base, public PolicyFn {
104  policy() = default;
105 
107  constexpr explicit policy(PolicyFn fn) : PolicyFn(std::move(fn)) {}
108 
109  using PolicyFn::PolicyFn;
110  using PolicyFn::operator=;
111  using PolicyFn::operator();
112 
114 
118  template <class... Args>
119  constexpr auto operator()(Args...) -> //
120  std::enable_if_t< //
121  std::is_invocable_r_v<bool, PolicyFn> && !std::is_invocable_r_v<bool, PolicyFn, Args...>,
122  bool> {
123  return (*this)();
124  }
125 };
126 
128 template <class... Args>
129 using any_policy = policy<std::function<bool(Args...)>>;
130 
131 } // namespace beluga
132 
133 #endif
beluga::policy_base::operator&&
constexpr friend auto operator&&(policy< Left > left, policy< Right > right)
Short-circuited logical AND operation.
Definition: policy.hpp:48
beluga::make_policy
constexpr detail::make_policy_fn make_policy
Make policy function objects.
Definition: policy.hpp:42
beluga::policy_base::operator||
constexpr friend auto operator||(policy< Left > left, policy< Right > right)
Short-circuited logical OR operation.
Definition: policy.hpp:64
beluga::policy_base::operator!
constexpr friend auto operator!(policy< Fn > fn)
Logical NOT operation.
Definition: policy.hpp:80
beluga::policy::policy
constexpr policy(PolicyFn fn)
Conversion constructor.
Definition: policy.hpp:107
beluga::policy::policy
policy()=default
Default constructor.
beluga::policy_base::operator&
constexpr friend auto operator&(policy< Left > left, policy< Right > right)
Non-short-circuited logical AND operation.
Definition: policy.hpp:54
beluga::policy::operator()
constexpr auto operator()(Args...) -> std::enable_if_t< std::is_invocable_r_v< bool, PolicyFn > &&!std::is_invocable_r_v< bool, PolicyFn, Args... >, bool >
Call operator overload.
Definition: policy.hpp:119
beluga::policy
Forward declaration of policy.
Definition: policy.hpp:24
beluga::policy_base::operator|
constexpr friend auto operator|(policy< Left > left, policy< Right > right)
Non-short-circuited logical OR operation.
Definition: policy.hpp:70
std
Definition: circular_array.hpp:529
beluga::policy_base
Implementation detail for a policy base object.
Definition: policy.hpp:45
beluga
The main Beluga namespace.
Definition: 3d_embedding.hpp:21


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53