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