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 ); }
48 const T*
data()
const {
return _alloc->template getDynamic< T >( _index ); }
56 template<
class Q = T >
57 const typename std::enable_if<!std::is_base_of<Zerobuf,Q>::value, Q>::type&
61 template<
class Q = T >
62 typename std::enable_if< !std::is_base_of< Zerobuf, Q >::value, Q >::type&
66 template<
class Q = T >
67 const typename std::enable_if< std::is_base_of<Zerobuf,Q>::value, Q >::type&
71 template<
class Q = T >
72 typename std::enable_if< std::is_base_of< Zerobuf, Q >::value, Q >::type&
76 template<
class Q = T >
void 78 !std::is_base_of<Zerobuf,Q>::value, Q>::type& );
81 template<
class Q = T >
void 83 std::is_base_of<Zerobuf,Q>::value, Q>::type&);
86 void reset(
Allocator& alloc ) { _alloc = &alloc; _zerobufs.clear(); }
92 template<
class Q = T >
void 93 fromJSON(
const Json::Value& json,
const typename std::enable_if<
94 std::is_base_of< Zerobuf, Q >::value, Q >::type* =
nullptr );
97 template<
class Q = T >
void 98 fromJSON(
const Json::Value& json,
const typename std::enable_if<
99 !std::is_base_of< Zerobuf, Q >::value, Q >::type* =
nullptr );
102 template<
class Q = T >
void 103 toJSON( Json::Value& json,
const typename std::enable_if<
104 std::is_base_of< Zerobuf, Q >::value, Q >::type* =
nullptr )
const;
107 template<
class Q = T >
void 108 toJSON( Json::Value& json,
const typename std::enable_if<
109 !std::is_base_of< Zerobuf, Q >::value, Q >::type* =
nullptr )
const;
112 template<
class Q = T >
void 113 fromJSONBinary(
const Json::Value& json,
const typename std::enable_if<
114 std::is_pod< Q >::value, Q >::type* =
nullptr );
117 template<
class Q = T >
void 118 toJSONBinary( Json::Value& json,
const typename std::enable_if<
119 std::is_pod< Q >::value, Q >::type* =
nullptr )
const;
124 mutable std::vector< T > _zerobufs;
130 size_t _getSize()
const {
return _alloc->getDynamicSize( _index ); }
131 void _resize(
const size_t size_ )
133 void copyBuffer( uint8_t*
data,
size_t size );
135 template<
class Q = T >
size_t _getElementSize(
136 typename std::enable_if< std::is_base_of< Zerobuf, Q >::value,
137 Q >::type* = 0 )
const 139 return Q::ZEROBUF_STATIC_SIZE();
142 template<
class Q = T >
size_t _getElementSize(
143 typename std::enable_if< !std::is_base_of< Zerobuf, Q >::value,
144 Q >::type* = 0 )
const 151 template<
class T >
inline 163 template<
class T >
inline 168 const size_t size_ = _getSize();
169 if( size_ != rhs._getSize( ))
176 return ::memcmp( static_cast< const void* >(
data( )),
177 static_cast< const void* >( rhs.data( )), size_ ) == 0;
180 template<
class T >
inline 186 template<
class T >
template<
class Q >
inline const typename 187 std::enable_if<!std::is_base_of<Zerobuf,Q>::value, Q>::type&
190 if( index >=
size( ))
191 throw std::runtime_error(
"Vector out of bounds read" );
193 return data()[ index ];
196 template<
class T >
template<
class Q >
inline typename 197 std::enable_if< !std::is_base_of< Zerobuf, Q >::value, Q >::type&
200 if( index >=
size( ))
201 throw std::runtime_error(
"Vector out of bounds read" );
203 return data()[ index ];
206 template<
class T >
template<
class Q >
inline const typename 207 std::enable_if< std::is_base_of<Zerobuf,Q>::value, Q >::type&
210 if( index >=
size( ))
211 throw std::runtime_error(
"Vector out of bounds read" );
213 while( _zerobufs.size() < index + 1 )
214 _zerobufs.emplace_back( AllocatorPtr(
215 new ConstDynamicSubAllocator( *_alloc, _index, _zerobufs.size(),
216 _getElementSize< T >( ))));
217 return _zerobufs[ index ];
220 template<
class T >
template<
class Q >
inline typename 221 std::enable_if< std::is_base_of< Zerobuf, Q >::value, Q >::type&
224 if( index >=
size( ))
225 throw std::runtime_error(
"Vector out of bounds read" );
227 while( _zerobufs.size() < index + 1 )
228 _zerobufs.emplace_back( AllocatorPtr(
229 new DynamicSubAllocator( *_alloc, _index, _zerobufs.size(),
230 _getElementSize< T >( ))));
231 return _zerobufs[ index ];
234 template<
class T >
template<
class Q >
inline void 236 const typename std::enable_if<!std::is_base_of<Zerobuf,Q>::value, Q>::type&
239 const size_t size_ = _getSize();
240 T* newPtr =
reinterpret_cast< T*
>(
242 newPtr[ size_ / _getElementSize< T >() ] = value;
245 template<
class T >
template<
class Q >
inline void 247 const typename std::enable_if<std::is_base_of<Zerobuf,Q>::value, Q>::type&
250 const size_t size_ = _getSize();
251 const zerobuf::Data&
zerobuf = value.toBinary();
253 size_ + zerobuf.size );
254 ::memcpy( newPtr + size_, zerobuf.ptr.get(), zerobuf.size );
257 template<
class T >
template<
class Q >
inline 259 const typename std::enable_if<std::is_base_of<Zerobuf,Q>::value, Q>::type* )
261 const size_t size_ = getJSONSize( json );
263 for(
size_t i = 0; i < size_; ++i )
264 zerobuf::fromJSON( getJSONField( json, i ), (*
this)[i] );
267 template<
class T >
template<
class Q >
inline 269 const typename std::enable_if<!std::is_base_of<Zerobuf,Q>::value, Q>::type*)
271 const size_t size_ = getJSONSize( json );
272 T* array =
reinterpret_cast< T*
>(
275 for(
size_t i = 0; i < size_; ++i )
276 array[i] = zerobuf::fromJSON< T >( getJSONField( json, i ));
279 template<
class T >
template<
class Q >
inline void 281 std::is_base_of< Zerobuf, Q >::value, Q >::type* )
const 283 const size_t size_ =
size();
284 for(
size_t i = 0; i < size_; ++i )
285 zerobuf::toJSON( static_cast< const Zerobuf& >(( *
this )[ i ]),
286 getJSONField( json, i ));
289 template<
class T >
template<
class Q >
inline void 291 !std::is_base_of< Zerobuf, Q >::value, Q >::type* )
const 293 const size_t size_ =
size();
294 for(
size_t i = 0; i < size_; ++i )
295 zerobuf::toJSON( (*
this)[i], getJSONField( json, i ));
298 template<
class T >
template<
class Q >
inline void 300 const typename std::enable_if< std::is_pod< Q >::value, Q >::type* )
302 const std::string& decoded = zerobuf::fromJSONBinary( json );
303 copyBuffer( (uint8_t*)decoded.data(), decoded.length( ));
306 template<
class T >
template<
class Q >
inline void 308 const typename std::enable_if< std::is_pod< Q >::value, Q >::type* )
const 310 zerobuf::toJSONBinary(
data(), _getSize(), json );
313 template<
class T >
inline 317 ::memcpy( to, data_, size_ );
320 template<
class T >
inline 321 std::ostream& operator << ( std::ostream& os, const Vector< T >& vector )
323 return os <<
typeid( vector ).name() <<
" of size " << vector.size();
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.
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.
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.