Blender V4.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
5#pragma once
6
7#include "BLI_cpp_type.hh"
8
9namespace blender {
10
15 private:
16 const CPPType *type_ = nullptr;
17 void *data_ = nullptr;
18
19 public:
20 GMutablePointer() = default;
21
22 GMutablePointer(const CPPType *type, void *data = nullptr) : type_(type), data_(data)
23 {
24 /* If there is data, there has to be a type. */
25 BLI_assert(data_ == nullptr || type_ != nullptr);
26 }
27
28 GMutablePointer(const CPPType &type, void *data = nullptr) : GMutablePointer(&type, data) {}
29
30 template<typename T, BLI_ENABLE_IF(!std::is_void_v<T>)>
31 GMutablePointer(T *data) : GMutablePointer(&CPPType::get<T>(), data)
32 {
33 }
34
35 void *get() const
36 {
37 return data_;
38 }
39
40 const CPPType *type() const
41 {
42 return type_;
43 }
44
45 template<typename T> T *get() const
46 {
47 BLI_assert(this->is_type<T>());
48 return static_cast<T *>(data_);
49 }
50
51 template<typename T> bool is_type() const
52 {
53 return type_ != nullptr && type_->is<T>();
54 }
55
56 template<typename T> T relocate_out()
57 {
58 BLI_assert(this->is_type<T>());
59 T value;
60 type_->relocate_assign(data_, &value);
61 data_ = nullptr;
62 type_ = nullptr;
63 return value;
64 }
65
66 void destruct()
67 {
68 BLI_assert(data_ != nullptr);
69 type_->destruct(data_);
70 }
71};
72
76class GPointer {
77 private:
78 const CPPType *type_ = nullptr;
79 const void *data_ = nullptr;
80
81 public:
82 GPointer() = default;
83
84 GPointer(GMutablePointer ptr) : type_(ptr.type()), data_(ptr.get()) {}
85
86 GPointer(const CPPType *type, const void *data = nullptr) : type_(type), data_(data)
87 {
88 /* If there is data, there has to be a type. */
89 BLI_assert(data_ == nullptr || type_ != nullptr);
90 }
91
92 GPointer(const CPPType &type, const void *data = nullptr) : type_(&type), data_(data) {}
93
94 template<typename T> GPointer(T *data) : GPointer(&CPPType::get<T>(), data) {}
95
96 const void *get() const
97 {
98 return data_;
99 }
100
101 const CPPType *type() const
102 {
103 return type_;
104 }
105
106 template<typename T> const T *get() const
107 {
108 BLI_assert(this->is_type<T>());
109 return static_cast<const T *>(data_);
110 }
111
112 template<typename T> bool is_type() const
113 {
114 return type_ != nullptr && type_->is<T>();
115 }
116};
117
118} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:50
bool is() const
void destruct(void *ptr) const
void relocate_assign(void *src, void *dst) const
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
PointerRNA * ptr
Definition wm_files.cc:4126