Blender V5.0
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, BLI_ENABLE_IF((!std::is_void_v<T>))>
105 {
106 }
107
108 operator bool() const
109 {
110 return data_ != nullptr;
111 }
112
113 const void *get() const
114 {
115 return data_;
116 }
117
118 const CPPType *type() const
119 {
120 return type_;
121 }
122
123 template<typename T> const T *get() const
124 {
125 BLI_assert(this->is_type<T>());
126 return static_cast<const T *>(data_);
127 }
128
129 template<typename T> bool is_type() const
130 {
131 return type_ != nullptr && type_->is<T>();
132 }
133};
134
135} // 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:4238