Blender  V2.93
BLI_resource_scope.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 
42 #include "BLI_linear_allocator.hh"
43 #include "BLI_utility_mixins.hh"
44 #include "BLI_vector.hh"
45 
46 namespace blender {
47 
49  private:
50  struct ResourceData {
51  void *data;
52  void (*free)(void *data);
53  const char *debug_name;
54  };
55 
56  LinearAllocator<> m_allocator;
57  Vector<ResourceData> m_resources;
58 
59  public:
60  ResourceScope() = default;
61 
63  {
64  /* Free in reversed order. */
65  for (int64_t i = m_resources.size(); i--;) {
66  ResourceData &data = m_resources[i];
67  data.free(data.data);
68  }
69  }
70 
75  template<typename T> T *add(std::unique_ptr<T> resource, const char *name)
76  {
77  BLI_assert(resource.get() != nullptr);
78  T *ptr = resource.release();
79  if (ptr == nullptr) {
80  return nullptr;
81  }
82  this->add(
83  ptr,
84  [](void *data) {
85  T *typed_data = reinterpret_cast<T *>(data);
86  delete typed_data;
87  },
88  name);
89  return ptr;
90  }
91 
96  template<typename T> T *add(destruct_ptr<T> resource, const char *name)
97  {
98  T *ptr = resource.release();
99  if (ptr == nullptr) {
100  return nullptr;
101  }
102  /* There is no need to keep track of such types. */
103  if (std::is_trivially_destructible_v<T>) {
104  return ptr;
105  }
106 
107  this->add(
108  ptr,
109  [](void *data) {
110  T *typed_data = reinterpret_cast<T *>(data);
111  typed_data->~T();
112  },
113  name);
114  return ptr;
115  }
116 
121  void add(void *userdata, void (*free)(void *), const char *name)
122  {
123  ResourceData data;
124  data.debug_name = name;
125  data.data = userdata;
126  data.free = free;
127  m_resources.append(data);
128  }
129 
134  template<typename T> T &add_value(T &&value, const char *name)
135  {
136  return this->construct<T>(name, std::forward<T>(value));
137  }
138 
144  {
145  return m_allocator;
146  }
147 
151  template<typename T, typename... Args> T &construct(const char *name, Args &&... args)
152  {
153  destruct_ptr<T> value_ptr = m_allocator.construct<T>(std::forward<Args>(args)...);
154  T &value_ref = *value_ptr;
155  this->add(std::move(value_ptr), name);
156  return value_ref;
157  }
158 
163  void print(StringRef name) const
164  {
165  if (m_resources.size() == 0) {
166  std::cout << "\"" << name << "\" has no resources.\n";
167  return;
168  }
169  else {
170  std::cout << "Resources for \"" << name << "\":\n";
171  for (const ResourceData &data : m_resources) {
172  std::cout << " " << data.data << ": " << data.debug_name << '\n';
173  }
174  }
175  }
176 };
177 
178 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:58
void BLI_kdtree_nd_() free(KDTree *tree)
Definition: kdtree_impl.h:116
destruct_ptr< T > construct(Args &&... args)
T & construct(const char *name, Args &&... args)
T * add(destruct_ptr< T > resource, const char *name)
T & add_value(T &&value, const char *name)
LinearAllocator & linear_allocator()
void add(void *userdata, void(*free)(void *), const char *name)
void print(StringRef name) const
T * add(std::unique_ptr< T > resource, const char *name)
int64_t size() const
Definition: BLI_vector.hh:662
void append(const T &value)
Definition: BLI_vector.hh:438
#define T
std::unique_ptr< T, DestructValueAtAddress< T > > destruct_ptr
__int64 int64_t
Definition: stdint.h:92
PointerRNA * ptr
Definition: wm_files.c:3157