MVault 0.0.1
Loading...
Searching...
No Matches
DataSaver.h
1#pragma once
2
3#include <string>
4#include <iostream>
5#include <typeindex>
6
7#include "Functions/ToString.h"
8#include "Functions/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& dataSaver) noexcept;
54
62 template<class T>
63 DataSaver(const T& data) noexcept : DataType(typeid(void))
64 {
65 SetData(data);
66 }
67
80 template<class T, class F>
81 DataSaver(const T& data, F&& customDeleteFunc) noexcept : DataType(typeid(void))
82 {
83 SetData(data, customDeleteFunc);
84 }
85
92 DataSaver& operator=(const DataSaver& dataSaver) noexcept;
93
101 template <class T>
102 void SetData(const T& data) noexcept
103 {
104 SetData(data, nullptr);
105 }
106
112 bool SetDataFromString(const std::string& data) noexcept;
113
123 template <class T, class F>
124 void SetData(const T& data, F&& customDeleteFunc) noexcept
125 {
126 // Clear Ptr if it was data before
127 if (Ptr != nullptr)
128 DeleteFunc(Ptr);
129
130 // Create new T type object and save it pointer like void ptr. Data from data will be copying using copy constructor
131 Ptr = static_cast<void*>(new T(data));
132
133 // Save data type to DataType
134 DataType = typeid(data);
135
136 // Set new CopyFunc. It is get to void pointers and convert void pointers to T pointers and copy data.
137 CopyFunc = [](void*& dst, void* src)
138 {
139 // Convert src pointer to T pointer and get data from T pointer.
140 // Use T copy constructor to create T object.
141 // Allocate new memory to T type and convert it to void pointer.
142 dst = static_cast<void*>(new T(*reinterpret_cast<T*>(src)));
143 };
144
145 // Set new DeleteFunc
146 DeleteFunc = [](void*& ptrToDelete)
147 {
148 delete static_cast<T*>(ptrToDelete);
149 };
150
151 // Set new to string function
152 ToStringFunc = [](void* ptrToPrint)
153 {
154 return ToString(*static_cast<T*>(ptrToPrint));
155 };
156
157 // Set custom delete function from dataSaver
158 CustomDeleteFunc = customDeleteFunc;
159
160 // Set new copy from string function
161 SetDataFromStringFunc = [](void* ptrToStoreDataFromString, const std::string& str)
162 {
163 return FromString(str, *static_cast<T*>(ptrToStoreDataFromString));
164 };
165 }
166
177 template <class T>
178 bool GetData(T& data) const noexcept
179 {
180 // Check data type stored in DataSaver
181 if (DataType != typeid(data))
182 {
183 std::cerr << "Wrong type! Was: " + std::string(DataType.name()) + " Requested: " + typeid(data).name() << std::endl;
184 return false;
185 }
186
187 // Check that was data inside Ptr
188 if (Ptr == nullptr)
189 return false;
190
191 // Copy data from Ptr to data
192 data = *static_cast<T*>(Ptr);
193 return true;
194 }
195
201 void ResetData() noexcept;
202
208 void Swap(DataSaver& dataSaver) noexcept;
209
217 std::string Str() const noexcept;
218
224 std::type_index GetDataType() const noexcept;
225
227 ~DataSaver() noexcept;
228 };
229}
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
DataSaver & operator=(const DataSaver &dataSaver) noexcept
Assignment operator.
Definition DataSaver.cpp:13
bool GetData(T &data) const noexcept
Template method to get data from DataSaver.
Definition DataSaver.h:178
void ResetData() noexcept
Resets the object to its initial state.
Definition DataSaver.cpp:60
void Swap(DataSaver &dataSaver) noexcept
Swap data between 2 DataSavers.
Definition DataSaver.cpp:80
bool SetDataFromString(const std::string &data) noexcept
A method for saving data from a string to a DataStorage.
Definition DataSaver.cpp:55
void SetData(const T &data, F &&customDeleteFunc) noexcept
Template method to save data and custom delete function inside DataSaver.
Definition DataSaver.h:124
DataSaver(const T &data) noexcept
A template constructor that accepts a variable to store inside DataSaver.
Definition DataSaver.h:63
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:81
void SetData(const T &data) noexcept
Template method to save data inside DataSaver.
Definition DataSaver.h:102
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:30
std::string ToString(const T &data) noexcept
A template method for providing an interface converting any type to a string.
Definition ToString.h:75