Porytiles
Loading...
Searching...
No Matches
any_map.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <any>
4#include <optional>
5#include <typeindex>
6#include <unordered_map>
7
9
10namespace porytiles {
11
12class AnyMap {
13 public:
14 AnyMap() = default;
15
16 // -- Range-for support --
17 using iterator = std::unordered_map<std::string, std::any>::iterator;
18 using const_iterator = std::unordered_map<std::string, std::any>::const_iterator;
19
20 iterator begin() noexcept {
21 return config_.begin();
22 }
23 iterator end() noexcept {
24 return config_.end();
25 }
26 [[nodiscard]] const_iterator begin() const noexcept {
27 return config_.begin();
28 }
29 [[nodiscard]] const_iterator end() const noexcept {
30 return config_.end();
31 }
32 [[nodiscard]] const_iterator cbegin() const noexcept {
33 return config_.cbegin();
34 }
35 [[nodiscard]] const_iterator cend() const noexcept {
36 return config_.cend();
37 }
38
39 template <typename T>
40 [[nodiscard]] std::optional<T> Try(const std::string &key) const {
41 if (!Contains(key)) {
42 return std::nullopt;
43 }
44 try {
45 return std::optional{std::any_cast<T>(config_.at(key))};
46 } catch (const std::bad_any_cast &) {
47 return std::nullopt;
48 }
49 }
50
51 template <typename T>
52 [[nodiscard]] std::optional<T> Get(const std::string &key) const {
53 if (!Contains(key)) {
54 Panic("Key not found: " + key);
55 }
56 try {
57 return std::optional{std::any_cast<T>(config_.at(key))};
58 } catch (const std::bad_any_cast &) {
59 Panic("Invalid type requested for key: " + key);
60 }
61 }
62
63 [[nodiscard]] std::optional<std::any> TryAny(const std::string &key) const {
64 if (!Contains(key)) {
65 return std::nullopt;
66 }
67 return std::optional{config_.at(key)};
68 }
69
70 [[nodiscard]] std::any GetAny(const std::string &key) const {
71 if (!Contains(key)) {
72 Panic("Key not found: " + key);
73 }
74 return config_.at(key);
75 }
76
77 void Put(const std::string &key, const std::any &value) {
78 config_.insert_or_assign(key, value);
79 }
80
81 [[nodiscard]] bool Contains(const std::string &key) const {
82 return config_.contains(key);
83 }
84
85 [[nodiscard]] std::optional<std::type_index> GetType(const std::string &key) const {
86 if (!Contains(key)) {
87 return std::nullopt;
88 }
89 return config_.at(key).type();
90 }
91
92 private:
93 std::unordered_map<std::string, std::any> config_;
94};
95
96} // namespace porytiles
std::optional< T > Get(const std::string &key) const
Definition any_map.hpp:52
std::optional< std::type_index > GetType(const std::string &key) const
Definition any_map.hpp:85
const_iterator end() const noexcept
Definition any_map.hpp:29
std::unordered_map< std::string, std::any >::const_iterator const_iterator
Definition any_map.hpp:18
const_iterator begin() const noexcept
Definition any_map.hpp:26
std::unordered_map< std::string, std::any >::iterator iterator
Definition any_map.hpp:17
void Put(const std::string &key, const std::any &value)
Definition any_map.hpp:77
bool Contains(const std::string &key) const
Definition any_map.hpp:81
iterator begin() noexcept
Definition any_map.hpp:20
iterator end() noexcept
Definition any_map.hpp:23
std::optional< std::any > TryAny(const std::string &key) const
Definition any_map.hpp:63
std::optional< T > Try(const std::string &key) const
Definition any_map.hpp:40
std::any GetAny(const std::string &key) const
Definition any_map.hpp:70
const_iterator cbegin() const noexcept
Definition any_map.hpp:32
const_iterator cend() const noexcept
Definition any_map.hpp:35
void Panic(const StringViewSourceLoc &s) noexcept
Definition panic.hpp:31