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
06bf7e0e
Commit
06bf7e0e
authored
Jul 08, 2014
by
Davis King
Browse files
merged
parents
0ad899c6
2e3d77f1
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
0 deletions
+82
-0
dlib/serialize.h
dlib/serialize.h
+29
-0
dlib/test/serialize.cpp
dlib/test/serialize.cpp
+53
-0
No files found.
dlib/serialize.h
View file @
06bf7e0e
...
...
@@ -1281,6 +1281,17 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
inline
void
serialize
(
const
char
*
item
,
std
::
ostream
&
out
)
{
// We serialize literal strings the same way we serialize std::string, that is, we
// write the data but no trailing 0 byte.
const
unsigned
long
length
=
strlen
(
item
);
serialize
(
length
,
out
);
out
.
write
(
item
,
length
);
}
// ----------------------------------------------------------------------------------------
class
proxy_serialize
...
...
@@ -1302,6 +1313,24 @@ namespace dlib
return
*
this
;
}
template
<
size_t
length
>
proxy_serialize
&
operator
<<
(
const
char
(
&
item
)[
length
])
{
// We serialize char arrays like we do all other arrays. That is, we write
// every element.
serialize
(
length
,
*
fout
);
fout
->
write
(
item
,
length
);
return
*
this
;
}
proxy_serialize
&
operator
<<
(
const
char
*
item
)
{
// We serialize literal strings the same way we serialize std::string, that is,
// we write the data but no trailing 0 byte.
serialize
(
item
,
*
fout
);
return
*
this
;
}
private:
shared_ptr
<
std
::
ofstream
>
fout
;
};
...
...
dlib/test/serialize.cpp
View file @
06bf7e0e
...
...
@@ -940,6 +940,58 @@ namespace
}
}
// ----------------------------------------------------------------------------------------
void
test_strings
()
{
string
str1
=
"stuff"
;
char
buf
[
6
];
buf
[
0
]
=
0
;
buf
[
1
]
=
1
;
buf
[
2
]
=
2
;
buf
[
3
]
=
0
;
buf
[
4
]
=
3
;
buf
[
5
]
=
3
;
dlib
::
serialize
(
"ser_test_string.dat"
)
<<
str1
<<
buf
<<
"morestuff"
;
string
str2
,
str3
;
char
buf2
[
6
];
memset
(
buf2
,
0
,
sizeof
(
buf2
));
dlib
::
deserialize
(
"ser_test_string.dat"
)
>>
str2
>>
buf2
>>
str3
;
DLIB_TEST
(
str2
==
"stuff"
);
DLIB_TEST
(
str3
==
"morestuff"
);
DLIB_TEST
(
buf2
[
0
]
==
0
);
DLIB_TEST
(
buf2
[
1
]
==
1
);
DLIB_TEST
(
buf2
[
2
]
==
2
);
DLIB_TEST
(
buf2
[
3
]
==
0
);
DLIB_TEST
(
buf2
[
4
]
==
3
);
DLIB_TEST
(
buf2
[
5
]
==
3
);
ofstream
fout
(
"ser_test_string.dat"
,
ios
::
binary
);
dlib
::
serialize
(
str1
,
fout
);
dlib
::
serialize
(
buf
,
fout
);
dlib
::
serialize
(
"morestuff"
,
fout
);
fout
.
close
();
ifstream
fin
(
"ser_test_string.dat"
,
ios
::
binary
);
memset
(
buf2
,
0
,
sizeof
(
buf2
));
str2
.
clear
();
str3
.
clear
();
dlib
::
deserialize
(
str2
,
fin
);
dlib
::
deserialize
(
buf2
,
fin
);
dlib
::
deserialize
(
str3
,
fin
);
DLIB_TEST
(
str2
==
"stuff"
);
DLIB_TEST
(
str3
==
"morestuff"
);
DLIB_TEST
(
buf2
[
0
]
==
0
);
DLIB_TEST
(
buf2
[
1
]
==
1
);
DLIB_TEST
(
buf2
[
2
]
==
2
);
DLIB_TEST
(
buf2
[
3
]
==
0
);
DLIB_TEST
(
buf2
[
4
]
==
3
);
DLIB_TEST
(
buf2
[
5
]
==
3
);
}
// ----------------------------------------------------------------------------------------
class
serialize_tester
:
public
tester
...
...
@@ -966,6 +1018,7 @@ namespace
test_vector
<
int
>
();
test_vector_bool
();
test_array2d_and_matrix_serialization
();
test_strings
();
}
}
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