cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
Loading...
Searching...
No Matches
serialized_message.cpp
Go to the documentation of this file.
1
/*
2
* Copyright 2023 MusicScience37 (Kenta Kabashima)
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
20
#include "
msgpack_rpc/messages/serialized_message.h
"
21
22
#include <cstddef>
23
#include <cstring>
24
#include <utility>
25
26
#include "
msgpack_rpc/messages/impl/sharable_binary_header.h
"
27
28
namespace
msgpack_rpc::messages
{
29
30
SerializedMessage::SerializedMessage
(
const
char
*
data
, std::size_t
size
)
31
:
buffer_
(
impl
::allocate_sharable_binary(
size
)) {
32
// Following operations won't throw exceptions.
33
impl::enable_reference_count_of_sharable_binary
(
buffer_
);
34
std::memcpy(
impl::binary_buffer_of
(
buffer_
),
data
,
size
);
35
}
36
37
SerializedMessage::SerializedMessage
(
impl::SharableBinaryHeader
* buffer)
38
:
buffer_
(buffer) {}
39
40
SerializedMessage::SerializedMessage
(
const
SerializedMessage
& other) noexcept
41
:
buffer_
(other.buffer_) {
42
impl::add_reference_count_of_sharable_binary
(
buffer_
);
43
}
44
45
SerializedMessage::SerializedMessage
(
SerializedMessage
&& other) noexcept
46
:
buffer_
(std::exchange(other.buffer_,
nullptr
)) {}
47
48
SerializedMessage
&
SerializedMessage::operator=
(
49
const
SerializedMessage
& other)
noexcept
{
50
if
(
this
== &other) {
51
return
*
this
;
52
}
53
if
(
buffer_
!=
nullptr
) {
54
impl::decrease_reference_count_of_sharable_binary
(
buffer_
);
55
}
56
buffer_
= other.buffer_;
57
impl::add_reference_count_of_sharable_binary
(
buffer_
);
58
return
*
this
;
59
}
60
61
SerializedMessage
&
SerializedMessage::operator=
(
62
SerializedMessage
&& other)
noexcept
{
63
std::swap(
buffer_
, other.buffer_);
64
return
*
this
;
65
}
66
67
SerializedMessage::~SerializedMessage
() noexcept {
68
if
(
buffer_
!=
nullptr
) {
69
impl::decrease_reference_count_of_sharable_binary
(
buffer_
);
70
}
71
}
72
73
const
char
*
SerializedMessage::data
() const noexcept {
74
return
impl::binary_buffer_of
(
buffer_
);
75
}
76
77
std::size_t
SerializedMessage::size
() const noexcept {
78
return
buffer_
->
binary_size
;
79
}
80
81
}
// namespace msgpack_rpc::messages
msgpack_rpc::messages::SerializedMessage::buffer_
impl::SharableBinaryHeader * buffer_
Buffer.
Definition
serialized_message.h:104
msgpack_rpc::messages::SerializedMessage::operator=
SerializedMessage & operator=(const SerializedMessage &other) noexcept
Copy assignment operator.
Definition
serialized_message.cpp:48
msgpack_rpc::messages::SerializedMessage::data
const char * data() const noexcept
Get the pointer to the data.
Definition
serialized_message.cpp:73
msgpack_rpc::messages::SerializedMessage::SerializedMessage
SerializedMessage(const char *data, std::size_t size)
Constructor.
Definition
serialized_message.cpp:30
msgpack_rpc::messages::SerializedMessage::size
std::size_t size() const noexcept
Get the size of the data.
Definition
serialized_message.cpp:77
msgpack_rpc::messages::SerializedMessage::~SerializedMessage
~SerializedMessage() noexcept
Destructor.
Definition
serialized_message.cpp:67
msgpack_rpc::messages::impl
Namespace of internal implementations.
Definition
serialization_buffer.h:28
msgpack_rpc::messages::impl::decrease_reference_count_of_sharable_binary
void decrease_reference_count_of_sharable_binary(SharableBinaryHeader *buffer) noexcept
Decrease a reference count of a buffer of sharable binary data, and deallocate the buffer if possible...
Definition
sharable_binary_header.h:212
msgpack_rpc::messages::impl::binary_buffer_of
char * binary_buffer_of(SharableBinaryHeader *buffer) noexcept
Get buffer of binary data from a buffer of sharable binary data.
Definition
sharable_binary_header.h:227
msgpack_rpc::messages::impl::add_reference_count_of_sharable_binary
void add_reference_count_of_sharable_binary(SharableBinaryHeader *buffer) noexcept
Add a reference count of a buffer of sharable binary data.
Definition
sharable_binary_header.h:201
msgpack_rpc::messages::impl::enable_reference_count_of_sharable_binary
void enable_reference_count_of_sharable_binary(SharableBinaryHeader *buffer) noexcept
Enable reference count of a buffer of sharable binary data.
Definition
sharable_binary_header.h:188
msgpack_rpc::messages
Namespace of messages.
Definition
buffer_view.h:24
serialized_message.h
Definition of SerializedMessage class.
sharable_binary_header.h
Definition of SharableBinaryHeader struct.
msgpack_rpc::messages::impl::SharableBinaryHeader
Struct of headers of sharable binary data.
Definition
sharable_binary_header.h:36
msgpack_rpc::messages::impl::SharableBinaryHeader::binary_size
std::size_t binary_size
Size of the binary data.
Definition
sharable_binary_header.h:42
src
msgpack_rpc
messages
serialized_message.cpp
Generated on
for cpp-msgpack-rpc by
1.18.0