4040#include " velocypack/Buffer.h"
4141#include " velocypack/Exception.h"
4242#include " velocypack/Options.h"
43+ #include " velocypack/Serializable.h"
4344#include " velocypack/Slice.h"
4445#include " velocypack/StringRef.h"
4546#include " velocypack/Value.h"
@@ -74,7 +75,7 @@ class Builder {
7475 // buffer. Whenever the stack is empty, one can use the start,
7576 // size and slice methods to get out the ready built VPack
7677 // object(s).
77-
78+
7879 private:
7980 std::shared_ptr<Buffer<uint8_t >> _buffer; // Here we collect the result
8081 Buffer<uint8_t >* _bufferPtr; // used for quicker access than shared_ptr
@@ -89,10 +90,10 @@ class Builder {
8990
9091 public:
9192 Options const * options;
92-
93- // create an empty Builder, using default Options
93+
94+ // create an empty Builder, using default Options
9495 Builder ();
95-
96+
9697 explicit Builder (Options const * options);
9798 explicit Builder (std::shared_ptr<Buffer<uint8_t >> const & buffer,
<
8000
/code>9899 Options const * options = &Options::Defaults);
@@ -208,15 +209,15 @@ class Builder {
208209 ValueLength const tos = _stack.back ();
209210 return _start[tos] == 0x0b || _start[tos] == 0x14 ;
210211 }
211-
212+
212213 inline void openArray (bool unindexed = false ) {
213214 openCompoundValue (unindexed ? 0x13 : 0x06 );
214215 }
215216
216217 inline void openObject (bool unindexed = false ) {
217218 openCompoundValue (unindexed ? 0x14 : 0x0b );
218219 }
219-
220+
220221 template <typename T>
221222 uint8_t * addUnchecked (char const * attrName, std::size_t attrLength, T const & sub) {
222223 bool haveReported = false ;
@@ -251,7 +252,7 @@ class Builder {
251252 inline uint8_t * add (char const * attrName, std::size_t attrLength, Value const & sub) {
252253 return addInternal<Value>(attrName, attrLength, sub);
253254 }
254-
255+
255256 // Add a subvalue into an object from a Slice:
256257 inline uint8_t * add (std::string const & attrName, Slice const & sub) {
257258 return addInternal<Slice>(attrName, sub);
@@ -265,7 +266,7 @@ class Builder {
265266 inline uint8_t * add (char const * attrName, std::size_t attrLength, Slice const & sub) {
266267 return addInternal<Slice>(attrName, attrLength, sub);
267268 }
268-
269+
269270 // Add a subvalue into an object from a ValuePair:
270271 inline uint8_t * add (std::string const & attrName, ValuePair const & sub) {
271272 return addInternal<ValuePair>(attrName, sub);
@@ -279,22 +280,41 @@ class Builder {
279280 inline uint8_t * add (char const * attrName, std::size_t attrLength, ValuePair const & sub) {
280281 return addInternal<ValuePair>(attrName, attrLength, sub);
281282 }
282-
283+
284+ // Add a subvalue into an object from a Serializable:
285+ inline uint8_t * add (std::string const & attrName, Serialize const & sub) {
286+ return addInternal<Serializable>(attrName, sub._sable );
287+ }
288+ inline uint8_t * add (StringRef const & attrName, Serialize const & sub) {
289+ return addInternal<Serializable>(attrName, sub._sable );
290+ }
291+ inline uint8_t * add (char const * attrName, Serialize const & sub) {
292+ return addInternal<Serializable>(attrName, sub._sable );
293+ }
294+ inline uint8_t * add (char const * attrName, std::size_t attrLength, Serialize const & sub) {
295+ return addInternal<Serializable>(attrName, attrLength, sub._sable );
296+ }
297+
283298 // Add a subvalue into an array from a Value:
284299 inline uint8_t * add (Value const & sub) {
285- return addInternal<Value>(sub);
300+ return addInternal<Value>(sub);
286301 }
287-
302+
288303 // Add a slice to an array
289304 inline uint8_t * add (Slice const & sub) {
290- return addInternal<Slice>(sub);
305+ return addInternal<Slice>(sub);
291306 }
292307
293308 // Add a subvalue into an array from a ValuePair:
294309 inline uint8_t * add (ValuePair const & sub) {
295310 return addInternal<ValuePair>(sub);
296311 }
297312
313+ // Add a subvalue into an array from a Serializable:
314+ inline uint8_t * add (Serialize const & sub) {
315+ return addInternal<Serializable>(sub._sable );
316+ }
317+
298318 // Add an External slice to an array
299319 uint8_t * addExternal (uint8_t const * sub) {
300320 if (options->disallowExternals ) {
@@ -327,7 +347,7 @@ class Builder {
327347 throw ;
328348 }
329349 }
330-
350+
331351 // Add all subkeys and subvalues into an object from an ObjectIterator
332352 // and leaves open the object intentionally
333353 uint8_t * add (ObjectIterator&& sub);
@@ -445,18 +465,23 @@ class Builder {
445465
446466 void addInt (int64_t v) {
447467 if (v >= 0 && v <= 9 ) {
468+ // SmallInt
448469 appendByte (static_cast <uint8_t >(0x30 + v));
449470 } else if (v < 0 && v >= -6 ) {
471+ // SmallInt
450472 appendByte (static_cast <uint8_t >(0x40 + v));
451473 } else {
474+ // regular int
452475 appendInt (v, 0x1f );
453476 }
454477 }
455478
456479 void addUInt (uint64_t v) {
457480 if (v <= 9 ) {
481+ // SmallInt
458482 appendByte (static_cast <uint8_t >(0x30 + v));
459483 } else {
484+ // regular UInt
460485 appendUInt (v, 0x27 );
461486 }
462487 }
@@ -524,7 +549,7 @@ class Builder {
524549 uint8_t * addInternal (std::string const & attrName, T const & sub) {
525550 return addInternal<T>(attrName.data (), attrName.size (), sub);
526551 }
527-
552+
528553 template <typename T>
529554 uint8_t * addInternal (StringRef const & attrName, T const & sub) {
530555 return addInternal<T>(attrName.data (), attrName.size (), sub);
@@ -579,7 +604,7 @@ class Builder {
579604 throw ;
580605 }
581606 }
582-
607+
583608 void addCompoundValue (uint8_t type) {
584609 reserve (9 );
585610 // an Array or Object is started:
@@ -624,6 +649,12 @@ class Builder {
624649
625650 uint8_t * set (Slice const & item);
626651
652+ uint8_t * set (Serializable const & sable) {
653+ auto const oldPos = _pos;
654+ sable.toVelocyPack (*this );
655+ return _start + oldPos;
656+ }
657+
627658 void cleanupAdd () noexcept {
628659 std::size_t depth = _stack.size () - 1 ;
629660 VELOCYPACK_ASSERT (!_index[depth].empty ());
@@ -640,7 +671,7 @@ class Builder {
640671 reserve (n);
641672 appendLengthUnchecked<n>(v);
642673 }
643-
674+
644675 template <uint64_t n>
645676 void appendLengthUnchecked (ValueLength v) {
646677 for (uint64_t i = 0 ; i < n; ++i) {
@@ -695,18 +726,18 @@ class Builder {
695726 x >>= 8 ;
696727 }
697728 }
698-
729+
699730 inline void appendByte (uint8_t value) {
700731 reserve (1 );
701732 appendByteUnchecked (value);
702733 }
703-
734+
704735 inline void appendByteUnchecked (uint8_t value) {
705736 _start[_pos++] = value;
706737 VELOCYPACK_ASSERT (_bufferPtr != nullptr );
707738 _bufferPtr->advance ();
708739 }
709-
740+
710741 inline void resetTo (std::size_t value) {
711742 _pos = value;
712743 VELOCYPACK_ASSERT (_bufferPtr != nullptr );
@@ -719,7 +750,7 @@ class Builder {
719750 VELOCYPACK_ASSERT (_bufferPtr != nullptr );
720751 _bufferPtr->advance (value);
721752 }
722-
753+
723754 // move byte position x bytes back
724755 inline void rollback (std::size_t value) noexcept {
725756 _pos -= value;
0 commit comments