Marine Library
1.0
C++ library for Linux Networking Development
|
Provide interfaces similar to std::string
for raw byte array.
More...
#include <char_buffer.hh>
Public Types | |
typedef std::char_traits< __Char > | traits_type |
typedef traits_type::char_type | value_type |
typedef value_type & | reference |
typedef const value_type & | const_reference |
typedef value_type * | pointer |
typedef const value_type * | const_pointer |
typedef __gnu_cxx::__normal_iterator< pointer, CCharBuffer > | iterator |
typedef __gnu_cxx::__normal_iterator< const_pointer, CCharBuffer > | const_iterator |
typedef size_t | size_type |
typedef ptrdiff_t | difference_type |
typedef std::reverse_iterator< iterator > | reverse_iterator |
typedef std::reverse_iterator< const_iterator > | const_reverse_iterator |
Public Member Functions | |
CCharBuffer () | |
Construct a null object with zero capacity. | |
CCharBuffer (pointer buf) | |
Construct from a C-style string. More... | |
CCharBuffer (pointer buf, size_type capacity, size_type size=0) | |
Construct from a byte buffer. More... | |
__Myt & | assign (pointer buf, size_type capacity, size_type size=0) |
Set this object to manage a byte buffer. More... | |
__Myt & | assign (const __Myt &other) |
Set this object to manage a buffer from another object. More... | |
void | swap (__Myt &other) throw () |
Swap two CCharBuffer objects. More... | |
Iterators | |
iterator | begin () |
Get the beginning of the data. More... | |
iterator | end () |
Get the ending of the data. More... | |
const_iterator | begin () const |
Get the beginning of the data. More... | |
const_iterator | end () const |
Get the ending of the data. More... | |
reverse_iterator | rbegin () |
Get the ending of the data. More... | |
reverse_iterator | rend () |
Get the beginning of the data. More... | |
const_reverse_iterator | rbegin () const |
Get the ending of the data. More... | |
const_reverse_iterator | rend () const |
Get the beginning of the data. More... | |
Capacity | |
bool | empty () const |
Test if the data is empty. More... | |
size_type | length () const |
Get the size of the data, same as size() . More... | |
size_type | size () const |
Get the size of the data. More... | |
size_type | capacity () const |
Get the capacity of the underlying byte buffer. More... | |
size_type | max_size () const |
Get the maximum number of bytes. More... | |
Element access | |
reference | operator[] (size_type i) |
Access a byte. More... | |
const_reference | operator[] (size_type i) const |
Access a byte. More... | |
reference | front () |
Access the first byte. More... | |
const_reference | front () const |
Access the first byte. More... | |
reference | back () |
Access the last byte. More... | |
const_reference | back () const |
Access the last byte. More... | |
reference | at (size_type i) |
Access a certain byte. More... | |
const_reference | at (size_type i) const |
Access a certain byte. More... | |
const_pointer | c_str () const |
Get the underlying C-style string. More... | |
const_pointer | data () const |
Get the data pointer. More... | |
Operations | |
If any of the following operations makes | |
void | clear () |
Empty the data. More... | |
void | resize (size_type count, value_type val=0) |
Resize the data. More... | |
size_type | copy (pointer buf, size_type count, size_type offset=0) const |
Copy content of a buffer to self. More... | |
void | push_back (value_type val) |
Append a byte to the end of the data. More... | |
__Myt & | append (size_type count, value_type val) |
Append a number of bytes to the end of the data. More... | |
__Myt & | append (const_pointer buf, size_type count) |
Append content of a buffer to the end of the data. More... | |
__Myt & | append (const_pointer buf) |
Append a C-style string to the end of the data. More... | |
__Myt & | append (const __Myt &other, size_type offset, size_type count) |
Append content of another CCharBuffer content to the end of the data. More... | |
__Myt & | append (const __Myt &other) |
Append content of another CCharBuffer content to the end of the data. More... | |
__Myt & | operator+= (const __Myt &other) |
Append content of another CCharBuffer to the end of the data. More... | |
__Myt & | operator+= (const_pointer buf) |
Append a C-style string to the end of the data. More... | |
__Myt & | operator+= (value_type val) |
Append a byte to the end of the data. More... | |
__Myt & | insert (size_type offset, size_type count, value_type val) |
Insert a number of bytes. More... | |
__Myt & | insert (size_type offset, const_pointer buf, size_type count) |
Insert the content of a buffer. More... | |
__Myt & | insert (size_type offset, const_pointer buf) |
Insert a C-style string. More... | |
__Myt & | insert (size_type offset, const __Myt &other, size_type newOffset, size_type count) |
Insert the content of another CCharBuffer. More... | |
__Myt & | insert (size_type offset, const __Myt &other) |
Insert the content of another CCharBuffer. More... | |
iterator | insert (iterator pos, value_type val) |
Insert a byte. More... | |
__Myt & | replace (size_type offset, size_type count, size_type newCount, value_type val) |
Replace a range of bytes with a number of new bytes. More... | |
__Myt & | replace (size_type offset, size_type count, const_pointer buf, size_type newCount) |
Replace a range of bytes with the content of a byte buffer. More... | |
__Myt & | replace (size_type offset, size_type count, const_pointer buf) |
Replace a range of bytes with the content of a C-style string. More... | |
__Myt & | replace (size_type offset, size_type count, const __Myt &other, size_type newOffset, size_type newCount) |
Replace a range of bytes with the content of another CCharBuffer. More... | |
__Myt & | replace (size_type offset, size_type count, const __Myt &other) |
Replace a range of bytes with the content of another CCharBuffer. More... | |
__Myt & | replace (iterator first, iterator last, size_type count, value_type val) |
Replace a range of bytes with a number of new bytes. More... | |
__Myt & | replace (iterator first, iterator last, const_pointer buf, size_type count) |
Replace a range of bytes with the content of a byte buffer. More... | |
__Myt & | replace (iterator first, iterator last, const_pointer buf) |
Replace a range of bytes with the content of a C-style string. More... | |
__Myt & | replace (iterator first, iterator last, const __Myt &other, size_type offset, size_type count) |
Replace a range of bytes with the content of another CCharBuffer. More... | |
__Myt & | replace (iterator first, iterator last, const __Myt &other) |
Replace a range of bytes with the content of another CCharBuffer. More... | |
iterator | erase (iterator first, iterator last) |
Erase a range of bytes. More... | |
iterator | erase (iterator pos) |
Erase a byte. More... | |
__Myt & | erase (size_type offset=0, size_type count=npos) |
Erase a range of bytes. More... | |
__Myt | substr (size_type offset=0, size_type count=npos) |
Get a substring. More... | |
int | compare (size_type offset, size_type count, const_pointer buf, size_type newCount) const |
Compare a range of bytes with a buffer lexicographically. More... | |
int | compare (size_type offset, size_type count, const_pointer buf) const |
Compare a range of bytes with a C-style string lexicographically. More... | |
int | compare (const_pointer buf) const |
Compare self with a C-style string lexicographically. More... | |
int | compare (size_type offset, size_type count, const __Myt &other, size_type newOffset, size_type newCount) const |
Compare a range of bytes with another CCharBuffer object. More... | |
int | compare (size_type offset, size_type count, const __Myt &other) const |
Compare a range of bytes with another CCharBuffer object. More... | |
int | compare (const __Myt &other) const |
Compare self with another CCharBuffer object. More... | |
Static Public Attributes | |
static const size_type | npos = static_cast<size_type>(-1) |
Provide interfaces similar to std::string
for raw byte array.
CCharBuffer provides convenient interfaces, like begin/end
, append
, insert
, to manipulate a byte array, and performs necessary boundary checks to prevent access violation. It does NOT manage memory, so the user is responsible for the validation of the underlying buffer.
CharT | A character type, must be char , unsigned char or signed char . Other wider sized types are NOT supported, e.g. wchar_t . |
|
inline |
Construct from a C-style string.
Both size and capacity are set to strlen(buf)
.
buf | Pointer to a string |
|
inline |
Construct from a byte buffer.
buf | Pointer to a byte buffer. |
capacity | Size of bytes that this object can manipulate. It could be smaller than the allocated size of buf , but not greater. |
size | Size of used bytes. It should be no more that capacity , otherwise capacity will be used instead. |
|
inline |
Append a number of bytes to the end of the data.
count | Number of bytes to append. |
val | Value of bytes to append. |
|
inline |
Append content of a buffer to the end of the data.
buf | Pointer to a byte buffer. |
count | Size of the buff. |
|
inline |
Append a C-style string to the end of the data.
buf | Pointer to a string. |
|
inline |
Append content of another CCharBuffer content to the end of the data.
This function appends contents between other.begin() + offset
and other.begin() + offset + count
to the data.
other | Another CCharBuffer object. |
offset | Offset of the content in other . |
count | Size of the content in other . |
|
inline |
Append content of another CCharBuffer content to the end of the data.
other | Another CCharBuffer object. |
|
inline |
Set this object to manage a byte buffer.
buf | Pointer to a byte buffer |
capacity | Size of bytes that this object can manipulate. It could be different from the real allocated size of buf . |
size | Size of used bytes. It should be no more that capacity , otherwise capacity will be used instead. |
|
inline |
Set this object to manage a buffer from another object.
These two objects will manipulate the same byte buffer, so if one changes the content of the buffer, the other will reflect the change. But they can have different size
, for example, one can resize
to a new size, the other object's size
won't get affected.
other | Another CCharBuffer object. |
|
inline |
Access a certain byte.
i | Index of the byte |
i
exceed size
, an exception will be thrown.
|
inline |
Access a certain byte.
i | Index of the byte |
i
exceed size
, an exception will be thrown.
|
inline |
Access the last byte.
|
inline |
Access the last byte.
|
inline |
Get the beginning of the data.
|
inline |
Get the beginning of the data.
|
inline |
|
inline |
Get the capacity of the underlying byte buffer.
|
inline |
Empty the data.
This function will NOT change the content of the underlying byte buffer, only reset size
of this object.
|
inline |
Compare a range of bytes with a buffer lexicographically.
This function compares content between this->begin() + offset
and this->begin() + offset + count
, with content between buf
and buf + count
.
offset | Start position in self to compare. |
count | Size of bytes in self to compare. |
buf | Pointer to a byte buffer to compare with. |
newCount | Length of buf to compare with. |
0
if two contents are equal;
|
inline |
Compare a range of bytes with a C-style string lexicographically.
This function compares content between this->begin() + offset
and this->begin() + offset + count
, with content of buf
.
offset | Start position in self to compare. |
count | Size of bytes in self to compare. |
buf | Pointer to a string. |
0
if two contents are equal;
|
inline |
Compare self with a C-style string lexicographically.
This function compares content between this->begin()
and this->end()
, with content of buf
.
buf | Pointer to a string. |
0
if two contents are equal;
|
inline |
Compare a range of bytes with another CCharBuffer object.
This function compares content between this->begin() + offset
and this->begin() + offset + count
, with content between other.begin() + newOffset
and other.begin() + newOffset + newCount
.
offset | Start position in self to compare. |
count | Size of bytes in self to compare. |
other | Another CCharBuffer object to compare with. |
newOffset | Start position in other to compare with. |
newCount | Size of bytes in other to compare with. |
0
if two contents are equal;
|
inline |
Compare a range of bytes with another CCharBuffer object.
This function compares content between this->begin() + offset
and this->begin() + offset + count
, with content between other.begin()
and other.end()
.
offset | Start position in self to compare. |
count | Size of bytes in self to compare. |
other | Another CCharBuffer object to compare with. |
0
if two contents are equal;
|
inline |
Compare self with another CCharBuffer object.
This function compares content between this->begin()
and this->end()
, with content between other.begin()
and other.end()
.
other | Another CCharBuffer object to compare with. |
0
if two contents are equal;
|
inline |
Copy content of a buffer to self.
This function tries to copy at most count
bytes from buf
, to the buffer managed by this object, starting from offset
, which is similar to:
except that if self cannot hold as many as count
bytes from offset
, it will copy less bytes, which will be indicated in the return value.
buf | Pointer to a byte buffer. |
count | Size of bytes to copy from buf . |
offset | Offset of destination in self. |
|
inline |
|
inline |
Test if the data is empty.
true
if size
is 0; otherwise false
.
|
inline |
Get the ending of the data.
|
inline |
Get the ending of the data.
|
inline |
Erase a range of bytes.
first | Start position to be erased. |
last | End position to be erased. |
first
.
|
inline |
Erase a byte.
pos | Position of byte to be erased. |
pos
.
|
inline |
Erase a range of bytes.
offset | Offset of start position to be erased. |
count | Size of bytes to be erased. |
|
inline |
Access the first byte.
|
inline |
Access the first byte.
|
inline |
Insert a number of bytes.
offset | Position to insert into. |
count | Number of bytes to insert. |
val | Value of bytes to insert. |
|
inline |
Insert the content of a buffer.
offset | Position to insert into. |
buf | Pointer to a byte buffer. |
count | Length of buf . |
|
inline |
Insert a C-style string.
The trailing '\0'
is NOT included.
offset | Position to insert into. |
buf | Pointer to a string. |
|
inline |
Insert the content of another CCharBuffer.
This function reads contents from other.begin() + newOffset
to other.begin() + newOffset + count
, and insert it to this->begin() + offset
.
offset | Position to insert into. |
other | Another CCharBuffer object. |
newOffset | Offset of the content in other . |
count | Size of the content in other . |
|
inline |
Insert the content of another CCharBuffer.
offset | Position to insert into. |
other | Another CCharBuffer object. |
|
inline |
Insert a byte.
pos | Position to insert the byte. |
val | Value of the byte. |
pos
.
|
inline |
|
inline |
Get the maximum number of bytes.
|
inline |
Append content of another CCharBuffer to the end of the data.
other | Another CCharBuffer object. |
|
inline |
Append a C-style string to the end of the data.
The trailing '\0'
is NOT included.
buf | Pointer to a string. |
|
inline |
Append a byte to the end of the data.
val | Value to append to the data. |
|
inline |
Access a byte.
i | Index of the byte. |
|
inline |
Access a byte.
i | Index of the byte. |
|
inline |
Append a byte to the end of the data.
val | Value to append to the data. |
|
inline |
Get the ending of the data.
|
inline |
Get the ending of the data.
|
inline |
Get the beginning of the data.
|
inline |
Get the beginning of the data.
|
inline |
Replace a range of bytes with a number of new bytes.
This function performs actions similar to the following:
begin() + offset
to begin() + offset + count
; begin() + offset
;The size of the data may change if count
is not equal to newCount
.
offset | Start position of bytes to be replaced. |
count | Size of bytes to be replaced. |
newCount | Number of new bytes. |
val | Value of new bytes. |
|
inline |
Replace a range of bytes with the content of a byte buffer.
This function performs actions similar to the following:
begin() + offset
to begin() + offset + count
; begin() + offset
;The size of the data may change if count
is not equal to newCount
.
offset | Start position of bytes to be replaced. |
count | Size of bytes to be replaced. |
buf | Pointer to a byte buffer. |
newCount | Length of buf . |
|
inline |
Replace a range of bytes with the content of a C-style string.
This function performs actions similar to the following:
begin() + offset
to begin() + offset + count
; begin() + offset
;The size of the data may change if count
is not equal to the length of buf
.
The trailing '\0'
of buf
is not included.
offset | Start position of bytes to be replaced. |
count | Size of bytes to be replaced. |
buf | Pointer to a string |
|
inline |
Replace a range of bytes with the content of another CCharBuffer.
This function performs actions similar to the following:
begin() + offset
to begin() + offset + count
; begin() + offset
;The size of the data may change if count
is not equal to newCount
.
offset | Start position of bytes to be replaced. |
count | Size of bytes to be replaced. |
other | Another CCharBuffer object. |
newOffset | Offset of content in other . |
newCount | Size of content in other . |
|
inline |
Replace a range of bytes with the content of another CCharBuffer.
This function performs actions similar to the following:
begin() + offset
to begin() + offset + count
; begin() + offset
;The size of the data may change if count
is not equal to other.size()
.
offset | Start position of bytes to be replaced. |
count | Size of bytes to be replaced. |
other | Another CCharBuffer object. |
|
inline |
Replace a range of bytes with a number of new bytes.
This function performs actions similar to the following:
first
to last
; first
;The size of the data may change if last - first
is not equal to count
.
first | Start position to be replaced. |
last | End position to be replaced. |
count | Number of new bytes. |
val | Value of new bytes. |
|
inline |
Replace a range of bytes with the content of a byte buffer.
This function performs actions similar to the following:
first
to last
; first
;The size of the data may change if last - first
is not equal to count
.
first | Start position to be replaced. |
last | End position to be replaced. |
buf | Pointer to a byte buffer. |
newCount | Length of buf . |
|
inline |
Replace a range of bytes with the content of a C-style string.
This function performs actions similar to the following:
first
to last
; first
;The size of the data may change if last - first
is not equal to the length of buf
.
The trailing '\0'
of buf
is not included.
first | Start position to be replaced. |
last | End position to be replaced. |
buf | Pointer to a string |
|
inline |
Replace a range of bytes with the content of another CCharBuffer.
This function performs actions similar to the following:
first
to last
; first
;The size of the data may change if last - first
is not equal to count
.
first | Start position to be replaced. |
last | End position to be replaced. |
other | Another CCharBuffer object. |
offset | Offset of content in other . |
count | Size of content in other . |
|
inline |
Replace a range of bytes with the content of another CCharBuffer.
This function performs actions similar to the following:
first
to last
; first
;The size of the data may change if last - first
is not equal to other.size()
.
first | Start position to be replaced. |
last | End position to be replaced. |
other | Another CCharBuffer object. |
|
inline |
Resize the data.
If data shrinks, no change will be made to the content of the underlying buffer. If data expands, new data will be filled with val
.
count | New size of the data |
val | Value to fill in the expanded data |
|
inline |
|
inline |
Get a substring.
The substring has the following attributes:
begin()
is equal to this->begin() + offset
; size()
is equal to the smaller one of count
and this->size() - offset
; capacity()
is equal to this->capacity() - offset
; offset | Offset of start position of the substring. |
count | Length of the substring. |
|
inline |
Swap two CCharBuffer objects.
other | Reference to another object. |