mimic++ v10.0.1
Loading...
Searching...
No Matches
GeneralPolicies.hpp
Go to the documentation of this file.
1// Copyright Dominic (DNKpp) Koepke 2024-2026.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// https://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef MIMICPP_EXPECTATION_POLICIES_GENERAL_POLICIES_HPP
7#define MIMICPP_EXPECTATION_POLICIES_GENERAL_POLICIES_HPP
8
9#pragma once
10
11#include "mimic++/Fwd.hpp"
16
17#ifndef MIMICPP_DETAIL_IS_MODULE
18 #include <iterator>
19 #include <optional>
20 #include <utility>
21#endif
22
23namespace mimicpp::detail
24{
25 [[nodiscard]]
26 constexpr bool is_matching(Constness const lhs, Constness const rhs) noexcept
27 {
28 return std::cmp_not_equal(0, util::to_underlying(lhs) & util::to_underlying(rhs));
29 }
30
31 [[nodiscard]]
32 constexpr bool is_matching(ValueCategory const lhs, ValueCategory const rhs) noexcept
33 {
34 return std::cmp_not_equal(0, util::to_underlying(lhs) & util::to_underlying(rhs));
35 }
36}
37
39{
41 {
42 public:
43 template <typename Return, typename... Args>
44 static constexpr void finalize_call(call::Info<Return, Args...> const&) noexcept
45 {
46 }
47 };
48
49 template <ValueCategory expected>
51 {
52 public:
53 [[nodiscard]]
54 static constexpr bool is_satisfied() noexcept
55 {
56 return true;
57 }
58
59 template <typename Return, typename... Args>
60 [[nodiscard]]
61 static MatchResult matches(call::Info<Return, Args...> const& info) noexcept
62 {
63 if (mimicpp::detail::is_matching(info.fromCategory, expected))
64 {
65 return MatchSuccess{.description = describe()};
66 }
67
68 return MatchFailure{.description = describe()};
69 }
70
71 template <typename Return, typename... Args>
72 static constexpr void consume([[maybe_unused]] call::Info<Return, Args...> const& info) noexcept
73 {
74 MIMICPP_ASSERT(mimicpp::detail::is_matching(info.fromCategory, expected), "Call does not match.");
75 }
76
77 private:
78 [[nodiscard]]
79 static auto describe()
80 {
81 if constexpr (ValueCategory::any != expected)
82 {
83 StringStreamT stream{};
84 stream << "expect: from ";
85 mimicpp::print(std::ostreambuf_iterator{stream}, expected);
86 stream << " category overload";
87
88 return std::move(stream).str();
89 }
90 else
91 {
92 return std::nullopt;
93 }
94 }
95 };
96
97 template <Constness constness>
99 {
100 public:
101 [[nodiscard]]
102 static constexpr bool is_satisfied() noexcept
103 {
104 return true;
105 }
106
107 template <typename Return, typename... Args>
108 [[nodiscard]]
110 {
111 if (mimicpp::detail::is_matching(info.fromConstness, constness))
112 {
113 return MatchSuccess{.description = describe()};
114 }
115
116 return MatchFailure{.description = describe()};
117 }
118
119 template <typename Return, typename... Args>
120 static constexpr void consume([[maybe_unused]] call::Info<Return, Args...> const& info) noexcept
121 {
122 MIMICPP_ASSERT(mimicpp::detail::is_matching(info.fromConstness, constness), "Call does not match.");
123 }
124
125 private:
126 [[nodiscard]]
127 static auto describe()
128 {
129 if constexpr (mimicpp::Constness::any != constness)
130 {
131 StringStreamT stream{};
132 stream << "expect: from ";
133 mimicpp::print(std::ostreambuf_iterator{stream}, constness);
134 stream << " qualified overload";
135
136 return std::move(stream).str();
137 }
138 else
139 {
140 return std::nullopt;
141 }
142 }
143 };
144}
145
146#endif
#define MIMICPP_ASSERT(condition, msg)
Definition Config.hpp:54
Definition Call.hpp:27
Definition GeneralPolicies.hpp:51
static constexpr bool is_satisfied() noexcept
Definition GeneralPolicies.hpp:54
static MatchResult matches(call::Info< Return, Args... > const &info) noexcept
Definition GeneralPolicies.hpp:61
static constexpr void consume(call::Info< Return, Args... > const &info) noexcept
Definition GeneralPolicies.hpp:72
Definition GeneralPolicies.hpp:99
static MatchResult matches(call::Info< Return, Args... > const &info) noexcept
Definition GeneralPolicies.hpp:109
static constexpr void consume(call::Info< Return, Args... > const &info) noexcept
Definition GeneralPolicies.hpp:120
static constexpr bool is_satisfied() noexcept
Definition GeneralPolicies.hpp:102
Definition GeneralPolicies.hpp:41
static constexpr void finalize_call(call::Info< Return, Args... > const &) noexcept
Definition GeneralPolicies.hpp:44
constexpr printing::PrintFn print
Functional object, converting the given object to its textual representation.
Definition Print.hpp:183
Definition ArgumentList.hpp:24
std::variant< MatchSuccess, MatchFailure > MatchResult
Definition Common.hpp:41
constexpr std::underlying_type_t< T > to_underlying(T const value) noexcept
Definition C++23Backports.hpp:62
ValueCategory
Definition Fwd.hpp:34
@ any
Definition Fwd.hpp:37
std::basic_ostringstream< CharT, CharTraitsT > StringStreamT
Definition Format.hpp:38
Constness
Definition Fwd.hpp:27
@ any
Definition Fwd.hpp:30
Definition Common.hpp:35
Definition Common.hpp:27