Maze Builder Docs 8.10.23
Loading...
Searching...
No Matches
create_contract.h
Go to the documentation of this file.
1#ifndef CREATE_CONTRACT_H
2#define CREATE_CONTRACT_H
3
4#include <any>
5#include <optional>
6#include <type_traits>
7#include <utility>
8
11namespace mazes
12{
13 class args;
14 class randomizer;
15
21 {
22 public:
23 virtual ~create_contract() = default;
24
25 [[nodiscard]] virtual std::any create(const args &a, randomizer &rng) noexcept = 0;
26
29 template <typename T>
30 [[nodiscard]] std::optional<T> create_as(const args &a, randomizer &rng) noexcept
31 {
32 static_assert(std::is_same_v<T, std::decay_t<T>>, "T must be a plain value type");
33 static_assert(std::is_copy_constructible_v<T>, "std::any requires copy-constructible results");
34
35 auto result = create(a, rng);
36 if (auto *value = std::any_cast<T>(&result))
37 {
38 return std::move(*value);
39 }
40 return std::nullopt;
41 }
42 };
43}
44
45#endif // CREATE_CONTRACT_H
Command-line argument handler with JSON support.
Definition args.h:19
Contract for the create function in the runtime.
Definition create_contract.h:21
std::optional< T > create_as(const args &a, randomizer &rng) noexcept
Calls create and extracts the result as T.
Definition create_contract.h:30
Provides random-number generating capabilities.
Definition randomizer.h:16