MantisBase v0.4.9
Loading...
Searching...
No Matches
context_store.h
Go to the documentation of this file.
1
9
10#ifndef MANTISAPP_CONTEXTSTORE_H
11#define MANTISAPP_CONTEXTSTORE_H
12
13#include <nlohmann/json.hpp>
14#include "../utils/utils.h"
15#include "types.h"
16
17#ifdef MB_SCRIPTING_ENABLED
18#include <dukglue/dukglue.h>
19#endif
20
21namespace mb
22{
24 using json = nlohmann::json;
25
60 */
62 {
63 std::unordered_map<std::string, std::any> data;
64
65 public:
66 MbContextStore() = default;
70 void dump();
71
77 bool hasKey(const std::string& key) const;
78
86 template <typename T>
87 void set(const std::string& key, T value)
88 {
89 data[key] = std::move(value);
90 }
91
99 template <typename T>
100 std::optional<T*> get(const std::string& key)
101 {
102 const auto it = data.find(key);
103 if (it != data.end()) return std::any_cast<T>(&it->second);
104 return std::nullopt;
105 }
106
115 template <typename T>
116 T& get(const std::string& key, T default_value)
117 {
118 if (const auto it = data.find(key); it == data.end())
119 {
120 data[key] = std::move(default_value);
121 }
122 return std::any_cast<T&>(data.at(key));
123 }
124
125#ifdef MB_SCRIPTING_ENABLED
126 DukValue get_duk(const std::string& key);
127
135 DukValue getOr_duk(const std::string& key, DukValue default_value);
136
143 void set_duk(const std::string& key, const DukValue& value);
144#endif
145 };
146
147} // mb
148
149#endif //MANTISAPP_CONTEXTSTORE_H
void set(const std::string &key, T value)
Store a key-value data in the context.
Definition context_store.h:86
std::optional< T * > get(const std::string &key)
Get context value given the key.
Definition context_store.h:99
bool hasKey(const std::string &key) const
Definition context_store.cpp:52
void dump()
Convenience method for dumping context data for debugging.
Definition context_store.cpp:12
MbContextStore()=default
#define MANTISBASE_API
Definition export.h:31
Definition api_keys.h:35
nlohmann::json json
Shorten JSON namespace.
Definition api_keys.h:36
Core type aliases, handler signatures, and the MbAppMixin DI mixin.
Collection of utility functions that are re-used across different files.