cpp-msgpack-light 0.3.0
A light library to serialize MessagePack.
Loading...
Searching...
No Matches
object_base.h
Go to the documentation of this file.
1/*
2 * Copyright 2024 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#pragma once
21
22#include <cstdint>
23#include <cstring>
24#include <stdexcept>
25#include <string_view>
26
34#include "msgpack_light/details/object_ref_decl.h" // IWYU pragma: keep
36
37namespace msgpack_light::details {
38
47template <typename Derived>
49public:
54
60 [[nodiscard]] object_data_type type() const noexcept { return data().type; }
61
67 [[nodiscard]] std::uint64_t as_unsigned_integer() const {
69 throw std::runtime_error("This object is not an unsigned integer.");
70 }
72 }
73
79 [[nodiscard]] std::int64_t as_signed_integer() const {
81 throw std::runtime_error("This object is not a signed integer.");
82 }
84 }
85
91 [[nodiscard]] bool as_boolean() const {
93 throw std::runtime_error("This object is not a boolean.");
94 }
95 return data().data.bool_value;
96 }
97
103 [[nodiscard]] float as_float32() const {
105 throw std::runtime_error(
106 "This object is not a 32-bit floating-point number.");
107 }
108 return data().data.float_value;
109 }
110
116 [[nodiscard]] double as_float64() const {
118 throw std::runtime_error(
119 "This object is not a 64-bit floating-point number.");
120 }
121 return data().data.double_value;
122 }
123
129 [[nodiscard]] std::string_view as_string() const {
131 throw std::runtime_error("This object is not a string.");
132 }
133 return std::string_view(
134 data().data.string_value.data, data().data.string_value.size);
135 }
136
142 [[nodiscard]] binary_view as_binary() const {
144 throw std::runtime_error("This object is not a binary.");
145 }
146 return binary_view(
147 data().data.binary_value.data, data().data.binary_value.size);
148 }
149
155 [[nodiscard]] const_array_ref as_array() const {
157 throw std::runtime_error("This object is not an array.");
158 }
160 }
161
167 [[nodiscard]] const_map_ref as_map() const {
169 throw std::runtime_error("This object is not a map.");
170 }
172 }
173
179 [[nodiscard]] const_extension_ref as_extension() const {
181 throw std::runtime_error("This object is not an extension.");
182 }
184 }
185
187
194
200 [[nodiscard]] const details::object_data& data() const noexcept {
201 return derived().data();
202 }
203
205
206protected:
212 [[nodiscard]] const Derived& derived() const noexcept {
213 return *static_cast<const Derived*>(this);
214 }
215
217 const_object_base() = default;
218};
219
229template <typename Derived, typename Allocator>
230class mutable_object_base : public const_object_base<Derived> {
231public:
233 using allocator_type = Allocator;
234
239
245 void set_unsigned_integer(std::uint64_t value) noexcept {
246 clear();
249 }
250
256 void set_signed_integer(std::int64_t value) noexcept {
257 clear();
260 }
261
267 void set_boolean(bool value) noexcept {
268 clear();
269 data().data.bool_value = value;
271 }
272
278 void set_float32(float value) noexcept {
279 clear();
280 data().data.float_value = value;
282 }
283
289 void set_float64(double value) noexcept {
290 clear();
291 data().data.double_value = value;
293 }
294
300 void set_string(std::string_view value) {
301 auto* ptr = allocator().allocate_char(value.size());
302 std::memcpy(ptr, value.data(), value.size());
303 clear();
304 data().data.string_value.data = ptr;
305 data().data.string_value.size = value.size();
307 }
308
315 auto* ptr = allocator().allocate_unsigned_char(value.size());
316 std::memcpy(ptr, value.data(), value.size());
317 clear();
318 data().data.binary_value.data = ptr;
319 data().data.binary_value.size = value.size();
321 }
322
330 clear();
331 data().data.array_value.data = allocator().allocate_object_data(size);
332 data().data.array_value.size = size;
333 std::memset(
334 data().data.array_value.data, 0, size * sizeof(object_data));
337 data().data.array_value, allocator());
338 }
339
346 mutable_map_ref<Allocator> set_map(std::size_t size = 0U) {
347 clear();
349 allocator().allocate_key_value_pair_data(size);
350 data().data.map_value.size = size;
351 std::memset(
352 data().data.map_value.data, 0, size * sizeof(key_value_pair_data));
354 return mutable_map_ref<Allocator>(data().data.map_value, allocator());
355 }
356
363 void set_extension(std::int8_t type, binary_view value_data) {
364 auto* ptr = allocator().allocate_unsigned_char(value_data.size());
365 std::memcpy(ptr, value_data.data(), value_data.size());
366 clear();
369 data().data.extension_value.size = value_data.size();
371 }
372
376 void clear() noexcept { clear_object_data(data(), allocator()); }
377
379
380 using const_object_base<Derived>::as_array;
381 using const_object_base<Derived>::as_map;
382
387
395 throw std::runtime_error("This object is not an array.");
396 }
398 data().data.array_value, allocator());
399 }
400
408 throw std::runtime_error("This object is not a map.");
409 }
410 return mutable_map_ref<Allocator>(data().data.map_value, allocator());
411 }
412
414
421
427 [[nodiscard]] details::object_data& data() noexcept {
428 return derived().data();
429 }
430
436 [[nodiscard]] const details::object_data& data() const noexcept {
437 return derived().data();
438 }
439
445 [[nodiscard]] allocator_wrapper<Allocator>& allocator() noexcept {
446 return derived().allocator();
447 }
448
450
451protected:
454
460 [[nodiscard]] Derived& derived() noexcept {
461 return *static_cast<Derived*>(this);
462 }
463
469 [[nodiscard]] const Derived& derived() const noexcept {
470 return *static_cast<const Derived*>(this);
471 }
472};
473
474} // namespace msgpack_light::details
Definition of allocator_wrapper class.
Definition of classes of references to arrays.
Definition of binary class.
Class to refer binary data.
Definition binary.h:78
std::size_t size() const noexcept
Get the size of the data.
Definition binary.h:116
const unsigned char * data() const noexcept
Get the pointer to the data.
Definition binary.h:109
Class to access constant arrays.
Definition array_ref.h:45
Class to access constant extension value.
Class to access constant maps.
Definition map_ref.h:43
Base class of constant objects in MessagePack.
Definition object_base.h:48
const details::object_data & data() const noexcept
Get the internal data.
const_array_ref as_array() const
Get data as an array.
bool as_boolean() const
Get data as a boolean.
Definition object_base.h:91
const Derived & derived() const noexcept
Access the derived class.
std::int64_t as_signed_integer() const
Get data as a signed integer.
Definition object_base.h:79
const_map_ref as_map() const
Get data as a map.
const_extension_ref as_extension() const
Get data as an extension.
std::string_view as_string() const
Get data as a string.
float as_float32() const
Get data as a 32-bit floating-pointe number.
binary_view as_binary() const
Get data as a binary.
std::uint64_t as_unsigned_integer() const
Get data as an unsigned integer.
Definition object_base.h:67
object_data_type type() const noexcept
Get the type of this object.
Definition object_base.h:60
double as_float64() const
Get data as a 64-bit floating-pointe number.
Allocator allocator_type
Type of the allocator.
void set_unsigned_integer(std::uint64_t value) noexcept
Set this object to an unsigned integer.
void set_float32(float value) noexcept
Set this object to a 32-bit floating-point number.
void set_string(std::string_view value)
Set this object to a string.
mutable_array_ref< Allocator > as_array()
Get data as an array.
void set_signed_integer(std::int64_t value) noexcept
Set this object to a signed integer.
const details::object_data & data() const noexcept
Get the internal data.
allocator_wrapper< Allocator > & allocator() noexcept
Get the allocator.
mutable_array_ref< Allocator > set_array(std::size_t size=0U)
Set this object to an array.
void set_boolean(bool value) noexcept
Set this object to a boolean value.
void clear() noexcept
Clear the data.
Derived & derived() noexcept
Access the derived class.
void set_float64(double value) noexcept
Set this object to a 64-bit floating-point number.
mutable_map_ref< Allocator > set_map(std::size_t size=0U)
Set this object to a map.
details::object_data & data() noexcept
Get the internal data.
mutable_map_ref< Allocator > as_map()
Get data as a map.
const Derived & derived() const noexcept
Access the derived class.
void set_extension(std::int8_t type, binary_view value_data)
Set this object to a extension value.
void set_binary(binary_view value)
Set this object to a binary.
Class to access non-constant arrays.
Definition array_ref.h:133
Class to access non-constant maps.
Definition map_ref.h:120
Definition of classes of references to extension values.
Definition of classes of references to maps.
Namespace of internal implementations.
Definition binary.h:35
void clear_object_data(object_data &data, allocator_wrapper< Allocator > &allocator) noexcept
Clear data.
object_data_type
Enumeration to specify types of data in objects.
@ float32
32-bit floating-point numbers.
@ float64
64-bit floating-point numbers.
Definition of object_data struct.
Definition of object_type enumeration.
Definition of helper functions for implementation of object class.
Declaration of classes of references to objects.
std::size_t size
Size of the data.
Definition object_data.h:61
std::size_t size
Size of the data.
Definition object_data.h:50
std::size_t size
Size of the data.
Definition object_data.h:88
Struct of data of key-value pairs in maps.
std::size_t size
Size of the data.
Definition object_data.h:74
key_value_pair_data * data
Data.
Definition object_data.h:71
Struct of data of objects in MessagePack.
Definition object_data.h:94
extension_data extension_value
Extension.
std::int64_t signed_integer_value
Signed integer.
double double_value
64-bit floating-point numbers.
union msgpack_light::details::object_data::@227127335344143142004214331276031357231211344336 data
Data.
object_data_type type
Type of the data.
float float_value
32-bit floating-point numbers.
std::uint64_t unsigned_integer_value
Unsigned integer.
Definition object_data.h:98
std::size_t size
Size of the data.
Definition object_data.h:39