tetengo 1.8.2
A multipurpose library set
Loading...
Searching...
No Matches
storage.hpp
Go to the documentation of this file.
1
7#if !defined(TETENGO_PROPERTY_STORAGE_HPP)
8#define TETENGO_PROPERTY_STORAGE_HPP
9
10#include <cstdint>
11#include <filesystem>
12#include <memory>
13#include <optional>
14#include <string>
15#include <unordered_map>
16#include <variant>
17
18#include <boost/core/noncopyable.hpp>
19
20
21namespace tetengo::property
22{
26 class storage : private boost::noncopyable
27 {
28 public:
29 // types
30
32 using value_map_type = std::unordered_map<std::string, std::variant<bool, std::uint32_t, std::string>>;
33
34
35 // constructors and destructor
36
40 virtual ~storage();
41
42
43 // functions
44
52 [[nodiscard]] std::optional<bool> get_bool(const std::filesystem::path& key) const;
53
60 void set_bool(const std::filesystem::path& key, bool value);
61
69 [[nodiscard]] std::optional<std::uint32_t> get_uint32(const std::filesystem::path& key) const;
70
77 void set_uint32(const std::filesystem::path& key, std::uint32_t value);
78
86 [[nodiscard]] std::optional<std::string> get_string(const std::filesystem::path& key) const;
87
94 void set_string(const std::filesystem::path& key, const std::string& value);
95
99 void save() const;
100
101
102 protected:
103 // constructors
104
111
112
113 // functions
114
120 [[nodiscard]] const value_map_type& value_map() const;
121
122
123 private:
124 // types
125
126 class impl;
127
128
129 // variables
130
131 const std::unique_ptr<impl> m_p_impl;
132
133
134 // virtual functions
135
136 virtual std::optional<bool> get_bool_impl(const std::filesystem::path& key) const;
137
138 virtual void set_bool_impl(const std::filesystem::path& key, bool value);
139
140 virtual std::optional<std::uint32_t> get_uint32_impl(const std::filesystem::path& key) const;
141
142 virtual void set_uint32_impl(const std::filesystem::path& key, std::uint32_t value);
143
144 virtual std::optional<std::string> get_string_impl(const std::filesystem::path& key) const;
145
146 virtual void set_string_impl(const std::filesystem::path& key, const std::string& value);
147
148 virtual void save_impl() const = 0;
149 };
150
151
155 class storage_loader : private boost::noncopyable
156 {
157 public:
158 // constructors and destructor
159
164
165
166 // functions
167
175 [[nodiscard]] std::unique_ptr<storage> load(const std::filesystem::path& path) const;
176
177
178 private:
179 // virtual functions
180
181 virtual std::unique_ptr<storage> load_impl(const std::filesystem::path& path) const = 0;
182 };
183
184
185}
186
187
188#endif
A storage loader.
Definition storage.hpp:156
std::unique_ptr< storage > load(const std::filesystem::path &path) const
Loads a storage.
virtual ~storage_loader()
Destroys the storage loader.
A storage.
Definition storage.hpp:27
void set_uint32(const std::filesystem::path &key, std::uint32_t value)
Sets a value in an unsigned 32-bit integer.
const value_map_type & value_map() const
Returns the value map.
std::unordered_map< std::string, std::variant< bool, std::uint32_t, std::string > > value_map_type
The value map type.
Definition storage.hpp:32
std::optional< std::string > get_string(const std::filesystem::path &key) const
Returns the value in a string.
std::optional< std::uint32_t > get_uint32(const std::filesystem::path &key) const
Returns the value in an unsigned 32-bit integer.
virtual ~storage()
Destroys the storage.
void set_bool(const std::filesystem::path &key, bool value)
Sets a value in a boolean.
std::optional< bool > get_bool(const std::filesystem::path &key) const
Returns the value in a boolean.
void save() const
Saves the values.
storage(value_map_type value_map)
Creates a storage.
void set_string(const std::filesystem::path &key, const std::string &value)
Sets a value in a string.
A property set library.