Macro for generating default serialisation functions (#2177)

* [DLIB] macro for generating default serialisation functions

* [DLIB]  refactoring

* [DLIB]  refactoring
This commit is contained in:
pfeatherstone 2020-09-13 02:18:46 +01:00 committed by GitHub
parent 9d60949a3a
commit 12a82f6542
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 1 deletions

View File

@ -1554,7 +1554,7 @@ namespace dlib
if (!(*fout))
throw serialization_error("Unable to open " + filename + " for writing.");
}
template <typename T>
inline proxy_serialize& operator<<(const T& item)
{
@ -1773,6 +1773,76 @@ namespace dlib
// ----------------------------------------------------------------------------------------
template<typename T>
inline void serialize_these(std::ostream& out, const T& x)
{
using dlib::serialize;
serialize(x, out);
}
template<typename T, typename... Rest>
inline void serialize_these(std::ostream& out, const T& x, const Rest& ... rest)
{
serialize_these(out, x);
serialize_these(out, rest...);
}
template<typename T>
inline void deserialize_these(std::istream& in, T& x)
{
using dlib::deserialize;
deserialize(x, in);
}
template<typename T, typename... Rest>
inline void deserialize_these(std::istream& in, T& x, Rest& ... rest)
{
deserialize_these(in, x);
deserialize_these(in, rest...);
}
#define DLIB_DEFINE_DEFAULT_SERIALIZATION(Type, ...) \
void serialize_to(std::ostream& out) const \
{ \
using dlib::serialize; \
using dlib::serialize_these; \
try \
{ \
int version = 1; \
serialize(version, out); \
serialize_these(out, __VA_ARGS__); \
} \
catch (dlib::serialization_error& e) \
{ \
throw dlib::serialization_error(e.info + "\n while serializing object of type " #Type); \
} \
} \
\
void deserialize_from(std::istream& in) \
{ \
using dlib::deserialize; \
using dlib::deserialize_these; \
try \
{ \
int version = 0; \
deserialize(version, in); \
if (version != 1) \
throw dlib::serialization_error("Unexpected version found while deserializing " #Type); \
deserialize_these(in, __VA_ARGS__); \
} \
catch (dlib::serialization_error& e) \
{ \
throw dlib::serialization_error(e.info + "\n while deserializing object of type " #Type); \
} \
} \
inline friend void serialize(const Type& item, std::ostream& out) \
{ \
item.serialize_to(out); \
} \
inline friend void deserialize(Type& item, std::istream& in) \
{ \
item.deserialize_from(in); \
}
}
#endif // DLIB_SERIALIZe_

View File

@ -400,7 +400,33 @@ namespace
dlib::deserialize(item.b_true,in);
dlib::deserialize(item.b_false,in);
}
struct my_custom_type
{
int a;
float b;
std::vector<float> c;
bool operator==(const my_custom_type& rhs) const
{
return std::tie(a,b,c) == std::tie(rhs.a, rhs.b, rhs.c);
}
DLIB_DEFINE_DEFAULT_SERIALIZATION(my_custom_type, a, b, c);
};
struct my_custom_type_array
{
std::vector<my_custom_type> v;
bool operator==(const my_custom_type_array& rhs) const
{
return v == rhs.v;
}
DLIB_DEFINE_DEFAULT_SERIALIZATION(my_custom_type_array, v);
};
// ----------------------------------------------------------------------------------------
// This function returns the contents of the file 'stuff.bin' but using the old
@ -1027,6 +1053,29 @@ namespace
DLIB_TEST(B == b);
}
}
void test_macros()
{
my_custom_type t1, t2, t3, t4;
t1.a = 1;
t1.b = 2.5;
t1.c.resize(1024);
t2.a = 2;
t2.b = 4.0;
t2.c.resize(10);
my_custom_type_array v1, v2;
v1.v.push_back(t1);
v1.v.push_back(t2);
dlib::serialize("serialization_test_macros.dat") << t1 << t2 << v1;
dlib::deserialize("serialization_test_macros.dat") >> t3 >> t4 >> v2;
DLIB_TEST(t1 == t3);
DLIB_TEST(t2 == t4);
DLIB_TEST(v1 == v2);
}
// ----------------------------------------------------------------------------------------
@ -1056,6 +1105,7 @@ namespace
test_array2d_and_matrix_serialization();
test_strings();
test_std_array();
test_macros();
}
} a;