7#include "Functions/ToString.h"
8#include "Functions/FromString.h"
27 std::type_index DataType;
30 void (*CopyFunc)(
void*& dst,
void* src) =
nullptr;
33 void (*DeleteFunc)(
void*& ptrToDelete) =
nullptr;
36 std::string (*ToStringFunc)(
void* ptrToPrint) =
nullptr;
39 void (*CustomDeleteFunc)(
const void* ptr) =
nullptr;
42 bool (*SetDataFromStringFunc)(
void* ptrToStoreDataFromString,
const std::string& str) =
nullptr;
63 DataSaver(
const T& data) noexcept : DataType(
typeid(
void))
80 template<
class T,
class F>
81 DataSaver(
const T& data, F&& customDeleteFunc) noexcept : DataType(
typeid(
void))
83 SetData(data, customDeleteFunc);
123 template <
class T,
class F>
124 void SetData(
const T& data, F&& customDeleteFunc)
noexcept
131 Ptr =
static_cast<void*
>(
new T(data));
134 DataType =
typeid(data);
137 CopyFunc = [](
void*& dst,
void* src)
142 dst =
static_cast<void*
>(
new T(*
reinterpret_cast<T*
>(src)));
146 DeleteFunc = [](
void*& ptrToDelete)
148 delete static_cast<T*
>(ptrToDelete);
152 ToStringFunc = [](
void* ptrToPrint)
154 return ToString(*
static_cast<T*
>(ptrToPrint));
158 CustomDeleteFunc = customDeleteFunc;
161 SetDataFromStringFunc = [](
void* ptrToStoreDataFromString,
const std::string& str)
163 return FromString(str, *
static_cast<T*
>(ptrToStoreDataFromString));
181 if (DataType !=
typeid(data))
183 std::cerr <<
"Wrong type! Was: " + std::string(DataType.name()) +
" Requested: " +
typeid(data).name() << std::endl;
192 data = *
static_cast<T*
>(Ptr);
217 std::
string Str() const noexcept;
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