Blender V4.5
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, BLI_ENABLE_IF(!std::is_void_v<T>)>
38
39 void *get() const
40 {
41 return data_;
42 }
43
44 const CPPType *type() const
45 {
46 return type_;
47 }
48
49 operator bool() const
50 {
51 return data_ != nullptr;
52 }
53
54 template<typename T> T *get() const
55 {
56 BLI_assert(this->is_type<T>());
57 return static_cast<T *>(data_);
58 }
59
60 template<typename T> bool is_type() const
61 {
62 return type_ != nullptr && type_->is<T>();
63 }
64
65 template<typename T> T relocate_out()
66 {
67 BLI_assert(this->is_type<T>());
68 T value;
69 type_->relocate_assign(data_, &value);
70 data_ = nullptr;
71 type_ = nullptr;
72 return value;
73 }
74
75 void destruct()
76 {
77 BLI_assert(data_ != nullptr);
78 type_->destruct(data_);
79 }
80};
81
85class GPointer {
86 private:
87 const CPPType *type_ = nullptr;
88 const void *data_ = nullptr;
89
90 public:
91 GPointer() = default;
92
93 GPointer(GMutablePointer ptr) : type_(ptr.type()), data_(ptr.get()) {}
94
95 GPointer(const CPPType *type, const void *data = nullptr) : type_(type), data_(data)
96 {
97 /* If there is data, there has to be a type. */
98 BLI_assert(data_ == nullptr || type_ != nullptr);
99 }
100
101 GPointer(const CPPType &type, const void *data = nullptr) : type_(&type), data_(data) {}
102
103 template<typename T> GPointer(T *data) : GPointer(&CPPType::get<T>(), data) {}
104
105 operator bool() const
106 {
107 return data_ != nullptr;
108 }
109
110 const void *get() const
111 {
112 return data_;
113 }
114
115 const CPPType *type() const
116 {
117 return type_;
118 }
119
120 template<typename T> const T *get() const
121 {
122 BLI_assert(this->is_type<T>());
123 return static_cast<const T *>(data_);
124 }
125
126 template<typename T> bool is_type() const
127 {
128 return type_ != nullptr && type_->is<T>();
129 }
130};
131
132} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:46
BMesh const char void * data
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
PointerRNA * ptr
Definition wm_files.cc:4226