Blender V5.3
BLI_any_derived.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BLI_any.hh"
12
13namespace blender {
14
15namespace detail {
16
17template<typename Base> struct AnyDerivedExtraInfo {
18
19 Base *(*get_impl)(const void *buffer);
20
21 template<typename StorageT> static constexpr AnyDerivedExtraInfo get()
22 {
23 /* These are the only allowed types in the #Any. */
24 static_assert(std::is_base_of_v<Base, StorageT> ||
26
27 /* Depending on how the implementation is stored in the #Any, a different #get_impl function
28 * is required. */
29 if constexpr (std::is_base_of_v<Base, StorageT>) {
30 return {[](const void *buffer) {
31 return static_cast<Base *>(const_cast<StorageT *>(static_cast<const StorageT *>(buffer)));
32 }};
33 }
34 else if constexpr (std::is_same_v<StorageT, Base *>) {
35 return {[](const void *buffer) {
36 return *const_cast<StorageT *>(static_cast<const StorageT *>(buffer));
37 }};
38 }
39 else if constexpr (std::is_same_v<StorageT, std::shared_ptr<Base>>) {
40 return {[](const void *buffer) {
41 return (const_cast<StorageT *>(static_cast<const StorageT *>(buffer)))->get();
42 }};
43 }
44 else {
46 return {};
47 }
48 }
49};
50
51} // namespace detail
52
70template<typename Base, int64_t InlineBufferCapacity = 24> struct AnyDerived {
71 private:
72 using Storage = Any<detail::AnyDerivedExtraInfo<Base>, InlineBufferCapacity, alignof(Base)>;
73
75 Base *impl_ = nullptr;
76
83 Storage storage_;
84
85 public:
86 AnyDerived() = default;
87
88 AnyDerived(const AnyDerived &other) : storage_(other.storage_)
89 {
90 impl_ = this->impl_from_storage();
91 }
92
93 AnyDerived(AnyDerived &&other) noexcept : storage_(std::move(other.storage_))
94 {
95 impl_ = this->impl_from_storage();
96 other.storage_.reset();
97 other.impl_ = nullptr;
98 }
99
100 explicit AnyDerived(Base *impl) : impl_(impl)
101 {
102 storage_ = impl_;
103 }
104
105 explicit AnyDerived(std::shared_ptr<Base> impl) : impl_(impl.get())
106 {
107 if (impl_) {
108 storage_ = std::move(impl);
109 }
110 }
111
113 {
114 if (this == &other) {
115 return *this;
116 }
117 storage_ = other.storage_;
118 impl_ = this->impl_from_storage();
119 return *this;
120 }
121
122 AnyDerived &operator=(AnyDerived &&other) noexcept
123 {
124 if (this == &other) {
125 return *this;
126 }
127 storage_ = std::move(other.storage_);
128 impl_ = this->impl_from_storage();
129 other.storage_.reset();
130 other.impl_ = nullptr;
131 return *this;
132 }
133
134 template<typename ImplT, typename... Args>
135 requires std::is_base_of_v<Base, ImplT>
136 void emplace(Args &&...args)
137 {
138 if constexpr (std::is_copy_constructible_v<ImplT> && Storage::template is_inline_v<ImplT>) {
139 /* Only inline the implementation when it is copyable and when it fits into the inline
140 * buffer of the storage. */
141 impl_ = &storage_.template emplace<ImplT>(std::forward<Args>(args)...);
142 }
143 else {
144 /* If it can't be inlined, create a new #std::shared_ptr instead and store that in the
145 * storage. */
146 std::shared_ptr<Base> ptr = std::make_shared<ImplT>(std::forward<Args>(args)...);
147 impl_ = &*ptr;
148 storage_ = std::move(ptr);
149 }
150 }
151
152 operator bool() const
153 {
154 return impl_ != nullptr;
155 }
156
158 {
159 return *impl_;
160 }
161
163 {
164 return impl_;
165 }
166
167 Base *get() const
168 {
169 return impl_;
170 }
171
172 protected:
174 {
175 if (!storage_.has_value()) {
176 return nullptr;
177 }
178 return storage_.extra_info().get_impl(storage_.get());
179 }
180};
181
182} // namespace blender
#define BLI_assert_unreachable()
BMesh Iterators.
constexpr bool is_same_any_v
Base & operator*() const
AnyDerived(AnyDerived &&other) noexcept
AnyDerived(std::shared_ptr< Base > impl)
AnyDerived & operator=(const AnyDerived &other)
Base * operator->() const
AnyDerived & operator=(AnyDerived &&other) noexcept
Base * impl_from_storage() const
void emplace(Args &&...args)
AnyDerived(const AnyDerived &other)
static constexpr AnyDerivedExtraInfo get()