Blender  V2.93
FN_generic_pointer.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
19 #include "FN_cpp_type.hh"
20 
21 namespace blender::fn {
22 
27  private:
28  const CPPType *type_ = nullptr;
29  void *data_ = nullptr;
30 
31  public:
32  GMutablePointer() = default;
33 
34  GMutablePointer(const CPPType *type, void *data = nullptr) : type_(type), data_(data)
35  {
36  /* If there is data, there has to be a type. */
37  BLI_assert(data_ == nullptr || type_ != nullptr);
38  }
39 
40  GMutablePointer(const CPPType &type, void *data = nullptr) : GMutablePointer(&type, data)
41  {
42  }
43 
44  template<typename T> GMutablePointer(T *data) : GMutablePointer(&CPPType::get<T>(), data)
45  {
46  }
47 
48  void *get() const
49  {
50  return data_;
51  }
52 
53  const CPPType *type() const
54  {
55  return type_;
56  }
57 
58  template<typename T> T *get() const
59  {
60  BLI_assert(this->is_type<T>());
61  return static_cast<T *>(data_);
62  }
63 
64  template<typename T> bool is_type() const
65  {
66  return type_ != nullptr && type_->is<T>();
67  }
68 
69  void destruct()
70  {
71  BLI_assert(data_ != nullptr);
72  type_->destruct(data_);
73  }
74 };
75 
79 class GPointer {
80  private:
81  const CPPType *type_ = nullptr;
82  const void *data_ = nullptr;
83 
84  public:
85  GPointer() = default;
86 
88  {
89  }
90 
91  GPointer(const CPPType *type, const void *data = nullptr) : type_(type), data_(data)
92  {
93  /* If there is data, there has to be a type. */
94  BLI_assert(data_ == nullptr || type_ != nullptr);
95  }
96 
97  GPointer(const CPPType &type, const void *data = nullptr) : type_(&type), data_(data)
98  {
99  }
100 
101  template<typename T> GPointer(T *data) : GPointer(&CPPType::get<T>(), data)
102  {
103  }
104 
105  const void *get() const
106  {
107  return data_;
108  }
109 
110  const CPPType *type() const
111  {
112  return type_;
113  }
114 
115  template<typename T> const T *get() const
116  {
117  BLI_assert(this->is_type<T>());
118  return static_cast<const T *>(data_);
119  }
120 
121  template<typename T> bool is_type() const
122  {
123  return type_ != nullptr && type_->is<T>();
124  }
125 };
126 
127 } // namespace blender::fn
#define BLI_assert(a)
Definition: BLI_assert.h:58
void destruct(void *ptr) const
Definition: FN_cpp_type.hh:358
GMutablePointer(const CPPType *type, void *data=nullptr)
GMutablePointer(const CPPType &type, void *data=nullptr)
const CPPType * type() const
const void * get() const
GPointer(const CPPType *type, const void *data=nullptr)
GPointer(GMutablePointer ptr)
GPointer(const CPPType &type, const void *data=nullptr)
const CPPType * type() const
T * data_
#define T
PointerRNA * ptr
Definition: wm_files.c:3157