-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_ctx.hpp
52 lines (38 loc) · 1.22 KB
/
encode_ctx.hpp
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
#pragma once
#include <stdint.h>
#include <unordered_set>
#include <vector>
#include <fmt/compile.h>
#include <fmt/core.h>
#include "common.hpp"
#define defaultBufferSize 4096
class EncodeContext {
public:
std::vector<char> buffer;
size_t stack_depth;
std::unordered_set<uintptr_t> seen;
EncodeContext() {
debug_print("new EncodeContext");
buffer = std::vector<char>();
stack_depth = 0;
buffer.reserve(defaultBufferSize);
}
~EncodeContext() {
debug_print("delete context");
seen.clear();
buffer.clear();
}
void reset() {
stack_depth = 0;
buffer.clear();
seen.clear();
}
void write(std::string_view val) { buffer.insert(buffer.end(), val.begin(), val.end()); }
void write(const char *data, Py_ssize_t size) {
buffer.insert(buffer.end(), data, data + size);
}
void writeSize_t(size_t val) { fmt::format_to(std::back_inserter(buffer), "{}", val); }
void writePySize_t(Py_ssize_t val) { fmt::format_to(std::back_inserter(buffer), "{}", val); }
void writeLongLong(int64_t val) { fmt::format_to(std::back_inserter(buffer), "{}", val); }
void writeChar(const char c) { buffer.push_back(c); }
};