MVault 1.0.0
Simple c++ database
Loading...
Searching...
No Matches
DataSaver.h
1#pragma once
2
3#include <string>
4#include <iostream>
5#include <typeindex>
6
7#include "Strings/ToString.h"
8#include "Strings/FromString.h"
9
10namespace mvlt
11{
21 {
22 private:
23 // Void pointer to save pointer to any data
24 void* Ptr = nullptr;
25
26 // Pointer to data type saver
27 std::type_index DataType;
28
29 // Pointer to copy function. Required to DataSaver copy
30 void (*CopyFunc)(void*& dst, void* src) = nullptr;
31
32 // Pointer to delete function. Required to delete pointer since it is not possibly to delete void*
33 void (*DeleteFunc)(void*& ptrToDelete) = nullptr;
34
35 // A pointer to a function that will convert the value stored inside to a string
36 std::string (*ToStringFunc)(void* ptrToPrint) = nullptr;
37
38 // Pointer to custom delete function. Required to delete data if it is pointer
39 void (*CustomDeleteFunc)(const void* ptr) = nullptr;
40
41 // Pointer to function to load data from string to data saver
42 bool (*SetDataFromStringFunc)(void* ptrToStoreDataFromString, const std::string& str) = nullptr;
43
44 public:
46 DataSaver() noexcept;
47
53 DataSaver(const DataSaver& other) noexcept;
54
60 DataSaver(DataSaver&& other) noexcept = delete;
61
69 template <class T>
70 explicit DataSaver(const T& data) noexcept : DataType(typeid(void))
71 {
72 SetData(data);
73 }
74
87 template <class T, class F>
88 DataSaver(const T& data, F&& customDeleteFunc) noexcept : DataType(typeid(void))
89 {
90 SetData(data, customDeleteFunc);
91 }
92
99 DataSaver& operator=(const DataSaver& other) noexcept;
100
107 DataSaver& operator=(DataSaver&& other) noexcept = delete;
108
116 template <class T>
117 void SetData(const T& data) noexcept
118 {
119 SetData(data, nullptr);
120 }
121
127 bool SetDataFromString(const std::string& data) noexcept;
128
138 template <class T, class F>
139 void SetData(const T& data, F&& customDeleteFunc) noexcept
140 {
141 // Clear Ptr if it was data before
142 if (Ptr != nullptr)
143 DeleteFunc(Ptr);
144
145 // Create new T type object and save it pointer like void ptr. Data from data will be copying using copy constructor
146 Ptr = static_cast<void*>(new T(data));
147
148 // Save data type to DataType
149 DataType = typeid(data);
150
151 // Set new CopyFunc. It is get to void pointers and convert void pointers to T pointers and copy data.
152 CopyFunc = [](void*& dst, void* src)
153 {
154 // Convert src pointer to T pointer and get data from T pointer.
155 // Use T copy constructor to create T object.
156 // Allocate new memory to T type and convert it to void pointer.
157 dst = static_cast<void*>(new T(*reinterpret_cast<T*>(src)));
158 };
159
160 // Set new DeleteFunc
161 DeleteFunc = [](void*& ptrToDelete)
162 {
163 delete static_cast<T*>(ptrToDelete);
164 };
165
166 // Set new to string function
167 ToStringFunc = [](void* ptrToPrint)
168 {
169 return ToString(*static_cast<T*>(ptrToPrint));
170 };
171
172 // Set custom delete function from dataSaver
173 CustomDeleteFunc = customDeleteFunc;
174
175 // Set new copy from string function
176 SetDataFromStringFunc = [](void* ptrToStoreDataFromString, const std::string& str)
177 {
178 return FromString(str, *static_cast<T*>(ptrToStoreDataFromString));
179 };
180 }
181
192 template <class T>
193 bool GetData(T& data) const noexcept
194 {
195 // Check data type stored in DataSaver
196 if (DataType != typeid(data))
197 {
198 std::cerr << "Wrong type! Was: " + std::string(DataType.name()) + " Requested: " + typeid(data).name() << "\n";
199 return false;
200 }
201
202 // Check that was data inside Ptr
203 if (Ptr == nullptr)
204 return false;
205
206 // Copy data from Ptr to data
207 data = *static_cast<T*>(Ptr);
208 return true;
209 }
210
216 void ResetData() noexcept;
217
223 void Swap(DataSaver& other) noexcept;
224
232 std::string Str() const noexcept;
233
239 std::type_index GetDataType() const noexcept;
240
242 ~DataSaver() noexcept;
243 };
244}
A class for storing any type of data.
Definition DataSaver.h:21
std::type_index GetDataType() const noexcept
A method for getting the type of saved data.
Definition DataSaver.cpp:92
DataSaver() noexcept
Default constructor.
Definition DataSaver.cpp:5
void Swap(DataSaver &other) noexcept
Swap data between 2 DataSavers.
Definition DataSaver.cpp:80
bool GetData(T &data) const noexcept
Template method to get data from DataSaver.
Definition DataSaver.h:193
void ResetData() noexcept
Resets the object to its initial state.
Definition DataSaver.cpp:60
DataSaver & operator=(const DataSaver &other) noexcept
Assignment operator.
Definition DataSaver.cpp:13
bool SetDataFromString(const std::string &data) noexcept
A method for saving data from a string to a DataSaver.
Definition DataSaver.cpp:55
DataSaver & operator=(DataSaver &&other) noexcept=delete
Deleted assignment operator.
DataSaver(DataSaver &&other) noexcept=delete
Deleted move constructor.
void SetData(const T &data, F &&customDeleteFunc) noexcept
Template method to save data and custom delete function inside DataSaver.
Definition DataSaver.h:139
DataSaver(const T &data) noexcept
A template constructor that accepts a variable to store inside DataSaver.
Definition DataSaver.h:70
std::string Str() const noexcept
A method for getting a string that represents data inside a class object.
Definition DataSaver.cpp:87
DataSaver(const T &data, F &&customDeleteFunc) noexcept
A template constructor that accepts a variable and a function to delete a variable.
Definition DataSaver.h:88
void SetData(const T &data) noexcept
Template method to save data inside DataSaver.
Definition DataSaver.h:117
bool FromString(const std::string &stringToCopyDataFrom, T &data) noexcept
A template method for providing an interface converting string to a any type.
Definition FromString.h:31
std::string ToString(const T &data) noexcept
A template method for providing an interface converting any type to a string.
Definition ToString.h:76