mimic++ v10.0.1
Loading...
Searching...
No Matches
ScopedSequence.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_SCOPED_SEQUENCE_HPP
7#define MIMICPP_SCOPED_SEQUENCE_HPP
8
9#pragma once
10
11#include "mimic++/Fwd.hpp"
12#include "mimic++/Sequence.hpp"
17
18#ifndef MIMICPP_DETAIL_IS_MODULE
19 #include <functional>
20 #include <utility>
21#endif
22
23namespace mimicpp::sequence::detail
24{
25 template <typename Sequence>
26 class ExpectationBuilderFinalizer
27 {
28 public:
29 ExpectationBuilderFinalizer(ExpectationBuilderFinalizer const&) = delete;
30 ExpectationBuilderFinalizer& operator=(ExpectationBuilderFinalizer const&) = delete;
31 ExpectationBuilderFinalizer(ExpectationBuilderFinalizer&&) = delete;
32 ExpectationBuilderFinalizer& operator=(ExpectationBuilderFinalizer&&) = delete;
33
34 ~ExpectationBuilderFinalizer() = default;
35
36 template <typename Signature, auto state, typename... Args>
37 [[nodiscard]]
38 explicit(false) ExpectationBuilderFinalizer(
39 expectation::BasicBuilder<Signature, state, Args...>&& builder,
40 util::SourceLocation loc = {})
41 : m_FinalizeFn{
42 [&builder, loc = std::move(loc)](Sequence& sequence) mutable {
43 return ScopedExpectation{
44 std::move(builder) and expect::in_sequence(sequence),
45 std::move(loc)};
46 }}
47 {
48 }
49
50 [[nodiscard]]
51 ScopedExpectation finalize(Sequence& sequence)
52 {
53 MIMICPP_ASSERT(m_FinalizeFn, "Finalize-Function was already consumed.");
54
55 return std::invoke(
56 std::exchange(m_FinalizeFn, nullptr),
57 sequence);
58 }
59
60 private:
61 std::function<ScopedExpectation(Sequence&)> m_FinalizeFn;
62 };
63}
64
66{
85 template <auto Strategy>
87 : public sequence::detail::BasicSequenceInterface<sequence::Id, Strategy>
88 {
89 public:
93 [[nodiscard]]
95
100
104 ~BasicScopedSequence() noexcept(false) = default;
105
109 [[nodiscard]]
110 explicit BasicScopedSequence([[maybe_unused]] auto&&... canary, util::SourceLocation loc = {})
111 : sequence::detail::BasicSequenceInterface<sequence::Id, Strategy>{std::move(loc)}
112 {
113 static_assert(0u == sizeof...(canary), "ScopedSequence does not accept constructor arguments.");
114 }
115
119 [[nodiscard]]
121
127
137 BasicScopedSequence& operator+=(sequence::detail::ExpectationBuilderFinalizer<BasicScopedSequence>&& builder)
138 {
139 m_Expectations.attach(builder.finalize(*this));
140
141 return *this;
142 }
143
151 [[nodiscard]]
152 std::deque<ScopedExpectation> const& expectations() const noexcept
153 {
154 return m_Expectations.expectations();
155 }
156
157 private:
158 expectation::Collector m_Expectations{};
159 };
160
165 using GreedyScopedSequence = BasicScopedSequence<sequence::detail::GreedyStrategy{}>;
166
171 using LazyScopedSequence = BasicScopedSequence<sequence::detail::LazyStrategy{}>;
172
178}
179
180#endif
#define MIMICPP_ASSERT(condition, msg)
Definition Config.hpp:54
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
A sequence type that verifies its owned expectations during destruction.
Definition ScopedSequence.hpp:88
BasicScopedSequence & operator=(BasicScopedSequence const &)=delete
Deleted copy-assignment operator.
~BasicScopedSequence() noexcept(false)=default
Possibly throwing destructor, checking the owned expectations in order of construction.
BasicScopedSequence(BasicScopedSequence const &)=delete
Deleted copy-constructor.
std::deque< ScopedExpectation > const & expectations() const noexcept
Retrieves the collection of explicitly owned expectations.
Definition ScopedSequence.hpp:152
BasicScopedSequence & operator=(BasicScopedSequence &&)=default
Defaulted move-assignment operator.
BasicScopedSequence(BasicScopedSequence &&)=default
Defaulted move-constructor.
BasicScopedSequence & operator+=(sequence::detail::ExpectationBuilderFinalizer< BasicScopedSequence > &&builder)
Attaches a newly constructed expectation.
Definition ScopedSequence.hpp:137
Collects scoped expectations and verifies them during destruction.
Definition Collector.hpp:32
LazySequence Sequence
The default sequence type (LazySequence).
Definition Sequence.hpp:472
BasicScopedSequence< sequence::detail::LazyStrategy{}> LazyScopedSequence
The scoped-sequence type with lazy strategy.
Definition ScopedSequence.hpp:171
LazyScopedSequence ScopedSequence
The default scoped-sequence type (lazy strategy).
Definition ScopedSequence.hpp:177
BasicScopedSequence< sequence::detail::GreedyStrategy{}> GreedyScopedSequence
The scoped-sequence type with greedy strategy.
Definition ScopedSequence.hpp:165
constexpr auto in_sequence(sequence::detail::BasicSequenceInterface< Id, priorityStrategy > &sequence)
Attaches the expectation onto a sequence.
Definition Sequence.hpp:513
expectation::Owner ScopedExpectation
Takes the ownership of an expectation and checks whether it's satisfied during destruction.
Definition Fwd.hpp:480
constexpr std::optional< LexerTokenClass > expect(TokenStream &stream, LexerTokenClass const &expected)
Definition Parser.hpp:210
Definition Fwd.hpp:451
Definition Call.hpp:24