Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
06bf7e0e
Commit
06bf7e0e
authored
Jul 08, 2014
by
Davis King
Browse files
Options
Browse Files
Download
Plain Diff
merged
parents
0ad899c6
2e3d77f1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
0 deletions
+82
-0
serialize.h
dlib/serialize.h
+29
-0
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