1 /*
2 Copyright (c) DataStax, Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 #ifndef DATASTAX_INTERNAL_ENCODING_HPP
18 #define DATASTAX_INTERNAL_ENCODING_HPP
19
20 #include "buffer.hpp"
21 #include "types.hpp"
22
23 namespace datastax { namespace internal { namespace core {
24
encode_with_length(CassNull)25 inline Buffer encode_with_length(CassNull) {
26 Buffer buf(sizeof(int32_t));
27 buf.encode_int32(0, -1); // [bytes] "null"
28 return buf;
29 }
30
encode_with_length(CassUnset)31 inline Buffer encode_with_length(CassUnset) {
32 Buffer buf(sizeof(int32_t));
33 buf.encode_int32(0, -2); // [bytes] "unset"
34 return buf;
35 }
36
encode_with_length(cass_int8_t value)37 inline Buffer encode_with_length(cass_int8_t value) {
38 Buffer buf(sizeof(int32_t) + sizeof(int8_t));
39 size_t pos = buf.encode_int32(0, sizeof(int8_t));
40 buf.encode_int8(pos, value);
41 return buf;
42 }
43
encode_with_length(cass_int16_t value)44 inline Buffer encode_with_length(cass_int16_t value) {
45 Buffer buf(sizeof(int32_t) + sizeof(int16_t));
46 size_t pos = buf.encode_int32(0, sizeof(int16_t));
47 buf.encode_int16(pos, value);
48 return buf;
49 }
50
encode_with_length(cass_int32_t value)51 inline Buffer encode_with_length(cass_int32_t value) {
52 Buffer buf(sizeof(int32_t) + sizeof(int32_t));
53 size_t pos = buf.encode_int32(0, sizeof(int32_t));
54 buf.encode_int32(pos, value);
55 return buf;
56 }
57
encode_with_length(cass_uint32_t value)58 inline Buffer encode_with_length(cass_uint32_t value) {
59 Buffer buf(sizeof(int32_t) + sizeof(uint32_t));
60 size_t pos = buf.encode_int32(0, sizeof(uint32_t));
61 buf.encode_uint32(pos, value);
62 return buf;
63 }
64
encode_with_length(cass_int64_t value)65 inline Buffer encode_with_length(cass_int64_t value) {
66 Buffer buf(sizeof(int32_t) + sizeof(int64_t));
67 size_t pos = buf.encode_int32(0, sizeof(int64_t));
68 buf.encode_int64(pos, value);
69 return buf;
70 }
71
encode_with_length(cass_float_t value)72 inline Buffer encode_with_length(cass_float_t value) {
73 Buffer buf(sizeof(int32_t) + sizeof(float));
74 size_t pos = buf.encode_int32(0, sizeof(float));
75 buf.encode_float(pos, value);
76 return buf;
77 }
78
encode_with_length(cass_double_t value)79 inline Buffer encode_with_length(cass_double_t value) {
80 Buffer buf(sizeof(int32_t) + sizeof(double));
81 size_t pos = buf.encode_int32(0, sizeof(double));
82 buf.encode_double(pos, value);
83 return buf;
84 }
85
encode_with_length(cass_bool_t value)86 inline Buffer encode_with_length(cass_bool_t value) {
87 Buffer buf(sizeof(int32_t) + 1);
88 size_t pos = buf.encode_int32(0, 1);
89 buf.encode_byte(pos, static_cast<int8_t>(value));
90 return buf;
91 }
92
encode_with_length(CassString value)93 inline Buffer encode_with_length(CassString value) {
94 Buffer buf(sizeof(int32_t) + value.length);
95 size_t pos = buf.encode_int32(0, value.length);
96 buf.copy(pos, value.data, value.length);
97 return buf;
98 }
99
encode_with_length(CassBytes value)100 inline Buffer encode_with_length(CassBytes value) {
101 Buffer buf(sizeof(int32_t) + value.size);
102 size_t pos = buf.encode_int32(0, value.size);
103 buf.copy(pos, reinterpret_cast<const char*>(value.data), value.size);
104 return buf;
105 }
106
encode_with_length(CassCustom value)107 inline Buffer encode_with_length(CassCustom value) {
108 Buffer buf(sizeof(int32_t) + value.size);
109 size_t pos = buf.encode_int32(0, value.size);
110 buf.copy(pos, reinterpret_cast<const char*>(value.data), value.size);
111 return buf;
112 }
113
encode_with_length(CassUuid value)114 inline Buffer encode_with_length(CassUuid value) {
115 Buffer buf(sizeof(int32_t) + sizeof(CassUuid));
116 size_t pos = buf.encode_int32(0, sizeof(CassUuid));
117 buf.encode_uuid(pos, value);
118 return buf;
119 }
120
encode_with_length(CassInet value)121 inline Buffer encode_with_length(CassInet value) {
122 Buffer buf(sizeof(int32_t) + value.address_length);
123 size_t pos = buf.encode_int32(0, value.address_length);
124 buf.copy(pos, value.address, value.address_length);
125 return buf;
126 }
127
encode_with_length(CassDecimal value)128 inline Buffer encode_with_length(CassDecimal value) {
129 Buffer buf(sizeof(int32_t) + sizeof(int32_t) + value.varint_size);
130 size_t pos = buf.encode_int32(0, sizeof(int32_t) + value.varint_size);
131 pos = buf.encode_int32(pos, value.scale);
132 buf.copy(pos, value.varint, value.varint_size);
133 return buf;
134 }
135
encode(cass_int8_t value)136 inline Buffer encode(cass_int8_t value) {
137 Buffer buf(sizeof(cass_int8_t));
138 buf.encode_int8(0, value);
139 return buf;
140 }
141
encode(cass_int16_t value)142 inline Buffer encode(cass_int16_t value) {
143 Buffer buf(sizeof(cass_int16_t));
144 buf.encode_int16(0, value);
145 return buf;
146 }
147
encode(cass_int32_t value)148 inline Buffer encode(cass_int32_t value) {
149 Buffer buf(sizeof(int32_t));
150 buf.encode_int32(0, value);
151 return buf;
152 }
153
encode(cass_uint32_t value)154 inline Buffer encode(cass_uint32_t value) {
155 Buffer buf(sizeof(uint32_t));
156 buf.encode_uint32(0, value);
157 return buf;
158 }
159
encode(cass_int64_t value)160 inline Buffer encode(cass_int64_t value) {
161 Buffer buf(sizeof(int64_t));
162 buf.encode_int64(0, value);
163 return buf;
164 }
165
encode(cass_float_t value)166 inline Buffer encode(cass_float_t value) {
167 Buffer buf(sizeof(float));
168 buf.encode_float(0, value);
169 return buf;
170 }
171
encode(cass_double_t value)172 inline Buffer encode(cass_double_t value) {
173 Buffer buf(sizeof(double));
174 buf.encode_double(0, value);
175 return buf;
176 }
177
encode(cass_bool_t value)178 inline Buffer encode(cass_bool_t value) {
179 Buffer buf(1);
180 buf.encode_byte(0, static_cast<int8_t>(value));
181 return buf;
182 }
183
encode(CassString value)184 inline Buffer encode(CassString value) {
185 Buffer buf(value.length);
186 buf.copy(0, value.data, value.length);
187 return buf;
188 }
189
encode(CassBytes value)190 inline Buffer encode(CassBytes value) {
191 Buffer buf(value.size);
192 buf.copy(0, reinterpret_cast<const char*>(value.data), value.size);
193 return buf;
194 }
195
encode(CassCustom value)196 inline Buffer encode(CassCustom value) {
197 Buffer buf(value.size);
198 buf.copy(0, reinterpret_cast<const char*>(value.data), value.size);
199 return buf;
200 }
201
encode(CassUuid value)202 inline Buffer encode(CassUuid value) {
203 Buffer buf(sizeof(CassUuid));
204 buf.encode_uuid(0, value);
205 return buf;
206 }
207
encode(CassInet value)208 inline Buffer encode(CassInet value) {
209 Buffer buf(value.address_length);
210 buf.copy(0, value.address, value.address_length);
211 return buf;
212 }
213
encode(CassDecimal value)214 inline Buffer encode(CassDecimal value) {
215 Buffer buf(sizeof(int32_t) + value.varint_size);
216 size_t pos = buf.encode_int32(0, value.scale);
217 buf.copy(pos, value.varint, value.varint_size);
218 return buf;
219 }
220
221 Buffer encode(CassDuration value);
222
223 Buffer encode_with_length(CassDuration value);
224
225 }}} // namespace datastax::internal::core
226
227 #endif
228