MessagePack for C++
Toggle main menu visibility
Loading...
Searching...
No Matches
msgpack
v1
sbuffer.hpp
Go to the documentation of this file.
1
//
2
// MessagePack for C++ simple buffer implementation
3
//
4
// Copyright (C) 2008-2016 FURUHASHI Sadayuki and KONDO Takatoshi
5
//
6
// Distributed under the Boost Software License, Version 1.0.
7
// (See accompanying file LICENSE_1_0.txt or copy at
8
// http://www.boost.org/LICENSE_1_0.txt)
9
//
10
#ifndef MSGPACK_V1_SBUFFER_HPP
11
#define MSGPACK_V1_SBUFFER_HPP
12
13
#include "
msgpack/v1/cpp_config_decl.hpp
"
14
#include "
msgpack/v1/sbuffer_decl.hpp
"
15
#include "
msgpack/assert.hpp
"
16
17
#include <stdexcept>
18
#include <cstring>
19
20
namespace
msgpack
{
21
23
MSGPACK_API_VERSION_NAMESPACE
(v1) {
25
26
class
sbuffer
{
27
public
:
28
sbuffer
(
size_t
initsz =
MSGPACK_SBUFFER_INIT_SIZE
):m_size(0), m_alloc(initsz)
29
{
30
if
(initsz == 0) {
31
m_data =
MSGPACK_NULLPTR
;
32
}
else
{
33
m_data = (
char
*)::malloc(initsz);
34
if
(!m_data) {
35
throw
std::bad_alloc();
36
}
37
}
38
}
39
40
~sbuffer
()
41
{
42
::free(m_data);
43
}
44
45
#if !defined(MSGPACK_USE_CPP03)
46
sbuffer
(
const
sbuffer
&) =
delete
;
47
sbuffer
&
operator=
(
const
sbuffer
&) =
delete
;
48
49
sbuffer
(
sbuffer
&& other) :
50
m_size(other.m_size), m_data(other.m_data), m_alloc(other.m_alloc)
51
{
52
other.m_size = other.m_alloc = 0;
53
other.m_data =
MSGPACK_NULLPTR
;
54
}
55
56
sbuffer
&
operator=
(
sbuffer
&& other)
57
{
58
::free(m_data);
59
60
m_size = other.m_size;
61
m_alloc = other.m_alloc;
62
m_data = other.m_data;
63
64
other.m_size = other.m_alloc = 0;
65
other.m_data =
MSGPACK_NULLPTR
;
66
67
return
*
this
;
68
}
69
#endif
// !defined(MSGPACK_USE_CPP03)
70
71
void
write
(
const
char
* buf,
size_t
len)
72
{
73
MSGPACK_ASSERT
(buf || len == 0);
74
75
if
(!buf)
return
;
76
77
if
(m_alloc - m_size < len) {
78
expand_buffer(len);
79
}
80
std::memcpy(m_data + m_size, buf, len);
81
m_size += len;
82
}
83
84
char
*
data
()
85
{
86
return
m_data;
87
}
88
89
const
char
*
data
()
const
90
{
91
return
m_data;
92
}
93
94
size_t
size
()
const
95
{
96
return
m_size;
97
}
98
99
char
*
release
()
100
{
101
char
* tmp = m_data;
102
m_size = 0;
103
m_data =
MSGPACK_NULLPTR
;
104
m_alloc = 0;
105
return
tmp;
106
}
107
108
void
clear
()
109
{
110
m_size = 0;
111
}
112
113
private
:
114
void
expand_buffer(
size_t
len)
115
{
116
size_t
nsize = (m_alloc > 0) ?
117
m_alloc * 2 :
MSGPACK_SBUFFER_INIT_SIZE
;
118
119
while
(nsize < m_size + len) {
120
size_t
tmp_nsize = nsize * 2;
121
if
(tmp_nsize <= nsize) {
122
nsize = m_size + len;
123
break
;
124
}
125
nsize = tmp_nsize;
126
}
127
128
void
* tmp = ::realloc(m_data, nsize);
129
if
(!tmp) {
130
throw
std::bad_alloc();
131
}
132
133
m_data =
static_cast<
char
*
>
(tmp);
134
m_alloc = nsize;
135
}
136
137
#if defined(MSGPACK_USE_CPP03)
138
private
:
139
sbuffer
(
const
sbuffer
&);
140
sbuffer
&
operator=
(
const
sbuffer
&);
141
#endif
// defined(MSGPACK_USE_CPP03)
142
143
private
:
144
size_t
m_size;
145
char
* m_data;
146
size_t
m_alloc;
147
};
148
150
}
// MSGPACK_API_VERSION_NAMESPACE(v1)
152
153
}
// namespace msgpack
154
155
#endif
// MSGPACK_V1_SBUFFER_HPP
assert.hpp
MSGPACK_ASSERT
#define MSGPACK_ASSERT
Definition
assert.hpp:22
msgpack::sbuffer::sbuffer
sbuffer(sbuffer &&other)
Definition
sbuffer.hpp:49
msgpack::sbuffer::sbuffer
sbuffer(const sbuffer &)=delete
msgpack::sbuffer::data
const char * data() const
Definition
sbuffer.hpp:89
msgpack::sbuffer::data
char * data()
Definition
sbuffer.hpp:84
msgpack::sbuffer::operator=
sbuffer & operator=(const sbuffer &)=delete
msgpack::sbuffer::release
char * release()
Definition
sbuffer.hpp:99
msgpack::sbuffer::sbuffer
sbuffer(size_t initsz=MSGPACK_SBUFFER_INIT_SIZE)
Definition
sbuffer.hpp:28
msgpack::sbuffer::~sbuffer
~sbuffer()
Definition
sbuffer.hpp:40
msgpack::sbuffer::clear
void clear()
Definition
sbuffer.hpp:108
msgpack::sbuffer::operator=
sbuffer & operator=(sbuffer &&other)
Definition
sbuffer.hpp:56
msgpack::sbuffer::size
size_t size() const
Definition
sbuffer.hpp:94
msgpack::sbuffer::write
void write(const char *buf, size_t len)
Definition
sbuffer.hpp:71
msgpack
Definition
adaptor_base.hpp:15
cpp_config_decl.hpp
MSGPACK_NULLPTR
#define MSGPACK_NULLPTR
Definition
cpp_config_decl.hpp:85
sbuffer_decl.hpp
MSGPACK_SBUFFER_INIT_SIZE
#define MSGPACK_SBUFFER_INIT_SIZE
Definition
sbuffer_decl.hpp:16
MSGPACK_API_VERSION_NAMESPACE
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition
versioning.hpp:66
Generated by
1.17.0