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
eb2806f3
Commit
eb2806f3
authored
May 08, 2014
by
Davis King
Browse files
Added a simplified operator << and >> syntax for serializing to and from files.
parent
754610f2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
3 deletions
+68
-3
dlib/serialize.h
dlib/serialize.h
+68
-3
No files found.
dlib/serialize.h
View file @
eb2806f3
...
...
@@ -4,9 +4,8 @@
#define DLIB_SERIALIZe_
/*!
There are two global functions in the dlib namespace that provide
serialization and deserialization support. Their signatures and specifications
are as follows:
There are two global functions in the dlib namespace that provide serialization and
deserialization support. Their signatures and specifications are as follows:
void serialize (
const serializable_type& item,
...
...
@@ -47,6 +46,16 @@
- any other exception
*!/
For convenience, you can also serialize to a file using this syntax:
serialize("your_file.dat") << some_object << another_object;
That overwrites the contents of your_file.dat with the serialized data from some_object
and another_object. Then to recall the objects from the file you can do:
deserialize("your_file.dat") >> some_object >> another_object;
Finally, you can chain as many objects together using the << and >> operators as you
like.
This file provides serialization support to the following object types:
- The C++ base types (NOT including pointer types)
...
...
@@ -131,6 +140,7 @@
#include <iomanip>
#include <cstddef>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <complex>
...
...
@@ -145,6 +155,7 @@
#include "unicode.h"
#include "byte_orderer.h"
#include "float_details.h"
#include "smart_pointers/shared_ptr.h"
namespace
dlib
{
...
...
@@ -1271,6 +1282,60 @@ namespace dlib
}
// ----------------------------------------------------------------------------------------
class
proxy_serialize
{
public:
explicit
proxy_serialize
(
const
std
::
string
&
filename
)
{
fout
.
reset
(
new
std
::
ofstream
(
filename
.
c_str
()));
if
(
!
(
*
fout
))
throw
serialization_error
(
"Unable to open "
+
filename
+
" for writing."
);
}
template
<
typename
T
>
inline
proxy_serialize
&
operator
<<
(
const
T
&
item
)
{
serialize
(
item
,
*
fout
);
return
*
this
;
}
private:
shared_ptr
<
std
::
ofstream
>
fout
;
};
class
proxy_deserialize
{
public:
explicit
proxy_deserialize
(
const
std
::
string
&
filename
)
{
fin
.
reset
(
new
std
::
ifstream
(
filename
.
c_str
()));
if
(
!
(
*
fin
))
throw
serialization_error
(
"Unable to open "
+
filename
+
" for reading."
);
}
template
<
typename
T
>
inline
proxy_deserialize
&
operator
>>
(
T
&
item
)
{
deserialize
(
item
,
*
fin
);
return
*
this
;
}
private:
shared_ptr
<
std
::
ifstream
>
fin
;
};
inline
proxy_serialize
serialize
(
const
std
::
string
&
filename
)
{
return
proxy_serialize
(
filename
);
}
inline
proxy_deserialize
deserialize
(
const
std
::
string
&
filename
)
{
return
proxy_deserialize
(
filename
);
}
// ----------------------------------------------------------------------------------------
}
// forward declare the MessageLite object so we can reference it below.
...
...
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