cpp-msgpack-light 0.3.0
A light library to serialize MessagePack.
Loading...
Searching...
No Matches
array_ref.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 <algorithm>
23#include <cstring>
24
29#include "msgpack_light/details/object_ref_decl.h" // IWYU pragma: keep
31
32namespace msgpack_light {
33
46public:
49
52
55
61 explicit const_array_ref(const details::array_data& data) : data_(&data) {}
62
68 [[nodiscard]] std::size_t size() const noexcept { return data_->size; }
69
77 std::size_t index) const noexcept;
78
84 [[nodiscard]] const_array_iterator begin() const noexcept {
85 return const_array_iterator{data_->data};
86 }
87
93 [[nodiscard]] const_array_iterator end() const noexcept {
94 return const_array_iterator{data_->data + data_->size};
95 }
96
102 [[nodiscard]] const_array_iterator cbegin() const noexcept {
103 return const_array_iterator{data_->data};
104 }
105
111 [[nodiscard]] const_array_iterator cend() const noexcept {
112 return const_array_iterator{data_->data + data_->size};
113 }
114
115private:
118};
119
132template <typename Allocator = standard_allocator>
134public:
136 using allocator_type = Allocator;
137
140
143
146
149
159
165 void resize(std::size_t size) {
166 if (size < data_->size) {
167 for (std::size_t i = size; i < data_->size; ++i) {
169 }
170 }
171 details::object_data* new_data = allocator_->allocate_object_data(size);
172 std::memcpy(new_data, data_->data,
173 std::min(data_->size, size) * sizeof(details::object_data));
174 allocator_->deallocate_object_data(data_->data);
175 data_->data = new_data;
176 if (size > data_->size) {
177 std::memset(data_->data + data_->size, 0,
178 (size - data_->size) * sizeof(details::object_data));
179 }
180 data_->size = size;
181 }
182
188 [[nodiscard]] std::size_t size() const noexcept { return data_->size; }
189
197 std::size_t index) noexcept;
198
205 [[nodiscard]] const_object_ref_type operator[](
206 std::size_t index) const noexcept;
207
216
222 [[nodiscard]] mutable_array_iterator<Allocator> end() noexcept {
224 data_->data + data_->size, allocator_};
225 }
226
232 [[nodiscard]] const_array_iterator begin() const noexcept {
233 return const_array_iterator{data_->data};
234 }
235
241 [[nodiscard]] const_array_iterator end() const noexcept {
242 return const_array_iterator{data_->data + data_->size};
243 }
244
245private:
248
251};
252
253} // namespace msgpack_light
Definition of allocator_wrapper class.
Definition of classes of iterators of arrays.
Class of iterators of arrays to access constant objects.
const_array_iterator const_iterator
Type of iterators.
Definition array_ref.h:54
const_array_iterator begin() const noexcept
Get an iterator to the first element.
Definition array_ref.h:84
const_array_iterator end() const noexcept
Get an iterator to the past-the-end element.
Definition array_ref.h:93
const_object_ref const_object_ref_type
Type to access constant objects.
Definition array_ref.h:48
const_array_ref(const details::array_data &data)
Constructor.
Definition array_ref.h:61
const_array_iterator cend() const noexcept
Get an iterator to the past-the-end element.
Definition array_ref.h:111
const details::array_data * data_
Data.
Definition array_ref.h:117
const_object_ref_type operator[](std::size_t index) const noexcept
Get an object.
const_array_iterator iterator
Type of iterators.
Definition array_ref.h:51
const_array_iterator cbegin() const noexcept
Get an iterator to the first element.
Definition array_ref.h:102
std::size_t size() const noexcept
Get the size.
Definition array_ref.h:68
Class to access constant objects.
Definition object_ref.h:129
Class of iterators of arrays to access non-constant objects.
void resize(std::size_t size)
Change the size.
Definition array_ref.h:165
details::array_data * data_
Data.
Definition array_ref.h:247
mutable_array_iterator< Allocator > begin() noexcept
Get an iterator to the first element.
Definition array_ref.h:213
mutable_array_ref(details::array_data &data, details::allocator_wrapper< Allocator > &allocator)
Constructor.
Definition array_ref.h:156
const_array_iterator const_iterator
Type of iterators.
Definition array_ref.h:148
mutable_object_ref_type operator[](std::size_t index) noexcept
Get an object.
mutable_array_iterator< Allocator > iterator
Type of iterators.
Definition array_ref.h:145
details::allocator_wrapper< Allocator > * allocator_
Allocator.
Definition array_ref.h:250
mutable_array_iterator< Allocator > end() noexcept
Get an iterator to the past-the-end element.
Definition array_ref.h:222
const_object_ref const_object_ref_type
Type to access constant objects.
Definition array_ref.h:142
mutable_object_ref< Allocator > mutable_object_ref_type
Type to access non-constant objects.
Definition array_ref.h:139
Allocator allocator_type
Type of the allocator.
Definition array_ref.h:136
const_array_iterator end() const noexcept
Get an iterator to the past-the-end element.
Definition array_ref.h:241
std::size_t size() const noexcept
Get the size.
Definition array_ref.h:188
const_array_iterator begin() const noexcept
Get an iterator to the first element.
Definition array_ref.h:232
Class to access non-constant objects.
Definition object_ref.h:48
void clear_object_data(object_data &data, allocator_wrapper< Allocator > &allocator) noexcept
Clear data.
Namespace of this project.
Definition binary.h:33
Definition of object_data struct.
Definition of helper functions for implementation of object class.
Declaration of classes of references to objects.
Definition of standard_allocator class.
Struct of data of arrays.
Definition object_data.h:56
Struct of data of objects in MessagePack.
Definition object_data.h:94