CARMA C++
ByteBuffer.h
1 #ifndef CARMA_UTIL_BYTEBUFFER_H
2 #define CARMA_UTIL_BYTEBUFFER_H
3 
4 #include <algorithm>
5 
6 
7 namespace carma {
8 namespace util {
9 
10 
16 class ByteBuffer {
17  public:
19  explicit ByteBuffer( );
20 
22  ~ByteBuffer( );
23 
25  void swap( ByteBuffer & rhs );
26 
28  char * get( ) const;
29 
31  size_t size( ) const;
32 
34  size_t reserve( ) const;
35 
40  void destructiveReserve( size_t count );
41 
46  void destructiveResize( size_t count );
47 
48  private:
49  ByteBuffer( const ByteBuffer & rhs );
50  ByteBuffer & operator=( const ByteBuffer & rhs );
51 
52  void internalFree( );
53  void internalDestructiveReserve( size_t count );
54 
55  char * ptr_;
56  size_t allocSize_;
57  size_t presentSize_;
58 };
59 
60 
61 } // namespace carma::util
62 } // namespace carma
63 
64 
65 // ******* Below here is simply implementation *******
66 
67 
68 inline
70 ptr_( 0 ),
71 allocSize_( 0 ),
72 presentSize_( 0 )
73 {
74 }
75 
76 
77 inline
79 {
80  if ( ptr_ != 0 )
81  internalFree();
82 }
83 
84 
85 inline void
87 {
88  ::std::swap( ptr_, rhs.ptr_ );
89  ::std::swap( allocSize_, rhs.allocSize_ );
90  ::std::swap( presentSize_, rhs.presentSize_ );
91 }
92 
93 
94 inline char *
96 {
97  return ptr_;
98 }
99 
100 
101 inline size_t
103 {
104  return presentSize_;
105 }
106 
107 
108 inline size_t
110 {
111  return allocSize_;
112 }
113 
114 
115 inline void
117 {
118  if ( count > allocSize_ )
119  internalDestructiveReserve( count );
120 }
121 
122 
123 inline void
125 {
126  if ( count > allocSize_ )
127  internalDestructiveReserve( count );
128 
129  presentSize_ = count;
130 }
131 
132 
133 #endif
ByteBuffer()
Construct an empty buffer.
Definition: ByteBuffer.h:69
size_t reserve() const
Get the reserved size of the buffer.
Definition: ByteBuffer.h:109
char * get() const
Get the present pointer to the buffer.
Definition: ByteBuffer.h:95
~ByteBuffer()
Destruct a ByteBuffer.
Definition: ByteBuffer.h:78
void swap(ByteBuffer &rhs)
Swap this instance with another ByteBuffer instance.
Definition: ByteBuffer.h:86
size_t size() const
Get the present size of the buffer.
Definition: ByteBuffer.h:102
void destructiveReserve(size_t count)
Reserve space in the buffer.
Definition: ByteBuffer.h:116
void destructiveResize(size_t count)
Resize the buffer.
Definition: ByteBuffer.h:124
Manages a (possibly destructively) resizable buffer of raw bytes.
Definition: ByteBuffer.h:16