.. _program_listing_file_include_beluga_policies_policy.hpp: Program Listing for File policy.hpp =================================== |exhale_lsh| :ref:`Return to documentation for file ` (``include/beluga/policies/policy.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // Copyright 2024 Ekumen, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef BELUGA_POLICIES_POLICY_HPP #define BELUGA_POLICIES_POLICY_HPP #include namespace beluga { template struct policy; namespace detail { struct make_policy_fn { template constexpr policy operator()(Fn&& fn) const { return policy{std::forward(fn)}; } }; } // namespace detail inline constexpr detail::make_policy_fn make_policy; struct policy_base { template friend constexpr auto operator&&(policy left, policy right) { return make_policy([=](const auto&... args) mutable -> bool { return left(args...) && right(args...); }); } template friend constexpr auto operator&(policy left, policy right) { return make_policy([=](const auto&... args) mutable -> bool { const bool first = left(args...); const bool second = right(args...); return first && second; }); } template friend constexpr auto operator||(policy left, policy right) { return make_policy([=](const auto&... args) mutable -> bool { return left(args...) || right(args...); }); } template friend constexpr auto operator|(policy left, policy right) { return make_policy([=](const auto&... args) mutable -> bool { const bool first = left(args...); const bool second = right(args...); return first || second; }); } template friend constexpr auto operator!(policy fn) { return make_policy([=](const auto&... args) mutable -> bool { return !fn(args...); }); } }; template struct policy : public policy_base, public PolicyFn { policy() = default; constexpr explicit policy(PolicyFn fn) : PolicyFn(std::move(fn)) {} using PolicyFn::PolicyFn; using PolicyFn::operator=; using PolicyFn::operator(); template constexpr auto operator()(Args...) -> // std::enable_if_t< // std::is_invocable_r_v && !std::is_invocable_r_v, bool> { return (*this)(); } }; template using any_policy = policy>; } // namespace beluga #endif