Otclient
14/8/2020
databuffer.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a copy
5
* of this software and associated documentation files (the "Software"), to deal
6
* in the Software without restriction, including without limitation the rights
7
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
* copies of the Software, and to permit persons to whom the Software is
9
* furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice shall be included in
12
* all copies or substantial portions of the Software.
13
*
14
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
* THE SOFTWARE.
21
*/
22
23
#ifndef DATABUFFER_H
24
#define DATABUFFER_H
25
26
template
<
class
T>
27
class
DataBuffer
28
{
29
public
:
30
DataBuffer
(
uint
res = 64) :
31
m_size(0),
32
m_capacity(res),
33
m_buffer(new T[m_capacity]) { }
34
~DataBuffer
() {
35
delete
[] m_buffer;
36
}
37
38
inline
void
reset
() { m_size = 0; }
39
inline
void
clear
() {
40
m_size = 0;
41
m_capacity = 0;
42
delete
[] m_buffer;
43
m_buffer =
nullptr
;
44
}
45
46
inline
bool
empty
()
const
{
return
m_size == 0; }
47
inline
uint
size
()
const
{
return
m_size; }
48
inline
T *
data
()
const
{
return
m_buffer; }
49
50
inline
const
T&
at
(
uint
i)
const
{
return
m_buffer[i]; }
51
inline
const
T&
last
()
const
{
return
m_buffer[m_size-1]; }
52
inline
const
T&
first
()
const
{
return
m_buffer[0]; }
53
inline
const
T&
operator[]
(
uint
i)
const
{
return
m_buffer[i]; }
54
inline
T&
operator[]
(
uint
i) {
return
m_buffer[i]; }
55
56
inline
void
reserve
(
uint
n) {
57
if
(n > m_capacity) {
58
T *buffer =
new
T[n];
59
for
(
uint
i=0;i<m_size;++i)
60
buffer[i] = m_buffer[i];
61
62
delete
[] m_buffer;
63
m_buffer = buffer;
64
m_capacity = n;
65
}
66
}
67
68
inline
void
resize
(
uint
n, T def = T()) {
69
if
(n == m_size)
70
return
;
71
reserve
(n);
72
for
(
uint
i=m_size;i<n;++i)
73
m_buffer[i] = def;
74
m_size = n;
75
}
76
77
inline
void
grow
(
uint
n) {
78
if
(n <= m_size)
79
return
;
80
if
(n > m_capacity) {
81
uint
newcapacity = m_capacity;
82
do
{ newcapacity *= 2; }
while
(newcapacity < n);
83
reserve
(newcapacity);
84
}
85
m_size = n;
86
}
87
88
inline
void
add
(
const
T& v) {
89
grow
(m_size + 1);
90
m_buffer[m_size-1] = v;
91
}
92
93
inline
DataBuffer
&
operator<<
(
const
T &t) {
add
(t);
return
*
this
; }
94
95
private
:
96
uint
m_size;
97
uint
m_capacity;
98
T *m_buffer;
99
};
100
101
#endif
DataBuffer::clear
void clear()
Definition:
databuffer.h:39
DataBuffer::DataBuffer
DataBuffer(uint res=64)
Definition:
databuffer.h:30
DataBuffer
Definition:
databuffer.h:27
DataBuffer::reserve
void reserve(uint n)
Definition:
databuffer.h:56
DataBuffer::size
uint size() const
Definition:
databuffer.h:47
DataBuffer::empty
bool empty() const
Definition:
databuffer.h:46
DataBuffer::add
void add(const T &v)
Definition:
databuffer.h:88
DataBuffer::last
const T & last() const
Definition:
databuffer.h:51
DataBuffer::reset
void reset()
Definition:
databuffer.h:38
DataBuffer::resize
void resize(uint n, T def=T())
Definition:
databuffer.h:68
uint
unsigned int uint
Definition:
types.h:31
DataBuffer::at
const T & at(uint i) const
Definition:
databuffer.h:50
DataBuffer::data
T * data() const
Definition:
databuffer.h:48
DataBuffer::first
const T & first() const
Definition:
databuffer.h:52
DataBuffer::operator<<
DataBuffer & operator<<(const T &t)
Definition:
databuffer.h:93
DataBuffer::~DataBuffer
~DataBuffer()
Definition:
databuffer.h:34
DataBuffer::operator[]
const T & operator[](uint i) const
Definition:
databuffer.h:53
DataBuffer::operator[]
T & operator[](uint i)
Definition:
databuffer.h:54
DataBuffer::grow
void grow(uint n)
Definition:
databuffer.h:77
src
framework
util
databuffer.h
Generated by
1.8.17
For more information visit
https://forum.opentibia.dev/