-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtyped_buffer.h
70 lines (54 loc) · 1.33 KB
/
typed_buffer.h
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
#pragma once
#include <memory>
#include <algorithm>
template<class T>
class typed_buffer_ptr {
std::unique_ptr<char[]> buffer_;
size_t size_;
public:
typed_buffer_ptr() {
}
explicit typed_buffer_ptr(size_t size) {
reset(size);
}
void reset(size_t size) {
buffer_.reset(new char[size]);
memset(buffer_.get(), 0, size);
size_ = size;
}
void resize(size_t size) {
std::unique_ptr<char[]> tmp(new char[size]);
memcpy(tmp.get(), buffer_.get(), min(size, size_));
buffer_ = std::move(tmp);
}
operator T*() {
return reinterpret_cast<T*>(buffer_.get());
}
operator const T*() const {
return cget();
}
T* operator->() const {
return reinterpret_cast<T*>(buffer_.get());
}
const T* cget() const {
return interpret_cast<const T*>(buffer_.get());
}
typed_buffer_ptr(const typed_buffer_ptr<T>& other) = delete;
typed_buffer_ptr& typed_buffer_ptr::operator=(const typed_buffer_ptr<T>& other) = delete;
typed_buffer_ptr(typed_buffer_ptr<T>&& other) {
buffer_ = std::move(other.buffer_);
size_ = other.size_;
other.size_ = 0;
}
typed_buffer_ptr& operator=(typed_buffer_ptr<T>&& other) {
if (this != &other)
{
buffer_ = std::move(other.buffer_);
size_ = other.size_;
other.size_ = 0;
}
}
size_t size() const {
return size_;
}
};