-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwtf_buffer.h
More file actions
47 lines (40 loc) · 1.7 KB
/
Copy pathwtf_buffer.h
File metadata and controls
47 lines (40 loc) · 1.7 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
#ifndef WTF_BUFFER_H
#define WTF_BUFFER_H
#include <variant>
#include <vector>
#include "fu2/function2.hpp"
/// Lazy buffer (my bad :)
/// Proxy class over own internal vector (owning) or external vector/raw buffer (non-owning).
class wtf_buffer
{
public:
explicit wtf_buffer(size_t size = 1024 * 1024);
/// construct over existing vector, non-owning mode
wtf_buffer(std::vector<char> &buf, size_t offset = 0);
/// construct over existing vector, take ownership
wtf_buffer(std::vector<char> &&buf, size_t offset = 0);
/// Non-owning wrap around any external buffer with optional resize functionality.
/// You may capture any container within realloc functinal object to own the container.
wtf_buffer(char *data, size_t length, fu2::unique_function<char*(size_t)> realloc = {});
size_t capacity() const noexcept;
size_t size() const noexcept;
int64_t available() const noexcept;
char* data() noexcept;
const char* data() const noexcept;
void reserve(size_t size);
void resize(size_t size);
void clear() noexcept;
//void swap(wtf_buffer &other) noexcept;
wtf_buffer(wtf_buffer &&) = default;
wtf_buffer& operator= (wtf_buffer &&) = default;
wtf_buffer(const wtf_buffer &) = delete;
wtf_buffer& operator= (const wtf_buffer &) = delete;
char *end; // let msgpuck to write directly
private:
// to avoid templating to preserve old code compatibility and stay simple
// due to single type (two random buffers have the same type => simple move, swap, etc)
std::variant<fu2::unique_function<char*(size_t)>, std::vector<char>*, std::vector<char>> target;
char *head = nullptr;
char *end_of_storage = nullptr;
};
#endif // WTF_BUFFER_H