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
25ccbc42
Commit
25ccbc42
authored
Nov 05, 2016
by
Davis King
Browse files
Added serialization support for std::array.
parent
cccde632
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
91 additions
and
0 deletions
+91
-0
dlib/serialize.h
dlib/serialize.h
+61
-0
dlib/test/serialize.cpp
dlib/test/serialize.cpp
+30
-0
No files found.
dlib/serialize.h
View file @
25ccbc42
...
...
@@ -62,6 +62,7 @@
- std::string
- std::wstring
- std::vector
- std::array
- std::deque
- std::map
- std::set
...
...
@@ -80,6 +81,7 @@
- std::string
- std::wstring
- std::vector
- std::array
- std::deque
- std::map
- std::set
...
...
@@ -145,6 +147,7 @@
#include <fstream>
#include <string>
#include <vector>
#include <array>
#include <deque>
#include <complex>
#include <map>
...
...
@@ -1363,6 +1366,64 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
template
<
typename
T
,
size_t
N
>
inline
void
serialize
(
const
std
::
array
<
T
,
N
>&
array
,
std
::
ostream
&
out
)
{
typedef
T
c_array_type
[
N
];
serialize
(
*
(
const
c_array_type
*
)
array
.
data
(),
out
);
}
template
<
typename
T
,
size_t
N
>
inline
void
deserialize
(
std
::
array
<
T
,
N
>&
array
,
std
::
istream
&
in
)
{
typedef
T
c_array_type
[
N
];
deserialize
(
*
(
c_array_type
*
)
array
.
data
(),
in
);
}
template
<
typename
T
>
inline
void
serialize
(
const
std
::
array
<
T
,
0
>&
/*array*/
,
std
::
ostream
&
out
)
{
size_t
N
=
0
;
serialize
(
N
,
out
);
}
template
<
typename
T
>
inline
void
deserialize
(
std
::
array
<
T
,
0
>&
/*array*/
,
std
::
istream
&
in
)
{
size_t
N
;
deserialize
(
N
,
in
);
if
(
N
!=
0
)
{
std
::
ostringstream
sout
;
sout
<<
"Expected std::array of size 0 but found a size of "
<<
N
;
throw
serialization_error
(
sout
.
str
());
}
}
// ----------------------------------------------------------------------------------------
template
<
...
...
dlib/test/serialize.cpp
View file @
25ccbc42
...
...
@@ -616,6 +616,35 @@ namespace
DLIB_TEST
(
c
.
size
()
==
0
);
}
void
test_std_array
(
)
{
std
::
array
<
int
,
5
>
a
,
b
;
a
=
{
1
,
2
,
3
,
4
,
5
};
ostringstream
sout
;
dlib
::
serialize
(
a
,
sout
);
istringstream
sin
(
sout
.
str
());
dlib
::
deserialize
(
b
,
sin
);
DLIB_TEST
(
a
.
size
()
==
b
.
size
());
DLIB_TEST
(
a
.
size
()
==
5
);
for
(
unsigned
long
i
=
0
;
i
<
a
.
size
();
++
i
)
{
DLIB_TEST
(
a
[
i
]
==
b
[
i
]);
}
std
::
array
<
int
,
0
>
aa
,
bb
;
sout
.
str
(
""
);
dlib
::
serialize
(
aa
,
sout
);
sin
.
str
(
sout
.
str
());
dlib
::
deserialize
(
bb
,
sin
);
DLIB_TEST
(
bb
.
size
()
==
0
);
}
void
test_vector_bool
(
)
{
...
...
@@ -1020,6 +1049,7 @@ namespace
test_vector_bool
();
test_array2d_and_matrix_serialization
();
test_strings
();
test_std_array
();
}
}
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