-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyte_buffer.hpp
More file actions
144 lines (116 loc) · 3.76 KB
/
Copy pathbyte_buffer.hpp
File metadata and controls
144 lines (116 loc) · 3.76 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Any copyright is dedicated to the Public Domain.
* https://creativecommons.org/publicdomain/zero/1.0/ */
#pragma once
#include "serv_types.hpp"
#include <cassert>
#include <cstring>
namespace ft
{
namespace serv
{
class byte_buffer
{
public:
typedef dynamic_array<byte_t>::type container_type;
typedef container_type::size_type size_type;
typedef container_type::iterator iterator;
private:
container_type buffer;
size_type position;
public:
inline byte_buffer()
: buffer(),
position()
{
}
inline byte_buffer(size_type length)
: buffer(length),
position()
{
}
inline byte_buffer(const byte_buffer& that)
: buffer(that.buffer),
position(that.position)
{
}
inline byte_buffer& operator=(byte_buffer that)
{
this->buffer.swap(that.buffer);
this->position = that.position;
return *this;
}
inline ~byte_buffer()
{
}
inline byte_t* raw_buffer() throw()
{
return this->buffer.data();
}
inline size_type raw_length() const throw()
{
return this->buffer.size();
}
inline void raw_shrink(size_type length)
{
assert(length <= this->buffer.size());
this->buffer.resize(length);
}
inline const byte_t* get() const throw()
{
return &this->buffer[this->position];
}
template <typename T>
inline T get(const size_type offset = size_type()) const
{
assert(this->size() >= offset + sizeof(T));
// Copy elision
T t;
std::memcpy(&t, this->get() + offset, sizeof(t));
return t;
}
inline size_type size() const throw()
{
assert(this->buffer.size() >= this->position);
return this->buffer.size() - this->position;
}
inline bool empty() const throw()
{
assert(this->buffer.size() >= this->position);
return this->buffer.size() == this->position;
}
inline void put(const void* const data, const size_type size)
{
const byte_t* const array = reinterpret_cast<const byte_t*>(data);
this->buffer.insert(this->buffer.end(), beginof(array), &array[size]);
}
template <typename T>
inline void put(const T& t)
{
byte_t data[sizeof(t)];
std::memcpy(&data, &t, sizeof(t));
this->put(data, sizeof(t));
}
inline void append_from(const byte_buffer& t)
{
this->put(t.get(), t.size());
}
inline void remove(const size_type size) throw()
{
assert(this->size() >= size);
this->position += size;
}
inline void discard() throw()
{
const iterator begin = this->buffer.begin();
const iterator end = begin + this->position;
this->buffer.erase(begin, end);
this->position = size_type();
}
inline void clear() throw()
{
this->buffer.clear();
this->position = size_type();
}
};
}
}