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 ); }
 
   49         { 
return const_cast< const Allocator* 
>( _alloc )->
template getDynamic< T >( _index ); }
 
   57     template< 
class Q = T >
 
   58     const typename std::enable_if<!std::is_base_of<Zerobuf,Q>::value, Q>::type&
 
   62     template< 
class Q = T >
 
   63     typename std::enable_if< !std::is_base_of< Zerobuf, Q >::value, Q >::type&
 
   67     template< 
class Q = T >
 
   68     const typename std::enable_if< std::is_base_of<Zerobuf,Q>::value, Q >::type&
 
   72     template< 
class Q = T >
 
   73     typename std::enable_if< std::is_base_of< Zerobuf, Q >::value, Q >::type&
 
   77     template< 
class Q = T > 
void 
   79                                 !std::is_base_of<Zerobuf,Q>::value, Q>::type& );
 
   82     template< 
class Q = T > 
void 
   84                                   std::is_base_of<Zerobuf,Q>::value, Q>::type&);
 
   87     void reset( 
Allocator& alloc ) { _alloc = &alloc; _zerobufs.clear(); }
 
   93     template< 
class Q = T > 
void 
   94     fromJSON( 
const Json::Value& json, 
const typename std::enable_if<
 
   95                   std::is_base_of< Zerobuf, Q >::value, Q >::type* = 
nullptr );
 
   98     template< 
class Q = T > 
void 
   99     fromJSON( 
const Json::Value& json, 
const typename std::enable_if<
 
  100                   !std::is_base_of< Zerobuf, Q >::value, Q >::type* = 
nullptr );
 
  103     template< 
class Q = T > 
void 
  104     toJSON( Json::Value& json, 
const typename std::enable_if<
 
  105              std::is_base_of< Zerobuf, Q >::value, Q >::type* = 
nullptr ) 
const;
 
  108     template< 
class Q = T > 
void 
  109     toJSON( Json::Value& json, 
const typename std::enable_if<
 
  110             !std::is_base_of< Zerobuf, Q >::value, Q >::type* = 
nullptr ) 
const;
 
  113     template< 
class Q = T > 
void 
  114     fromJSONBinary( 
const Json::Value& json, 
const typename std::enable_if<
 
  115                     std::is_pod< Q >::value, Q >::type* = 
nullptr );
 
  118     template< 
class Q = T > 
void 
  119     toJSONBinary( Json::Value& json, 
const typename std::enable_if<
 
  120                   std::is_pod< Q >::value, Q >::type* = 
nullptr ) 
const;
 
  125     mutable std::vector< T > _zerobufs;
 
  131     size_t _getSize()
 const { 
return _alloc->getDynamicSize( _index ); }
 
  132     void _resize( 
const size_t size_ )
 
  134     void copyBuffer( uint8_t* 
data, 
size_t size );
 
  136     template< 
class Q = T > 
size_t _getElementSize(
 
  137         typename std::enable_if< std::is_base_of< Zerobuf, Q >::value,
 
  138                                                   Q >::type* = 0 )
 const 
  140         return Q::ZEROBUF_STATIC_SIZE();
 
  143     template< 
class Q = T > 
size_t _getElementSize(
 
  144         typename std::enable_if< !std::is_base_of< Zerobuf, Q >::value,
 
  145                                                    Q >::type* = 0 )
 const 
  152 template< 
class T > 
inline 
  153 Vector< T >::Vector( Allocator& alloc, 
const size_t index )
 
  164 template< 
class T > 
inline 
  169     const size_t size_ = _getSize();
 
  170     if( size_ != rhs._getSize( ))
 
  177     return ::memcmp( static_cast< const void* >( data( )),
 
  178                      static_cast< const void* >( rhs.data( )), size_ ) == 0;
 
  181 template< 
class T > 
inline 
  184     return !(operator == ( rhs ));
 
  187 template< 
class T > 
template< 
class Q > 
inline const typename 
  188 std::enable_if<!std::is_base_of<Zerobuf,Q>::value, Q>::type&
 
  191     if( index >= size( ))
 
  192         throw std::runtime_error( 
"Vector out of bounds read" );
 
  194     return data()[ index ];
 
  197 template< 
class T > 
template< 
class Q > 
inline typename 
  198 std::enable_if< !std::is_base_of< Zerobuf, Q >::value, Q >::type&
 
  201     if( index >= size( ))
 
  202         throw std::runtime_error( 
"Vector out of bounds read" );
 
  204     return data()[ index ];
 
  207 template< 
