11#pragma once
22#include < Arduino.h>
3+ #include < GTL.h>
34#include < StringUtils.h>
45#include < limits.h>
5- #include < stack/stack.h>
66
77// ============== const ==============
88#define BS_MAX_LEN ((size_t )0b0001111111111111 )
@@ -39,27 +39,29 @@ class BSON : private gtl::stack_uniq<uint8_t> {
3939 using gtl::stack_uniq<uint8_t >::clear;
4040 using gtl::stack_uniq<uint8_t >::move;
4141
42- BSON () {}
43- BSON (BSON& b) {
44- move (b);
45- }
46- BSON (BSON&& b) {
47- move (b);
48- }
49- BSON& operator =(BSON& b) {
50- move (b);
51- return *this ;
52- }
53- BSON& operator =(BSON&& b) {
54- move (b);
55- return *this ;
56- }
57-
5842 // максимальная длина строк и бинарных данных
5943 static size_t maxDataLength () {
6044 return BS_MAX_LEN;
6145 }
6246
47+ // размер числа в байтах
48+ static uint8_t uint32Size (uint8_t * p) {
49+ if (p[3 ]) return 4 ;
50+ if (p[2 ]) return 3 ;
51+ if (p[1 ]) return 2 ;
52+ if (p[0 ]) return 1 ;
53+ return 0 ;
54+ }
55+
56+ // размер числа в байтах
57+ static uint8_t uint64Size (uint8_t * p) {
58+ if (p[7 ]) return 8 ;
59+ if (p[6 ]) return 7 ;
60+ if (p[5 ]) return 6 ;
61+ if (p[4 ]) return 5 ;
62+ return uint32Size (p);
63+ }
64+
6365 operator Text () {
6466 return toText ();
6567 }
@@ -153,7 +155,7 @@ class BSON : private gtl::stack_uniq<uint8_t> {
153155 // ============== val float ==============
154156 BSON& add (float value, int dec) {
155157 push (BS_FLOAT | BS_LSB5 (dec));
156- concat (( const uint8_t *) &value, 4 );
158+ write ( &value, 4 );
157159 return *this ;
158160 }
159161 BSON& add (double value, int dec) {
@@ -174,7 +176,7 @@ class BSON : private gtl::stack_uniq<uint8_t> {
174176 BSON& add (const Text& text) {
175177 uint16_t len = min ((size_t )text.length (), BS_MAX_LEN);
176178 beginStr (len);
177- concat (( const uint8_t *) text.str (), len, text.pgm ());
179+ write ( text.str (), len, text.pgm ());
178180 return *this ;
179181 }
180182 inline void operator =(const Text& val) { add (val); }
@@ -203,7 +205,7 @@ class BSON : private gtl::stack_uniq<uint8_t> {
203205 return true ;
204206 }
205207 BSON& add (const void * data, size_t size, bool pgm = false ) {
206- if (beginBin (size)) concat (( const uint8_t *) data, size, pgm);
208+ if (beginBin (size)) write ( data, size, pgm);
207209 return *this ;
208210 }
209211
@@ -221,30 +223,15 @@ class BSON : private gtl::stack_uniq<uint8_t> {
221223 // ============== private ==============
222224 private:
223225 BSON& _int32 (void * p, bool neg = false ) {
224- uint8_t len = _uint32Size ((uint8_t *)p);
226+ uint8_t len = uint32Size ((uint8_t *)p);
225227 push (BS_INTEGER | (neg ? BS_NEGATIVE : 0 ) | len);
226- concat (( const uint8_t *) p, len);
228+ write ( p, len);
227229 return *this ;
228230 }
229231 BSON& _int64 (void * p, bool neg = false ) {
230- uint8_t len = _uint64Size ((uint8_t *)p);
232+ uint8_t len = uint64Size ((uint8_t *)p);
231233 push (BS_INTEGER | (neg ? BS_NEGATIVE : 0 ) | len);
232- concat (( const uint8_t *) p, len);
234+ write ( p, len);
233235 return *this ;
234236 }
235-
236- uint8_t _uint32Size (uint8_t * p) {
237- if (p[3 ]) return 4 ;
238- if (p[2 ]) return 3 ;
239- if (p[1 ]) return 2 ;
240- if (p[0 ]) return 1 ;
241- return 0 ;
242- }
243- uint8_t _uint64Size (uint8_t * p) {
244- if (p[7 ]) return 8 ;
245- if (p[6 ]) return 7 ;
246- if (p[5 ]) return 6 ;
247- if (p[4 ]) return 5 ;
248- return _uint32Size (p);
249- }
250237};
0 commit comments