Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
cdeb2e06
"docs/git@developer.sourcefind.cn:OpenDAS/dgl.git" did not exist on "2fa7f71ad59453015f5e9cefb2e80305dbc150fb"
Commit
cdeb2e06
authored
Sep 12, 2020
by
Davis King
Browse files
add some docs
parent
12a82f65
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
73 additions
and
29 deletions
+73
-29
dlib/serialize.h
dlib/serialize.h
+73
-29
No files found.
dlib/serialize.h
View file @
cdeb2e06
...
@@ -136,6 +136,46 @@
...
@@ -136,6 +136,46 @@
then serialize the exponent and mantissa values using dlib's integral serialization
then serialize the exponent and mantissa values using dlib's integral serialization
format. Therefore, the output is first the exponent and then the mantissa. Note that
format. Therefore, the output is first the exponent and then the mantissa. Note that
the mantissa is a signed integer (i.e. there is not a separate sign bit).
the mantissa is a signed integer (i.e. there is not a separate sign bit).
MAKING YOUR OWN CUSTOM OBJECTS SERIALIZABLE
Suppose you create your own type, my_custom_type, and you want it to be serializable. I.e.
you want to be able to use the tools above to save and load it. E.g. maybe you have a
std::vector<my_custom_type> you wish to save.
To make my_custom_type properly serializable all you have to do is define global serialize
and deserialize functions **IN THE SAME NAMESPACE AS MY_CUSTOM_TYPE**. You must define them
in your namespace so that argument dependent lookup will be able to find them. So your code
might look like this:
namespace your_namespace
{
struct my_custom_type
{
int a;
float b;
std::vector<float> c;
};
void serialize (const my_custom_type& item, std::ostream& out);
void deserialize (my_custom_type& item, std::istream& in);
}
That's all you need to do. You may optionally avail yourself of the
DLIB_DEFINE_DEFAULT_SERIALIZATION macro, which generates global friend serialize and
deserialize functions for you. In that case you would do this instead:
namespace your_namespace
{
struct my_custom_type
{
int a;
float b;
std::vector<float> c;
DLIB_DEFINE_DEFAULT_SERIALIZATION(my_custom_type, a, b, c);
};
}
!*/
!*/
...
@@ -1801,37 +1841,41 @@ namespace dlib
...
@@ -1801,37 +1841,41 @@ namespace dlib
deserialize_these
(
in
,
rest
...);
deserialize_these
(
in
,
rest
...);
}
}
#define DLIB_DEFINE_DEFAULT_SERIALIZATION(Type, ...) \
#define DLIB_DEFINE_DEFAULT_SERIALIZATION(Type, ...) \
void serialize_to(std::ostream& out) const \
void serialize_to(std::ostream& out) const \
{ \
{ \
using dlib::serialize; \
using dlib::serialize; \
using dlib::serialize_these; \
using dlib::serialize_these; \
try \
try \
{ \
{ \
int version = 1; \
/* Write a version header so that if, at a later time, */
\
serialize(version, out); \
/* you realize you need to change the serialization */
\
serialize_these(out, __VA_ARGS__); \
/* format you can identify which version of the format */
\
} \
/* you are encountering when reading old files. */
\
catch (dlib::serialization_error& e) \
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); \
throw dlib::serialization_error(e.info + "\n while serializing object of type " #Type); \
} \
}
\
} \
}
\
\
\
void deserialize_from(std::istream& in) \
void deserialize_from(std::istream& in)
\
{ \
{
\
using dlib::deserialize; \
using dlib::deserialize;
\
using dlib::deserialize_these; \
using dlib::deserialize_these;
\
try \
try
\
{ \
{
\
int version = 0; \
int version = 0;
\
deserialize(version, in); \
deserialize(version, in);
\
if (version != 1) \
if (version != 1)
\
throw dlib::serialization_error("Unexpected version found while deserializing " #Type); \
throw dlib::serialization_error("Unexpected version found while deserializing " #Type); \
deserialize_these(in, __VA_ARGS__); \
deserialize_these(in, __VA_ARGS__);
\
} \
}
\
catch (dlib::serialization_error& e) \
catch (dlib::serialization_error& e)
\
{ \
{
\
throw dlib::serialization_error(e.info + "\n while deserializing object of type " #Type); \
throw dlib::serialization_error(e.info + "\n while deserializing object of type " #Type); \
} \
} \
} \
} \
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment