Branch data Line data Source code
1 : : // Copyright (c) 2026 M. A. Chatterjee
2 : : // SPDX-License-Identifier: BSD-2-Clause
3 : :
4 : : #include "triepack/triepack.hpp"
5 : :
6 : : extern "C" {
7 : : #include "triepack/triepack.h"
8 : : }
9 : :
10 : : namespace triepack
11 : : {
12 : :
13 : : // ---------------------------------------------------------------------------
14 : : // Encoder
15 : : // ---------------------------------------------------------------------------
16 : :
17 : 21 : Encoder::Encoder() : handle_(nullptr)
18 : : {
19 : 21 : tp_encoder_create(&handle_);
20 : 21 : }
21 : :
22 : 22 : Encoder::~Encoder()
23 : : {
24 : 22 : tp_encoder_destroy(&handle_);
25 : 22 : }
26 : :
27 : 1 : Encoder::Encoder(Encoder &&other) noexcept : handle_(nullptr)
28 : : {
29 : 1 : auto *tmp = other.handle_;
30 : 1 : other.handle_ = nullptr;
31 : 1 : handle_ = tmp;
32 : 1 : }
33 : :
34 : 2 : Encoder &Encoder::operator=(Encoder &&other) noexcept
35 : : {
36 [ + + ]: 2 : if (this != &other) {
37 : 1 : tp_encoder_destroy(&handle_);
38 : 1 : auto *tmp = other.handle_;
39 : 1 : other.handle_ = nullptr;
40 : 1 : handle_ = tmp;
41 : : }
42 : 2 : return *this;
43 : : }
44 : :
45 : 31 : void Encoder::insert(const char *key, int32_t value)
46 : : {
47 [ + - ]: 31 : tp_value v = tp_value_int(value);
48 [ + - ]: 31 : tp_encoder_add(handle_, key, &v);
49 : 31 : }
50 : :
51 : 18 : int Encoder::encode(const uint8_t **out_data, size_t *out_size)
52 : : {
53 : : /* tp_encoder_build allocates; caller must cast away const to free */
54 : 18 : uint8_t *buf = nullptr;
55 [ + - ]: 18 : tp_result rc = tp_encoder_build(handle_, &buf, out_size);
56 : 18 : *out_data = buf;
57 : 18 : return rc;
58 : : }
59 : :
60 : 6 : tp_encoder *Encoder::handle() const
61 : : {
62 : 6 : return handle_;
63 : : }
64 : :
65 : : // ---------------------------------------------------------------------------
66 : : // Dict
67 : : // ---------------------------------------------------------------------------
68 : :
69 : 16 : Dict::Dict(const uint8_t *data, size_t size) : handle_(nullptr)
70 : : {
71 : 16 : tp_dict_open(&handle_, data, size);
72 : 16 : }
73 : :
74 : 18 : Dict::~Dict()
75 : : {
76 : 18 : tp_dict_close(&handle_);
77 : 18 : }
78 : :
79 : 2 : Dict::Dict(Dict &&other) noexcept : handle_(nullptr)
80 : : {
81 : 2 : auto *tmp = other.handle_;
82 : 2 : other.handle_ = nullptr;
83 : 2 : handle_ = tmp;
84 : 2 : }
85 : :
86 : 2 : Dict &Dict::operator=(Dict &&other) noexcept
87 : : {
88 [ + + ]: 2 : if (this != &other) {
89 : 1 : tp_dict_close(&handle_);
90 : 1 : auto *tmp = other.handle_;
91 : 1 : other.handle_ = nullptr;
92 : 1 : handle_ = tmp;
93 : : }
94 : 2 : return *this;
95 : : }
96 : :
97 : 18 : bool Dict::lookup(const char *key, int32_t *out_value) const
98 : : {
99 : : tp_value val;
100 [ + - ]: 18 : tp_result rc = tp_dict_lookup(handle_, key, &val);
101 [ + + + + ]: 18 : if (rc == TP_OK && out_value) {
102 : 14 : *out_value = (int32_t)val.data.int_val;
103 : : }
104 : 18 : return rc == TP_OK;
105 : : }
106 : :
107 : 2 : size_t Dict::size() const
108 : : {
109 : 2 : return tp_dict_count(handle_);
110 : : }
111 : :
112 : 15 : tp_dict *Dict::handle() const
113 : : {
114 : 15 : return handle_;
115 : : }
116 : :
117 : : // ---------------------------------------------------------------------------
118 : : // Iterator
119 : : // ---------------------------------------------------------------------------
120 : :
121 : 7 : Iterator::Iterator(const Dict &dict) : handle_(nullptr)
122 : : {
123 : 7 : tp_dict_iterate(dict.handle(), &handle_);
124 : 7 : }
125 : :
126 : 8 : Iterator::~Iterator()
127 : : {
128 : 8 : tp_iter_destroy(&handle_);
129 : 8 : }
130 : :
131 : 1 : Iterator::Iterator(Iterator &&other) noexcept : handle_(nullptr)
132 : : {
133 : 1 : auto *tmp = other.handle_;
134 : 1 : other.handle_ = nullptr;
135 : 1 : handle_ = tmp;
136 : 1 : }
137 : :
138 : 2 : Iterator &Iterator::operator=(Iterator &&other) noexcept
139 : : {
140 [ + + ]: 2 : if (this != &other) {
141 : 1 : tp_iter_destroy(&handle_);
142 : 1 : auto *tmp = other.handle_;
143 : 1 : other.handle_ = nullptr;
144 : 1 : handle_ = tmp;
145 : : }
146 : 2 : return *this;
147 : : }
148 : :
149 : 1 : bool Iterator::next()
150 : : {
151 : 1 : const char *k = nullptr;
152 : 1 : size_t klen = 0;
153 : : tp_value v;
154 [ + - ]: 2 : return tp_iter_next(handle_, &k, &klen, &v) == TP_OK;
155 : : }
156 : :
157 : 1 : const char *Iterator::key() const
158 : : {
159 : : /* TODO: cache key from last next() call */
160 : 1 : return nullptr;
161 : : }
162 : :
163 : 1 : int32_t Iterator::value() const
164 : : {
165 : : /* TODO: cache value from last next() call */
166 : 1 : return 0;
167 : : }
168 : :
169 : 6 : tp_iterator *Iterator::handle() const
170 : : {
171 : 6 : return handle_;
172 : : }
173 : :
174 : : } // namespace triepack
|