ZeroBuf  0.1.0
ZeroBuf is a replacement for FlatBuffers
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Vector.h
1 
2 /* Copyright (c) 2015, Human Brain Project
3  * Stefan.Eilemann@epfl.ch
4  */
5 
6 #ifndef ZEROBUF_VECTOR_H
7 #define ZEROBUF_VECTOR_H
8 
9 #include <zerobuf/BaseVector.h> // base class
10 
11 namespace zerobuf
12 {
18 template< class T >
19 class Vector : public BaseVector< Allocator, T >
20 {
22 
23 public:
28  Vector( Allocator* alloc, size_t index );
29  ~Vector() {}
30 
31  void push_back( const T& value );
32  T* data()
33  { return Super::_parent->template getDynamic< T >( Super::_index ); }
34 
35 private:
36  Vector();
37  void _resize( const size_t size )
38  { Super::_parent->updateAllocation( Super::_index, size ); }
39  void copyBuffer( uint8_t* data, size_t size );
40 };
41 
42 // Implementation
43 template< class T > inline
44 Vector< T >::Vector( Allocator* alloc, const size_t index )
45  : BaseVector< Allocator, T >( alloc, index )
46 {}
47 
48 template< class T > inline
49 void Vector< T >::push_back( const T& value )
50 {
51  const size_t size_ = Super::_getSize();
52  const T* oldPtr = data();
53  T* newPtr = reinterpret_cast< T* >(
54  Super::_parent->updateAllocation( Super::_index, size_ + sizeof( T )));
55  if( oldPtr != newPtr )
56  ::memcpy( newPtr, oldPtr, size_ );
57 
58  newPtr[ size_ / sizeof(T) ] = value;
59 }
60 
61 template< class T > inline
62 void Vector< T >::copyBuffer( uint8_t* data_, size_t size )
63 {
64  void* to = Super::_parent->updateAllocation( Super::_index, size );
65  ::memcpy( to, data_, size );
66 }
67 
68 }
69 
70 #endif
Allocator base class and interface.
Definition: Allocator.h:20
Non-const vector.
Definition: Vector.h:19