numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
object_storage.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 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 <array>
24#include <cstddef>
25#include <new>
26#include <utility>
27
29
30#ifndef NDEBUG
31#include <cassert>
32#endif
33
34namespace num_collect::util {
35
47template <typename T, std::size_t MinimumAlignment = cache_line>
48class alignas(std::max(MinimumAlignment, alignof(T))) object_storage {
49public:
53 object_storage() noexcept = default;
54
55 object_storage(const object_storage&) = delete;
57 auto operator=(const object_storage&) = delete;
58 auto operator=(object_storage&&) = delete;
59
63 ~object_storage() noexcept
64#ifndef NDEBUG
65 {
66 assert(!has_object_);
67 }
68#else
69 = default;
70#endif
71
80 template <typename... Args>
81 void emplace(Args&&... args) {
82#ifndef NDEBUG
83 assert(!has_object_);
84#endif
85 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
86 ::new (storage_.data()) T(std::forward<Args>(args)...);
87#ifndef NDEBUG
88 has_object_ = true;
89#endif
90 }
91
97 void reset() noexcept {
98 get_pointer()->~T();
99#ifndef NDEBUG
100 has_object_ = false;
101#endif
102 }
103
111 [[nodiscard]] auto get_ref() noexcept -> T& { return *get_pointer(); }
112
120 [[nodiscard]] auto get_ref() const noexcept -> const T& {
121 return *get_pointer();
122 }
123
131 [[nodiscard]] auto get_pointer() noexcept -> T* {
132#ifndef NDEBUG
133 assert(has_object_);
134#endif
135 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
136 return std::launder(reinterpret_cast<T*>(storage_.data()));
137 }
138
146 [[nodiscard]] auto get_pointer() const noexcept -> const T* {
147#ifndef NDEBUG
148 assert(has_object_);
149#endif
150 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
151 return std::launder(reinterpret_cast<const T*>(storage_.data()));
152 }
153
154private:
156 std::array<char, sizeof(T)> storage_{};
157
158#ifndef NDEBUG
163 bool has_object_{false};
164#endif
165};
166
167} // namespace num_collect::util
Definition of cache_line variable.
Class of storage of objects.
auto get_ref() const noexcept -> const T &
Get the reference of the value.
auto get_pointer() noexcept -> T *
Get the pointer of the value.
std::array< char, sizeof(T)> storage_
Storage.
bool has_object_
Whether this storage has an object. (Defined only in debug build for debugging.)
auto get_ref() noexcept -> T &
Get the reference of the value.
void emplace(Args &&... args)
Construct an object.
void reset() noexcept
Destruct the object.
object_storage() noexcept=default
Constructor.
auto get_pointer() const noexcept -> const T *
Get the pointer of the value.
Namespace of utilities.
Definition assert.h:30