source: source/ariba/utility/transport/messages/buffer.hpp@ 12774

Last change on this file since 12774 was 10653, checked in by Michael Tänzer, 12 years ago

Merge the ASIO branch back into trunk

File size: 4.4 KB
Line 
1//-----------------------------------------------------------------------------
2// Part of reboost (http://reboost.org). Released under the
3// BSD 2-clause license (http://www.opensource.org/licenses/bsd-license.php).
4// Copyright 2012, Sebastian Mies <mies@reboost.org> --- All rights reserved.
5//-----------------------------------------------------------------------------
6
7#ifndef BUFFER_HPP_
8#define BUFFER_HPP_
9
10#include<cassert>
11#include<iostream>
12
13namespace reboost {
14
15/// buffer size and octet
16typedef size_t bsize_t;
17typedef unsigned char boctet_t;
18
19/**
20 * A simple buffer.
21 *
22 * @author Sebastian Mies <mies@cpptools.org>
23 */
24class buffer_t {
25 typedef buffer_t self;
26protected:
27 bsize_t size_;
28 boctet_t * data_;
29
30public:
31 /// a buffer of zero size, pointing to NULL
32 inline buffer_t() :
33 size_(0), data_((boctet_t*) 0) {
34 }
35
36 /// copy constructor
37 inline buffer_t(const self& rhs) :
38 size_(rhs.size_), data_(rhs.data_){
39 }
40
41 /// allocate a buffer of a certain size
42 inline buffer_t(bsize_t size) :
43 size_(size), data_(new boctet_t[size]) {
44 }
45
46 /// convenience constructor
47 inline buffer_t(bsize_t size, boctet_t* data) :
48 size_(size), data_(data) {
49 }
50
51 /// interpret string as buffer (maximal size 64K)
52 inline explicit buffer_t(const char* string) :
53 size_(0), data_((boctet_t*) string) {
54 while (string[size_] != 0 && size_ < 64000)
55 size_++;
56 }
57
58 /// clone a buffer
59 inline self clone() const {
60 buffer_t n(this->size_);
61 for (bsize_t i = 0; i < size_; i++)
62 n.data_[i] = this->data_[i];
63 return n;
64 }
65
66 /// copy a buffer pointer and size.
67 inline self& operator=(const self& rhs) {
68 this->size_ = rhs.size_;
69 this->data_ = rhs.data_;
70 return *this;
71 }
72
73 /// return sub-buffer.
74 inline self operator()(bsize_t index, bsize_t size = 0) const {
75 self n(*this);
76 n.data_ += index;
77 if (size == 0) n.size_ -= index;
78 else n.size_ = size;
79 return n;
80 }
81
82 /// returns a data element at given index
83 inline boctet_t operator[](bsize_t index) const {
84 return data_[index];
85 }
86
87 /// returns a data element at given index
88 inline boctet_t& operator[](bsize_t index) {
89 return data_[index];
90 }
91
92 /// assigns contents to another buffer
93 inline const self& copy_to( self& buf, bsize_t index = 0 ) const {
94 assert(index + size_ <= buf.size_);
95 const boctet_t *src=data_;
96 boctet_t* dst=buf.data_+index;
97 for (bsize_t i=0; i<size_; i++, src++, dst++ ) *dst = *src;
98 return *this;
99 }
100
101 /// convert to pointer of an immutable type
102 template<class T> inline const T* cast_to() {
103 return (T*)data_;
104 }
105
106 /// set buffer size
107 inline void size( bsize_t size ) {
108 size_ = size;
109 }
110
111
112 /// returns the size in octets
113 inline bsize_t size() const {
114 return size_;
115 }
116
117 /// sets the data pointer
118 inline void data( boctet_t* data ) {
119 data_ = data;
120 }
121
122 /// returns a pointer to immutable data
123 inline const boctet_t * data() const {
124 return data_;
125 }
126
127 /// returns a pointer to mutable data
128 inline boctet_t * mutable_data() {
129 return data_;
130 }
131
132 /// returns a hash value of this buffer using the ELF hash
133 inline size_t hash() const {
134 unsigned h = 0, g;
135 for (size_t i = 0; i < size_; i++) {
136 h = (h << 4) + data_[i];
137 g = h & 0xf0000000L;
138 if (g != 0) h ^= g >> 24;
139 h &= ~g;
140 }
141 return (size_t) h;
142 }
143
144 /// returns true if data_ is null
145 inline bool is_null() const {
146 return data_ == NULL;
147 }
148
149 /// returns true if size is zero
150 inline bool is_empty() const {
151 return size_ == 0;
152 }
153
154 /// compare two buffers
155 inline int compare_to(const self& rhs) const {
156 if (rhs.data_ == data_ && rhs.size_ == size_) return 0;
157 if (size_ < rhs.size_) return -1;
158 if (size_ > rhs.size_) return 1;
159 for (bsize_t i = 0; i < size_; i++) {
160 if (data_[i] > rhs.data_[i]) return 1;
161 if (data_[i] < rhs.data_[i]) return -1;
162 }
163 return 0;
164 }
165
166 /// convenience
167 inline bool operator==(const self& rhs) const {
168 return (compare_to(rhs) == 0);
169 }
170 inline bool operator!=(const self& rhs) const {
171 return (compare_to(rhs) != 0);
172 }
173 inline bool operator<(const self& rhs) const {
174 return (compare_to(rhs) < 0);
175 }
176 inline bool operator<=(const self& rhs) const {
177 return (compare_to(rhs) <= 0);
178 }
179 inline bool operator>(const self& rhs) const {
180 return (compare_to(rhs) > 0);
181 }
182 inline bool operator>=(const self& rhs) const {
183 return (compare_to(rhs) >= 0);
184 }
185};
186
187/// stream operator
188std::ostream& operator<<( std::ostream& os, const buffer_t& buf );
189
190}
191
192/// boost hash
193namespace boost {
194inline size_t hash_value(const reboost::buffer_t& buf) {
195 return buf.hash();
196}}
197
198#endif /* BUFFER_HPP_ */
Note: See TracBrowser for help on using the repository browser.