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
3ca91aae
Commit
3ca91aae
authored
Jan 24, 2016
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added unserialize.
parent
316099a9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
157 additions
and
0 deletions
+157
-0
vectorstream.h
dlib/vectorstream.h
+1
-0
unserialize.h
dlib/vectorstream/unserialize.h
+98
-0
unserialize_abstract.h
dlib/vectorstream/unserialize_abstract.h
+58
-0
No files found.
dlib/vectorstream.h
View file @
3ca91aae
...
...
@@ -4,6 +4,7 @@
#define DLIB_VECTORSTReAMh_
#include "vectorstream/vectorstream.h"
#include "vectorstream/unserialize.h"
#endif // DLIB_VECTORSTReAMh_
...
...
dlib/vectorstream/unserialize.h
0 → 100644
View file @
3ca91aae
// Copyright (C) 2016 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_uNSERIALIZE_Hh_
#define DLIB_uNSERIALIZE_Hh_
#include "unserialize_abstract.h"
#include "../serialize.h"
#include "../algs.h"
#include "vectorstream.h"
namespace
dlib
{
class
unserialize
:
public
std
::
istream
{
class
mystreambuf
:
public
std
::
streambuf
{
typedef
std
::
vector
<
char
>::
size_type
size_type
;
size_type
read_pos
;
// buffer[read_pos] == next byte to read from buffer
public
:
std
::
vector
<
char
>
buffer
;
std
::
istream
&
str
;
template
<
typename
T
>
mystreambuf
(
const
T
&
item
,
std
::
istream
&
str_
)
:
read_pos
(
0
),
str
(
str_
)
{
// put the item into our buffer.
vectorstream
vstr
(
buffer
);
serialize
(
item
,
vstr
);
}
// ------------------------ INPUT FUNCTIONS ------------------------
int_type
underflow
(
)
{
if
(
read_pos
<
buffer
.
size
())
return
static_cast
<
unsigned
char
>
(
buffer
[
read_pos
]);
else
return
str
.
peek
();
}
int_type
uflow
(
)
{
if
(
read_pos
<
buffer
.
size
())
return
static_cast
<
unsigned
char
>
(
buffer
[
read_pos
++
]);
else
return
str
.
get
();
}
std
::
streamsize
xsgetn
(
char
*
s
,
std
::
streamsize
n
)
{
if
(
read_pos
<
buffer
.
size
())
{
const
size_type
num
=
std
::
min
<
size_type
>
(
n
,
buffer
.
size
()
-
read_pos
);
std
::
memcpy
(
s
,
&
buffer
[
read_pos
],
num
);
read_pos
+=
num
;
return
num
;
}
else
{
return
str
.
rdbuf
()
->
sgetn
(
s
,
n
);
}
return
0
;
}
};
public
:
template
<
typename
T
>
unserialize
(
const
T
&
item
,
std
::
istream
&
str
)
:
std
::
istream
(
&
buf
),
buf
(
item
,
str
)
{}
private
:
mystreambuf
buf
;
};
}
#endif // DLIB_uNSERIALIZE_Hh_
dlib/vectorstream/unserialize_abstract.h
0 → 100644
View file @
3ca91aae
// Copyright (C) 2016 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_uNSERIALIZE_ABSTRACT_Hh_
#ifdef DLIB_uNSERIALIZE_ABSTRACT_Hh_
#include "../serialize.h"
#include <iostream>
namespace
dlib
{
class
unserialize
:
public
std
::
iostream
{
/*!
WHAT THIS OBJECT REPRESENTS
This is a tool that allows you to effectively put an object you just
deserialized from a stream back into the stream. Its use is best
illustrated via an example.
void example(std::istream& in)
{
// Suppose that in contains serialized copies of three "some_type"
// objects. You could read them as follows:
some_type obj1, obj2, obj3;
deserialize(obj1, in); // reads obj1 from stream.
deserialize(obj2, in); // reads obj2 from stream.
unserialize in2(obj2, in); // make the in2 stream that has obj2 at its front.
deserialize(obj2, in2); // reads obj2 from stream again.
deserialize(obj3, in2); // reads obj3 from stream.
}
The reason unserialize is useful is because it allows you to peek at the
next object in a stream and potentially do something different based on
what object is coming next, but still allowing subsequent deserialize()
statements to be undisturbed by the fact that you peeked at the data.
!*/
public
:
template
<
typename
T
>
unserialize
(
const
T
&
item
,
std
::
istream
&
in
);
/*!
requires
- T must be serializable
ensures
- The bytes in this stream begin with a serialized copy of item followed
immediately by the bytes in the given istream.
!*/
};
}
#endif // DLIB_uNSERIALIZE_ABSTRACT_Hh_
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