MVault 0.0.1
Loading...
Searching...
No Matches
DataContainer.h
1#pragma once
2
3#include <map>
4#include <set>
5#include <unordered_map>
6#include <unordered_set>
7
8#include "DataSaver.h"
9
10namespace mvlt
11{
27 template <class C>
29 {
30 protected:
33
34 public:
36 typedef typename C::iterator iterator;
37
39 typedef typename C::const_iterator const_iterator;
40
42 inline iterator begin() noexcept { return Container.begin(); }
44 inline const_iterator cbegin() const noexcept { return Container.cbegin(); }
45
47 inline iterator end() noexcept { return Container.end(); }
49 inline const_iterator cend() const noexcept { return Container.cend(); }
50
61 template <class T>
62 void AddData(const std::string& key, const T& data) noexcept
63 {
64 Container.emplace(key, DataSaver(data));
65 }
66
79 template <class T, class F>
80 void AddData(const std::string& key, const T& data, F&& deleteFunc) noexcept
81 {
82 Container.emplace(key, DataSaver(data, deleteFunc));
83 }
84
91 void AddDataFromDataSaver(const std::string& key, const DataSaver& dataSaver) noexcept
92 {
93 Container.emplace(key, dataSaver);
94 }
95
105 template <class T>
106 void SetData(const std::string& key, const T& data) noexcept
107 {
108 SetData(key, data, nullptr);
109 }
110
123 template <class T, class F>
124 void SetData(const std::string& key, const T& data, F&& deleteFunc) noexcept
125 {
126 // Find data iterator
127 auto findResIt = Container.find(key);
128
129 // Checking whether there was such a key in the container
130 if (findResIt == Container.end())
131 AddData(key, data, deleteFunc); // Adding data to the container if there was no key
132 else
133 findResIt->second.SetData(data, deleteFunc); // Setting new data for the key if it was already in the container
134 }
135
144 void SetDataFromDataSaver(const std::string& key, const DataSaver& dataSaver) noexcept
145 {
146 // Find data iterator
147 auto findResIt = Container.find(key);
148
149 // Checking whether there was such a key in the container
150 if (findResIt == Container.end())
151 AddDataFromDataSaver(key, dataSaver); // Adding data to the container if there was no key
152 else
153 findResIt->second = dataSaver; // Setting new data for the key if it was already in the container
154 }
155
164 bool SetDataFromString(const std::string& key, const std::string& str) noexcept
165 {
166 // Find data iterator
167 auto findResIt = Container.find(key);
168
169 // Checking whether there was such a key in the container
170 if (findResIt != Container.end())
171 return findResIt->second.SetDataFromString(str);
172
173 return false;
174 }
175
187 template <class T>
188 bool GetData(const std::string& key, T& data) const noexcept
189 {
190 auto findResIt = Container.find(key);
191 if (findResIt == Container.end())
192 return false;
193
194 findResIt->second.GetData(data);
195 return true;
196 }
197
207 bool GetDataSaver(const std::string& key, DataSaver& dataSaver) const noexcept
208 {
209 auto findResIt = Container.find(key);
210 if (findResIt == Container.end())
211 return false;
212
213 dataSaver = findResIt->second;
214 return true;
215 }
216
226 bool GetDataAsString(const std::string& key, std::string& str) const noexcept
227 {
228 auto findResIt = Container.find(key);
229 if (findResIt == Container.end())
230 return false;
231
232 str = findResIt->second.Str();
233 return true;
234 }
235
243 bool IsData(const std::string& key) const noexcept
244 {
245 return Container.find(key) != Container.end();
246 }
247
255 void EraseData(const std::string& key) noexcept
256 {
257 auto findResIt = Container.find(key);
258 if (findResIt != Container.end())
259 {
260 findResIt->second.ResetData();
261 Container.erase(findResIt);
262 }
263 }
264
270 void Clear() noexcept
271 {
272 Container.clear();
273 }
274
280 std::size_t Size() const noexcept
281 {
282 return Container.size();
283 }
284 };
285
287 class DataHashMap : public DataContainer<std::unordered_map<std::string, DataSaver>> {};
288
290 class DataMultiHashMap : public DataContainer<std::unordered_multimap<std::string, DataSaver>>
291 {
292 public:
302 std::pair<DataContainer::const_iterator, DataContainer::const_iterator> GetAllData(const std::string& key) const noexcept
303 {
304 return Container.equal_range(key);
305 }
306 };
307
309 class DataMap : public DataContainer<std::map<std::string, DataSaver>> {};
310
312 class DataMultiMap : public DataContainer<std::multimap<std::string, DataSaver>>
313 {
314 public:
324 std::pair<DataContainer::const_iterator, DataContainer::const_iterator> GetAllData(const std::string& key) const noexcept
325 {
326 return Container.equal_range(key);
327 }
328 };
329}
Template prototype of a container for storing different types of data.
Definition DataContainer.h:29
C Container
Container to store all data inside C container.
Definition DataContainer.h:32
bool GetDataAsString(const std::string &key, std::string &str) const noexcept
Method for getting data converted to string from a container using a key.
Definition DataContainer.h:226
void AddData(const std::string &key, const T &data) noexcept
Template method for adding a new data to the container.
Definition DataContainer.h:62
void EraseData(const std::string &key) noexcept
Function for erasing data from a container.
Definition DataContainer.h:255
void AddData(const std::string &key, const T &data, F &&deleteFunc) noexcept
Template method for adding a new data to the container and a function to delete this data.
Definition DataContainer.h:80
std::size_t Size() const noexcept
Method for getting the container size.
Definition DataContainer.h:280
bool GetDataSaver(const std::string &key, DataSaver &dataSaver) const noexcept
Method for getting dataSaver from a container using a key.
Definition DataContainer.h:207
bool GetData(const std::string &key, T &data) const noexcept
Method for getting data from a container using a key.
Definition DataContainer.h:188
bool IsData(const std::string &key) const noexcept
A method for checking whether data with such a key is in the container.
Definition DataContainer.h:243
C::const_iterator const_iterator
Redefine const_iterator from C container.
Definition DataContainer.h:39
iterator begin() noexcept
Begin provides access to the Data iterator.
Definition DataContainer.h:42
const_iterator cbegin() const noexcept
Cbegin provides access to the Data const_iterator.
Definition DataContainer.h:44
iterator end() noexcept
End provides access to the Data iterator.
Definition DataContainer.h:47
void SetDataFromDataSaver(const std::string &key, const DataSaver &dataSaver) noexcept
Method for changing the value of a data inside a container using a key.
Definition DataContainer.h:144
void AddDataFromDataSaver(const std::string &key, const DataSaver &dataSaver) noexcept
Method for adding a new data to the container.
Definition DataContainer.h:91
C::iterator iterator
Redefine iterator from C container.
Definition DataContainer.h:36
void SetData(const std::string &key, const T &data, F &&deleteFunc) noexcept
Method for changing the value of a data inside a container using a key.
Definition DataContainer.h:124
void Clear() noexcept
Method for clear all data inside container.
Definition DataContainer.h:270
const_iterator cend() const noexcept
Cend provides access to the Data const_iterator.
Definition DataContainer.h:49
void SetData(const std::string &key, const T &data) noexcept
Method for changing the value of a data inside a container using a key.
Definition DataContainer.h:106
bool SetDataFromString(const std::string &key, const std::string &str) noexcept
Method for setting data by key.
Definition DataContainer.h:164
Specialization of class DataContainer, used as a type std::unordered_map<std::string,...
Definition DataContainer.h:287
Specialization of class DataContainer, used as a type std::map<std::string, DataSaver>
Definition DataContainer.h:309
Specialization of class DataContainer, used as a type std::unordered_multimap<std::string,...
Definition DataContainer.h:291
std::pair< DataContainer::const_iterator, DataContainer::const_iterator > GetAllData(const std::string &key) const noexcept
A function for searching for multiple elements with the same keys.
Definition DataContainer.h:302
Specialization of class DataContainer, used as a type std::multimap<std::string, DataSaver>
Definition DataContainer.h:313
std::pair< DataContainer::const_iterator, DataContainer::const_iterator > GetAllData(const std::string &key) const noexcept
A function for searching for multiple elements with the same keys.
Definition DataContainer.h:324
A class for storing any type of data.
Definition DataSaver.h:21