6 #ifndef ZEROBUF_VECTOR_H 7 #define ZEROBUF_VECTOR_H 9 #include <zerobuf/DynamicSubAllocator.h> 10 #include <zerobuf/Zerobuf.h> 11 #include <zerobuf/json.h> 32 Vector( Allocator& alloc,
size_t index );
36 bool empty()
const {
return _getSize() == 0; }
39 uint64_t
size()
const {
return _getSize() / _getElementSize< T >(); }
45 T*
data() {
return _alloc->template getDynamic< T >( _index ); }
50 ( _alloc )->
template getDynamic< T >( _index ); }
58 template<
class Q = T >
59 const typename std::enable_if<!std::is_base_of<Zerobuf,Q>::value, Q>::type&
63 template<
class Q = T >
64 typename std::enable_if< !std::is_base_of< Zerobuf, Q >::value, Q >::type&
68 template<
class Q = T >
69 const typename std::enable_if< std::is_base_of<Zerobuf,Q>::value, Q >::type&
73 template<
class Q = T >
74 typename std::enable_if< std::is_base_of< Zerobuf, Q >::value, Q >::type&
78 template<
class Q = T >
void 80 !std::is_base_of<Zerobuf,Q>::value, Q>::type& );
83 template<
class Q = T >
void 85 std::is_base_of<Zerobuf,Q>::value, Q>::type& );
88 void reset(
Allocator& alloc ) { _alloc = &alloc; _zerobufs.clear(); }
94 template<
class Q = T >
void 95 fromJSON(
const Json::Value& json,
const typename std::enable_if<
96 std::is_base_of< Zerobuf, Q >::value, Q >::type* =
nullptr );
99 template<
class Q = T >
void 100 fromJSON(
const Json::Value& json,
const typename std::enable_if<
101 std::is_enum< Q >::value, Q >::type* =
nullptr );
104 template<
class Q = T >
void 105 fromJSON(
const Json::Value& json,
const typename std::enable_if<
106 std::is_arithmetic< Q >::value, Q >::type* =
nullptr );
109 template<
class Q = T >
void 110 fromJSON(
const Json::Value& json,
const typename std::enable_if<
111 std::is_same< Q, servus::uint128_t >::value, Q >::type* =
nullptr );
114 template<
class Q = T >
void 115 toJSON( Json::Value& json,
const typename std::enable_if<
116 std::is_base_of< Zerobuf, Q >::value, Q >::type* =
nullptr )
const;
119 template<
class Q = T >
void 120 toJSON( Json::Value& json,
const typename std::enable_if<
121 std::is_enum< Q >::value, Q >::type* =
nullptr )
const;
124 template<
class Q = T >
void 125 toJSON( Json::Value& json,
const typename std::enable_if<
126 std::is_arithmetic< Q >::value, Q >::type* =
nullptr )
const;
129 template<
class Q = T >
void 130 toJSON( Json::Value& json,
const typename std::enable_if<
131 std::is_same< Q, servus::uint128_t >::value, Q >::type* =
nullptr )
const;
134 template<
class Q = T >
void 135 fromJSONBinary(
const Json::Value& json,
const typename std::enable_if<
136 std::is_pod< Q >::value, Q >::type* =
nullptr );
139 template<
class Q = T >
void 140 toJSONBinary( Json::Value& json,
const typename std::enable_if<
141 std::is_pod< Q >::value, Q >::type* =
nullptr )
const;
146 mutable std::vector< T > _zerobufs;
152 size_t _getSize()
const {
return _alloc->getDynamicSize( _index ); }
153 void _resize(
const size_t size_ )
155 void copyBuffer( uint8_t*
data,
size_t size );
157 template<
class Q = T >
size_t _getElementSize(
158 typename std::enable_if< std::is_base_of< Zerobuf, Q >::value,
159 Q >::type* = 0 )
const 161 return Q::ZEROBUF_STATIC_SIZE();
164 template<
class Q = T >
size_t _getElementSize(
165 typename std::enable_if< !std::is_base_of< Zerobuf, Q >::value,
166 Q >::type* = 0 )
const 173 template<
class T >
inline 185 template<
class T >
inline 190 const size_t size_ = _getSize();
191 if( size_ != rhs._getSize( ))
198 return ::memcmp( static_cast< const void* >(
data( )),
199 static_cast< const void* >( rhs.data( )), size_ ) == 0;
202 template<
class T >
inline 208 template<
class T >
template<
class Q >
inline const typename 209 std::enable_if<!std::is_base_of<Zerobuf,Q>::value, Q>::type&
212 if( index >=
size( ))
213 throw std::runtime_error(
"Vector out of bounds read" );
215 return data()[ index ];
218 template<
class T >
template<
class Q >
inline typename 219 std::enable_if< !std::is_base_of< Zerobuf, Q >::value, Q >::type&
222 if( index >=
size( ))
223 throw std::runtime_error(
"Vector out of bounds read" );
225 return data()[ index ];
228 template<
class T >
template<
class Q >
inline const typename 229 std::enable_if< std::is_base_of<Zerobuf,Q>::value, Q >::type&
232 if( index >=
size( ))
233 throw std::runtime_error(
"Vector out of bounds read" );
235 while( _zerobufs.size() < index + 1 )
236 _zerobufs.emplace_back( AllocatorPtr(
238 _getElementSize< T >( ))));
239 return _zerobufs[ index ];
242 template<
class T >
template<
class Q >
inline typename 243 std::enable_if< std::is_base_of< Zerobuf, Q >::value, Q >::type&
246 if( index >=
size( ))
247 throw std::runtime_error(
"Vector out of bounds read" );
249 while( _zerobufs.size() < index + 1 )
250 _zerobufs.emplace_back( AllocatorPtr(
252 _getElementSize< T >( ))));
253 return _zerobufs[ index ];
256 template<
class T >
template<
class Q >
inline void 258 const typename std::enable_if<!std::is_base_of<Zerobuf,Q>::value, Q>::type&
261 const size_t size_ = _getSize();
262 T* newPtr =
reinterpret_cast< T*
>(
264 newPtr[ size_ / _getElementSize< T >() ] = value;
267 template<
class T >
template<
class Q >
inline void 269 const typename std::enable_if<std::is_base_of<Zerobuf,Q>::value, Q>::type&
272 const size_t size_ = _getSize();
273 const zerobuf::Data&
zerobuf = value.toBinary();
275 size_ + zerobuf.size );
276 ::memcpy( newPtr + size_, zerobuf.ptr.get(), zerobuf.size );
279 template<
class T >
template<
class Q >
inline 281 const typename std::enable_if<std::is_base_of<Zerobuf,Q>::value, Q>::type* )
283 const size_t size_ = getJSONSize( json );
285 for(
size_t i = 0; i < size_; ++i )
286 zerobuf::fromJSON( getJSONField( json, i ), (*
this)[i] );
289 template<
class T >
template<
class Q >
inline 291 const typename std::enable_if<std::is_enum< Q>::value, Q>::type*)
293 const size_t size_ = getJSONSize( json );
294 T* array =
reinterpret_cast< T*
>(
297 for(
size_t i = 0; i < size_; ++i )
298 array[i] = string_to_enum< T >( zerobuf::fromJSON< std::string >
299 ( getJSONField( json, i )));
302 template<
class T >
template<
class Q >
inline 304 const typename std::enable_if<std::is_arithmetic< Q>::value, Q>::type*)
306 const size_t size_ = getJSONSize( json );
307 T* array =
reinterpret_cast< T*
>(
310 for(
size_t i = 0; i < size_; ++i )
311 array[i] = zerobuf::fromJSON< T >( getJSONField( json, i ));
314 template<
class T >
template<
class Q >
inline 316 const typename std::enable_if< std::is_same< Q,
317 servus::uint128_t >::value, Q>::type*)
319 const size_t size_ = getJSONSize( json );
320 T* array =
reinterpret_cast< T*
>(
323 for(
size_t i = 0; i < size_; ++i )
324 array[i] = zerobuf::fromJSON< T >( getJSONField( json, i ));
327 template<
class T >
template<
class Q >
inline void 329 std::is_base_of< Zerobuf, Q >::value, Q >::type* )
const 331 const size_t size_ =
size();
332 zerobuf::emptyJSONArray( json );
333 for(
size_t i = 0; i < size_; ++i )
334 zerobuf::toJSON( static_cast< const Zerobuf& >(( *
this )[ i ]),
335 getJSONField( json, i ));
338 template<
class T >
template<
class Q >
inline void 340 std::is_enum< Q >::value, Q >::type* )
const 342 const size_t size_ =
size();
343 zerobuf::emptyJSONArray( json );
344 for(
size_t i = 0; i < size_; ++i )
345 zerobuf::toJSON( enum_to_string< T >( (*
this)[i] ),
346 getJSONField( json, i ));
349 template<
class T >
template<
class Q >
inline void 351 std::is_arithmetic< Q >::value, Q >::type* )
const 353 const size_t size_ =
size();
354 zerobuf::emptyJSONArray( json );
355 for(
size_t i = 0; i < size_; ++i )
356 zerobuf::toJSON( (*
this)[i], getJSONField( json, i ));
359 template<
class T >
template<
class Q >
inline void 361 std::is_same< Q, servus::uint128_t >::value, Q >::type* )
const 363 const size_t size_ =
size();
364 zerobuf::emptyJSONArray( json );
365 for(
size_t i = 0; i < size_; ++i )
366 zerobuf::toJSON( (*
this)[i], getJSONField( json, i ));
369 template<
class T >
template<
class Q >
inline void 371 const typename std::enable_if< std::is_pod< Q >::value, Q >::type* )
373 const std::string& decoded = zerobuf::fromJSONBinary( json );
374 copyBuffer( (uint8_t*)decoded.data(), decoded.length( ));
377 template<
class T >
template<
class Q >
inline void 379 const typename std::enable_if< std::is_pod< Q >::value, Q >::type* )
const 381 zerobuf::toJSONBinary(
data(), _getSize(), json );
384 template<
class T >
inline 388 ::memcpy( to, data_, size_ );
391 template<
class T >
inline 392 std::ostream& operator << ( std::ostream& os, const Vector< T >& vector )
394 return os <<
typeid( vector ).name() <<
" of size " << vector.size();
398 std::ostream& operator << ( std::ostream& os, const Vector< char >& string )
400 return os <<
string.data();
void compact(float)
Remove unused memory from vector and all members.
bool operator==(const Vector &rhs) const
bool operator!=(const Vector &rhs) const
void fromJSON(const Json::Value &json, const typename std::enable_if< std::is_base_of< Zerobuf, Q >::value, Q >::type *=nullptr)
Update this vector of ZeroBufs from its JSON representation.
Zero-copy, zero-serialize, zero-hassle protocol buffers.
const std::enable_if<!std::is_base_of< Zerobuf, Q >::value, Q >::type & operator[](const size_t index) const
virtual uint8_t * updateAllocation(size_t, bool, size_t)
Update allocation of the dynamic elem at index to have newSize bytes.
void push_back(const typename std::enable_if< !std::is_base_of< Zerobuf, Q >::value, Q >::type &)
Insert a builtin element at the end of the vector.
void fromJSONBinary(const Json::Value &json, const typename std::enable_if< std::is_pod< Q >::value, Q >::type *=nullptr)
Update this vector from its JSON, base64-encoded representation.
void toJSONBinary(Json::Value &json, const typename std::enable_if< std::is_pod< Q >::value, Q >::type *=nullptr) const
Allocator base class and interface.
void clear()
Empty the vector.
A zerobuf child allocator which manages a dynamic allocation of statically-sized members.
void toJSON(Json::Value &json, const typename std::enable_if< std::is_base_of< Zerobuf, Q >::value, Q >::type *=nullptr) const
STL-like vector abstraction for dynamic arrays in a zerobuf.