blob: dbfe5584b693a373de4cb1aa301c459dcb9b6711 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
// 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_
|