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
Commit
cdeb2e06
authored
Sep 12, 2020
by
Davis King
Browse files
add some docs
parent
12a82f65
Changes
1
Show 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 @@
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
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);
};
}
!*/
...
...
@@ -1808,6 +1848,10 @@ namespace dlib
using dlib::serialize_these; \
try \
{ \
/* Write a version header so that if, at a later time, */
\
/* you realize you need to change the serialization */
\
/* format you can identify which version of the format */
\
/* you are encountering when reading old files. */
\
int version = 1; \
serialize(version, out); \
serialize_these(out, __VA_ARGS__); \
...
...
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