class T > 
template< 
class Q > 
inline const typename 
  208 std::enable_if< std::is_base_of<Zerobuf,Q>::value, Q >::type&
 
  211     if( index >= size( ))
 
  212         throw std::runtime_error( 
"Vector out of bounds read" );
 
  214     while( _zerobufs.size() < index + 1 )
 
  215         _zerobufs.emplace_back( AllocatorPtr(
 
  216             new ConstDynamicSubAllocator( *_alloc, _index, _zerobufs.size(),
 
  217                                           _getElementSize< T >( ))));
 
  218     return _zerobufs[ index ];
 
  221 template< 
class T > 
template< 
class Q > 
inline typename 
  222 std::enable_if< std::is_base_of< Zerobuf, Q >::value, Q >::type&
 
  225     if( index >= size( ))
 
  226         throw std::runtime_error( 
"Vector out of bounds read" );
 
  228     while( _zerobufs.size() < index + 1 )
 
  229         _zerobufs.emplace_back( AllocatorPtr(
 
  230             new DynamicSubAllocator( *_alloc, _index, _zerobufs.size(),
 
  231                                      _getElementSize< T >( ))));
 
  232     return _zerobufs[ index ];
 
  235 template< 
class T > 
template< 
class Q > 
inline void 
  237     const typename std::enable_if<!std::is_base_of<Zerobuf,Q>::value, Q>::type&
 
  240     const size_t size_ = _getSize();
 
  241     T* newPtr = 
reinterpret_cast< T* 
>(
 
  243     newPtr[ size_ / _getElementSize< T >() ] = value;
 
  246 template< 
class T > 
template< 
class Q > 
inline void 
  248     const typename std::enable_if<std::is_base_of<Zerobuf,Q>::value, Q>::type&
 
  251     const size_t size_ = _getSize();
 
  252     const zerobuf::Data& 
zerobuf = value.toBinary();
 
  254                                                size_ + zerobuf.size );
 
  255     ::memcpy( newPtr + size_, zerobuf.ptr.get(), zerobuf.size );
 
  258 template< 
class T > 
template< 
class Q > 
inline 
  260     const typename std::enable_if<std::is_base_of<Zerobuf,Q>::value, Q>::type* )
 
  262     const size_t size_ = getJSONSize( json );
 
  264     for( 
size_t i = 0; i < size_; ++i )
 
  265         zerobuf::fromJSON( getJSONField( json, i ), (*
this)[i] );
 
  268 template< 
class T > 
template< 
class Q > 
inline 
  270     const typename std::enable_if<!std::is_base_of<Zerobuf,Q>::value, Q>::type*)
 
  272     const size_t size_ = getJSONSize( json );
 
  273     T* array = 
reinterpret_cast< T* 
>(
 
  276     for( 
size_t i = 0; i < size_; ++i )
 
  277         array[i] = zerobuf::fromJSON< T >( getJSONField( json, i ));
 
  280 template< 
class T > 
template< 
class Q > 
inline void 
  282                         std::is_base_of< Zerobuf, Q >::value, Q >::type* )
 const 
  284     const size_t size_ = size();
 
  285     for( 
size_t i = 0; i < size_; ++i )
 
  286         zerobuf::toJSON( static_cast< const Zerobuf& >(( *
this )[ i ]),
 
  287                          getJSONField( json, i ));
 
  290 template< 
class T > 
template< 
class Q > 
inline void 
  292                        !std::is_base_of< Zerobuf, Q >::value, Q >::type* )
 const 
  294     const size_t size_ = size();
 
  295     for( 
size_t i = 0; i < size_; ++i )
 
  296         zerobuf::toJSON( (*
this)[i], getJSONField( json, i ));
 
  299 template< 
class T > 
template< 
class Q > 
inline void 
  301     const typename std::enable_if< std::is_pod< Q >::value, Q >::type* )
 
  303     const std::string& decoded = zerobuf::fromJSONBinary( json );
 
  304     copyBuffer( (uint8_t*)decoded.data(), decoded.length( ));
 
  307 template< 
class T > 
template< 
class Q > 
inline void 
  309     const typename std::enable_if< std::is_pod< Q >::value, Q >::type* )
 const 
  311     zerobuf::toJSONBinary( data(), _getSize(), json );
 
  314 template< 
class T > 
inline 
  318     ::memcpy( to, data_, size_ );
 
  321 template< 
class T > 
inline 
  322 std::ostream& operator << ( std::ostream& os, const Vector< T >& vector )
 
  324     return os << 
typeid( vector ).name() << 
" of size " << vector.size();
 
  328 std::ostream& operator << ( std::ostream& os, const Vector< char >& string )
 
  330     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 from its JSON representation. 
Zero-copy, zero-serialize, zero-hassle protocol buffers. 
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. 
const std::enable_if<!std::is_base_of< Zerobuf, Q >::value, Q >::type & operator[](const size_t index) const 
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.