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
44916b89
Commit
44916b89
authored
Jul 15, 2012
by
Davis King
Browse files
Added overloads of serialize()/deserialize() so that they can serialize
Google protocol buffer objects.
parent
50ce2abb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
63 additions
and
0 deletions
+63
-0
dlib/serialize.h
dlib/serialize.h
+63
-0
No files found.
dlib/serialize.h
View file @
44916b89
...
@@ -57,6 +57,7 @@
...
@@ -57,6 +57,7 @@
- enumerable<T> where T is a serializable type
- enumerable<T> where T is a serializable type
- map_pair<D,R> where D and R are both serializable types.
- map_pair<D,R> where D and R are both serializable types.
- C style arrays of serializable types
- C style arrays of serializable types
- Google protocol buffer objects.
This file provides deserialization support to the following object types:
This file provides deserialization support to the following object types:
- The C++ base types (NOT including pointer types)
- The C++ base types (NOT including pointer types)
...
@@ -69,6 +70,7 @@
...
@@ -69,6 +70,7 @@
- dlib::uint64
- dlib::uint64
- dlib::int64
- dlib::int64
- C style arrays of serializable types
- C style arrays of serializable types
- Google protocol buffer objects.
Support for deserialization of objects which implement the enumerable or
Support for deserialization of objects which implement the enumerable or
map_pair interfaces is the responsibility of those objects.
map_pair interfaces is the responsibility of those objects.
...
@@ -1039,6 +1041,67 @@ namespace dlib
...
@@ -1039,6 +1041,67 @@ namespace dlib
}
}
}
}
// ----------------------------------------------------------------------------------------
}
// forward declare the Message object so we can reference it below.
namespace
google
{
namespace
protobuf
{
class
Message
;
}
}
namespace
dlib
{
/*!A is_protocol_buffer
This is a template that tells you if a type is a Google protocol buffer object.
!*/
template
<
typename
T
,
typename
U
=
void
>
struct
is_protocol_buffer
{
static
const
bool
value
=
false
;
};
template
<
typename
T
>
struct
is_protocol_buffer
<
T
,
typename
enable_if
<
is_convertible
<
T
*
,
::
google
::
protobuf
::
Message
*>
>::
type
>
{
static
const
bool
value
=
true
;
};
template
<
typename
T
>
typename
enable_if
<
is_protocol_buffer
<
T
>
>::
type
serialize
(
const
T
&
item
,
std
::
ostream
&
out
)
{
// Note that Google protocol buffer messages are not self delimiting (see https://developers.google.com/protocol-buffers/docs/techniques)
// This means they don't record their length or where they end, so we have to package them into a string which
// does know its length before we can use it in normal serialization code.
std
::
string
temp
;
if
(
!
item
.
SerializeToString
(
&
temp
))
{
throw
dlib
::
serialization_error
(
"Error while serializing a Google Protocol Buffer object."
);
}
serialize
(
temp
,
out
);
}
template
<
typename
T
>
typename
enable_if
<
is_protocol_buffer
<
T
>
>::
type
deserialize
(
T
&
item
,
std
::
istream
&
in
)
{
// Note that Google protocol buffer messages are not self delimiting (see https://developers.google.com/protocol-buffers/docs/techniques)
// This means they don't record their length or where they end, so we have to package them into a string which
// does know its length before we can use it in normal serialization code.
std
::
string
temp
;
deserialize
(
temp
,
in
);
if
(
!
item
.
ParseFromString
(
temp
))
{
throw
dlib
::
serialization_error
(
"Error while deserializing a Google Protocol Buffer object."
);
}
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
}
}
...
...
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