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
721d55a6
Commit
721d55a6
authored
Jan 16, 2013
by
Davis King
Browse files
Added serialization support for std::vector<bool>.
parent
b88c47d9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
0 deletions
+64
-0
dlib/serialize.h
dlib/serialize.h
+37
-0
dlib/test/serialize.cpp
dlib/test/serialize.cpp
+27
-0
No files found.
dlib/serialize.h
View file @
721d55a6
...
...
@@ -806,6 +806,43 @@ namespace dlib
{
throw
serialization_error
(
e
.
info
+
"
\n
while deserializing object of type std::set"
);
}
}
// ----------------------------------------------------------------------------------------
template
<
typename
alloc
>
void
serialize
(
const
std
::
vector
<
bool
,
alloc
>&
item
,
std
::
ostream
&
out
)
{
std
::
vector
<
unsigned
char
>
temp
(
item
.
size
());
for
(
unsigned
long
i
=
0
;
i
<
item
.
size
();
++
i
)
{
if
(
item
[
i
])
temp
[
i
]
=
'1'
;
else
temp
[
i
]
=
'0'
;
}
serialize
(
temp
,
out
);
}
template
<
typename
alloc
>
void
deserialize
(
std
::
vector
<
bool
,
alloc
>&
item
,
std
::
istream
&
in
)
{
std
::
vector
<
unsigned
char
>
temp
;
deserialize
(
temp
,
in
);
item
.
resize
(
temp
.
size
());
for
(
unsigned
long
i
=
0
;
i
<
temp
.
size
();
++
i
)
{
if
(
temp
[
i
]
==
'1'
)
item
[
i
]
=
true
;
else
item
[
i
]
=
false
;
}
}
// ----------------------------------------------------------------------------------------
template
<
typename
T
,
typename
alloc
>
...
...
dlib/test/serialize.cpp
View file @
721d55a6
...
...
@@ -530,6 +530,32 @@ namespace
}
}
void
test_vector_bool
(
)
{
std
::
vector
<
bool
>
a
,
b
;
a
.
push_back
(
true
);
a
.
push_back
(
true
);
a
.
push_back
(
false
);
a
.
push_back
(
true
);
a
.
push_back
(
false
);
a
.
push_back
(
true
);
ostringstream
sout
;
dlib
::
serialize
(
a
,
sout
);
istringstream
sin
(
sout
.
str
());
dlib
::
deserialize
(
b
,
sin
);
DLIB_TEST
(
a
.
size
()
==
b
.
size
());
DLIB_TEST
(
a
.
size
()
==
6
);
for
(
unsigned
long
i
=
0
;
i
<
a
.
size
();
++
i
)
{
DLIB_TEST
(
a
[
i
]
==
b
[
i
]);
}
}
class
serialize_tester
:
public
tester
...
...
@@ -554,6 +580,7 @@ namespace
test_vector
<
char
>
();
test_vector
<
unsigned
char
>
();
test_vector
<
int
>
();
test_vector_bool
();
}
}
a
;
...
...
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