Porytiles
Loading...
Searching...
No Matches
operand_bundle.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <any>
5#include <optional>
6#include <ranges>
7#include <typeindex>
8#include <unordered_map>
9#include <vector>
10
13
14namespace porytiles2 {
15
26 public:
27 OperandBundle() = default;
28
29 // -- Range-for support --
30 using iterator = std::unordered_map<std::string, std::any>::iterator;
31 using const_iterator = std::unordered_map<std::string, std::any>::const_iterator;
32 iterator begin() noexcept;
33 iterator end() noexcept;
34 [[nodiscard]] const_iterator begin() const noexcept;
35 [[nodiscard]] const_iterator end() const noexcept;
36 [[nodiscard]] const_iterator cbegin() const noexcept;
37 [[nodiscard]] const_iterator cend() const noexcept;
38 // -- Range-for support --
39
50 [[nodiscard]] std::optional<std::any> get(const std::string &key) const;
51
65 template <typename T>
66 [[nodiscard]] std::optional<T> get_unwrapped(const std::string &key) const
67 {
68 auto value = get(key);
69 if (!value) {
70 return std::nullopt;
71 }
72 try {
73 return std::optional{std::any_cast<T>(value.value())};
74 }
75 catch (const std::bad_any_cast &) {
76 panic("invalid type requested for key: " + key);
77 }
78 }
79
85 [[nodiscard]] std::size_t size() const;
86
97 void put(const std::string &key, const std::any &value);
98
105 [[nodiscard]] bool contains(const std::string &key) const;
106
117 [[nodiscard]] std::optional<std::type_index> type_index_of(const std::string &key) const;
118
130 [[nodiscard]] bool satisfies_declarations(const std::vector<OperandDeclaration> &declarations) const;
131
132 private:
133 std::unordered_map<std::string, std::any> config_;
134};
135
136} // namespace porytiles2
A type-erased container for orchestration operands with runtime type checking.
void put(const std::string &key, const std::any &value)
Stores an operand value with the given key.
std::unordered_map< std::string, std::any >::iterator iterator
const_iterator cbegin() const noexcept
std::unordered_map< std::string, std::any >::const_iterator const_iterator
const_iterator cend() const noexcept
bool contains(const std::string &key) const
Checks if an operand with the given key exists.
std::size_t size() const
Returns the number of operands stored in the bundle.
std::optional< T > get_unwrapped(const std::string &key) const
Retrieves and casts an operand value to the specified type.
iterator end() noexcept
std::optional< std::type_index > type_index_of(const std::string &key) const
Retrieves the runtime type information for an operand.
std::optional< std::any > get(const std::string &key) const
Retrieves an operand value as std::any.
bool satisfies_declarations(const std::vector< OperandDeclaration > &declarations) const
Validates that the bundle satisfies a set of operand declarations.
iterator begin() noexcept
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.hpp:53