C++Spec 1.0.0
BDD testing for C++
All Classes Files Functions Variables Pages Concepts
let.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <functional>
5#include <optional>
6
7namespace CppSpec {
8
16class LetBase {
17 protected:
18 bool delivered{false};
19
20 public:
21 constexpr LetBase() noexcept = default;
22 LetBase(const LetBase& copy) = default;
23 void reset() noexcept { delivered = false; }
24 [[nodiscard]] constexpr bool has_result() const noexcept { return this->delivered; }
25};
26
33template <typename T>
34class Let : public LetBase {
35 using block_t = std::function<T()>;
36 std::optional<T> result;
37
38 block_t body;
39
40 void exec();
41
42 public:
43 explicit Let(block_t body) noexcept : LetBase(), body(body) {}
44
45 T* operator->() {
46 value();
47 return result.operator->();
48 }
49
50 T& operator*() & { return value(); }
51 T& value() &;
52};
53
55template <typename T>
56void Let<T>::exec() {
57 if (!delivered) {
58 result = body();
59 delivered = true;
60 }
61}
62
67template <typename T>
69 exec();
70 return result.value();
71}
72
73} // namespace CppSpec
T & value() &
Get the value contained in the Let.
Definition let.hpp:68