Blender V5.3
BLI_generic_pointer.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BLI_cpp_type.hh"
12
13namespace blender {
14
19 private:
20 const CPPType *type_ = nullptr;
21 void *data_ = nullptr;
22
23 public:
24 GMutablePointer() = default;
25
26 GMutablePointer(const CPPType *type, void *data = nullptr) : type_(type), data_(data)
27 {
28 /* If there is data, there has to be a type. */
29 BLI_assert(data_ == nullptr || type_ != nullptr);
30 }
31
32 GMutablePointer(const CPPType &type, void *data = nullptr) : GMutablePointer(&type, data) {}
33
34 template<typename T>
36 requires(!std::is_void_v<T>)
38 {
39 }
40
41 void *get() const
42 {
43 return data_;
44 }
45
46 const CPPType *type() const
47 {
48 return type_;
49 }
50
51 operator bool() const
52 {
53 return data_ != nullptr;
54 }
55
56 template<typename T> T *get() const
57 {
58 BLI_assert(this->is_type<T>());
59 return static_cast<T *>(data_);
60 }
61
62 template<typename T> bool is_type() const
63 {
64 return type_ != nullptr && type_->is<T>();
65 }
66
67 template<typename T> T relocate_out()
68 {
69 BLI_assert(this->is_type<T>());
70 T value;
71 type_->relocate_assign(data_, &value);
72 data_ = nullptr;
73 type_ = nullptr;
74 return value;
75 }
76
77 void destruct()
78 {
79 BLI_assert(data_ != nullptr);
80 type_->destruct(data_);
81 }
82};
83
87class GPointer {
88 private:
89 const CPPType *type_ = nullptr;
90 const void *data_ = nullptr;
91
92 public:
93 GPointer() = default;
94
95 GPointer(GMutablePointer ptr) : type_(ptr.type()), data_(ptr.get()) {}
96
97 GPointer(const CPPType *type, const void *data = nullptr) : type_(type), data_(data)
98 {
99 /* If there is data, there has to be a type. */
100 BLI_assert(data_ == nullptr || type_ != nullptr);
101 }
102
103 GPointer(const CPPType &type, const void *data = nullptr) : type_(&type), data_(data) {}
104
105 template<typename T>
107 requires(!std::is_void_v<T>)
108 : GPointer(&CPPType::get<T>(), data)
109 {
110 }
111
112 operator bool() const
113 {
114 return data_ != nullptr;
115 }
116
117 const void *get() const
118 {
119 return data_;
120 }
121
122 const CPPType *type() const
123 {
124 return type_;
125 }
126
127 template<typename T> const T *get() const
128 {
129 BLI_assert(this->is_type<T>());
130 return static_cast<const T *>(data_);
131 }
132
133 template<typename T> bool is_type() const
134 {
135 return type_ != nullptr && type_->is<T>();
136 }
137};
138
139} // namespace blender
GMutablePointer(const CPPType *type, void *data=nullptr)
const CPPType * type() const
GMutablePointer(const CPPType &type, void *data=nullptr)
GPointer(GMutablePointer ptr)
const CPPType * type() const
GPointer(const CPPType &type, const void *data=nullptr)
GPointer(const CPPType *type, const void *data=nullptr)
GPointer()=default
const T * get() const
const void * get() const
#define T
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BMesh const char void * data