mimic++ v10.0.1
Loading...
Searching...
No Matches
Owner.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_OWNER_HPP
7#define MIMICPP_EXPECTATION_OWNER_HPP
8
9#pragma once
10
11#include "mimic++/Fwd.hpp"
15
16#ifndef MIMICPP_DETAIL_IS_MODULE
17 #include <concepts>
18 #include <utility>
19#endif
20
22{
23 namespace detail
24 {
25 template <typename T>
26 concept finalizable = requires(util::SourceLocation loc) {
27 { std::declval<T&&>().finalize(loc) } -> std::convertible_to<Owner>;
28 };
29 }
30
41 class Owner
42 {
43 public:
47 Owner(Owner const&) = delete;
48
52 Owner& operator=(Owner const&) = delete;
53
58 ~Owner() noexcept(false)
59 {
60 if (m_Registry)
61 {
62 m_Registry->remove(m_Expectation);
63 }
64 }
65
71 [[nodiscard]]
73 : m_Registry{std::move(registry)},
74 m_Expectation{std::move(expectation)}
75 {
76 }
77
84 template <detail::finalizable T>
85 [[nodiscard]]
86 explicit(false) constexpr Owner(T&& object, util::SourceLocation loc = {})
87 : Owner{std::forward<T>(object).finalize(std::move(loc))}
88 {
89 }
90
94 [[nodiscard]]
95 Owner(Owner&&) = default;
96
100 Owner& operator=(Owner&&) = default;
101
106 [[nodiscard]]
107 bool is_satisfied() const
108 {
109 return m_Expectation.is_satisfied();
110 }
111
116 [[nodiscard]]
117 bool is_applicable() const
118 {
119 return m_Expectation.is_applicable();
120 }
121
126 [[nodiscard]]
127 util::SourceLocation const& from() const noexcept
128 {
129 return m_Expectation.from();
130 }
131
136 [[nodiscard]]
137 StringT const& mock_name() const noexcept
138 {
139 return m_Expectation.mock_name();
140 }
141
142 private:
143 Registry::Ptr m_Registry;
144 Expectation m_Expectation;
145 };
146}
147
148#endif
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
The type-erased expectation type with shared ownership.
Definition Expectation.hpp:70
Takes the ownership of an expectation and checks whether it's satisfied during destruction.
Definition Owner.hpp:42
StringT const & mock_name() const noexcept
Queries the stored expectation for the name of the related mock.
Definition Owner.hpp:137
Owner & operator=(Owner &&)=default
Defaulted move-assignment-operator.
util::SourceLocation const & from() const noexcept
Queries the stored expectation for it's stored source-location.
Definition Owner.hpp:127
~Owner() noexcept(false)
Removes the owned expectation from the Registry and checks whether it's satisfied.
Definition Owner.hpp:58
Owner & operator=(Owner const &)=delete
Deleted copy-assignment-operator.
bool is_applicable() const
Queries the stored expectation, whether it's applicable.
Definition Owner.hpp:117
bool is_satisfied() const
Queries the stored expectation, whether it's satisfied.
Definition Owner.hpp:107
Owner(Registry::Ptr registry, Expectation expectation)
Constructor, which generates the type-erase storage.
Definition Owner.hpp:72
Owner(Owner const &)=delete
Deleted copy-constructor.
util::SourceLocation loc
Definition Owner.hpp:86
std::shared_ptr< Registry > Ptr
Definition Registry.hpp:38
A thin wrapper around general source-location info.
Definition SourceLocation.hpp:33
Definition Builder.hpp:33
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:397