Blender  V2.93
FN_generic_value_map.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 "BLI_linear_allocator.hh"
20 #include "BLI_map.hh"
21 
22 #include "FN_generic_pointer.hh"
23 
24 namespace blender::fn {
25 
30 template<typename Key> class GValueMap {
31  private:
32  /* Used to allocate values owned by this container. */
33  LinearAllocator<> &allocator_;
35 
36  public:
37  GValueMap(LinearAllocator<> &allocator) : allocator_(allocator)
38  {
39  }
40 
42  {
43  /* Destruct all values that are still in the map. */
44  for (GMutablePointer value : values_.values()) {
45  value.destruct();
46  }
47  }
48 
49  /* Add a value to the container. The container becomes responsible for destructing the value that
50  * is passed in. The caller remains responsible for freeing the value after it has been
51  * destructed. */
52  template<typename ForwardKey> void add_new_direct(ForwardKey &&key, GMutablePointer value)
53  {
54  values_.add_new_as(std::forward<ForwardKey>(key), value);
55  }
56 
57  /* Add a value to the container that is move constructed from the given value. The caller remains
58  * responsible for destructing and freeing the given value. */
59  template<typename ForwardKey> void add_new_by_move(ForwardKey &&key, GMutablePointer value)
60  {
61  const CPPType &type = *value.type();
62  void *buffer = allocator_.allocate(type.size(), type.alignment());
63  type.move_to_uninitialized(value.get(), buffer);
64  values_.add_new_as(std::forward<ForwardKey>(key), GMutablePointer{type, buffer});
65  }
66 
67  /* Add a value to the container that is copy constructed from the given value. The caller remains
68  * responsible for destructing and freeing the given value. */
69  template<typename ForwardKey> void add_new_by_copy(ForwardKey &&key, GPointer value)
70  {
71  const CPPType &type = *value.type();
72  void *buffer = allocator_.allocate(type.size(), type.alignment());
73  type.copy_to_uninitialized(value.get(), buffer);
74  values_.add_new_as(std::forward<ForwardKey>(key), GMutablePointer{type, buffer});
75  }
76 
77  /* Add a value to the container. */
78  template<typename ForwardKey, typename T> void add_new(ForwardKey &&key, T &&value)
79  {
80  if constexpr (std::is_rvalue_reference_v<T>) {
81  this->add_new_by_move(std::forward<ForwardKey>(key), &value);
82  }
83  else {
84  this->add_new_by_copy(std::forward<ForwardKey>(key), &value);
85  }
86  }
87 
88  /* Remove the value for the given name from the container and remove it. The caller is
89  * responsible for freeing it. The lifetime of the referenced memory might be bound to lifetime
90  * of the container. */
91  template<typename ForwardKey> GMutablePointer extract(const ForwardKey &key)
92  {
93  return values_.pop_as(key);
94  }
95 
96  /* Remove the value for the given name from the container and remove it. */
97  template<typename T, typename ForwardKey> T extract(const ForwardKey &key)
98  {
99  GMutablePointer value = values_.pop_as(key);
100  const CPPType &type = *value.type();
101  BLI_assert(type.is<T>());
102  T return_value;
103  type.relocate_to_initialized(value.get(), &return_value);
104  return return_value;
105  }
106 
107  template<typename T, typename ForwardKey> const T &lookup(const ForwardKey &key) const
108  {
109  GMutablePointer value = values_.lookup_as(key);
110  BLI_assert(value.is_type<T>());
111  BLI_assert(value.get() != nullptr);
112  return *(const T *)value.get();
113  }
114 
115  template<typename ForwardKey> bool contains(const ForwardKey &key) const
116  {
117  return values_.contains_as(key);
118  }
119 };
120 
121 } // namespace blender::fn
#define BLI_assert(a)
Definition: BLI_assert.h:58
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
void * allocate(const int64_t size, const int64_t alignment)
Value pop_as(const ForwardKey &key)
Definition: BLI_map.hh:375
void add_new_as(ForwardKey &&key, ForwardValue &&value)
Definition: BLI_map.hh:251
ValueIterator values() const
Definition: BLI_map.hh:806
bool contains_as(const ForwardKey &key) const
Definition: BLI_map.hh:326
const Value & lookup_as(const ForwardKey &key) const
Definition: BLI_map.hh:507
const CPPType * type() const
const void * get() const
const CPPType * type() const
GMutablePointer extract(const ForwardKey &key)
T extract(const ForwardKey &key)
const T & lookup(const ForwardKey &key) const
void add_new(ForwardKey &&key, T &&value)
void add_new_by_copy(ForwardKey &&key, GPointer value)
void add_new_direct(ForwardKey &&key, GMutablePointer value)
GValueMap(LinearAllocator<> &allocator)
void add_new_by_move(ForwardKey &&key, GMutablePointer value)
bool contains(const ForwardKey &key) const
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer
#define T