Blender V4.3
BLI_generic_value_map.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
9#include "BLI_map.hh"
10
11namespace blender {
12
17template<typename Key> class GValueMap {
18 private:
19 /* Used to allocate values owned by this container. */
20 LinearAllocator<> &allocator_;
22
23 public:
24 GValueMap(LinearAllocator<> &allocator) : allocator_(allocator) {}
25
27 {
28 /* Destruct all values that are still in the map. */
29 for (GMutablePointer value : values_.values()) {
30 value.destruct();
31 }
32 }
33
34 /* Add a value to the container. The container becomes responsible for destructing the value that
35 * is passed in. The caller remains responsible for freeing the value after it has been
36 * destructed. */
37 template<typename ForwardKey> void add_new_direct(ForwardKey &&key, GMutablePointer value)
38 {
39 values_.add_new_as(std::forward<ForwardKey>(key), value);
40 }
41
42 /* Add a value to the container that is move constructed from the given value. The caller remains
43 * responsible for destructing and freeing the given value. */
44 template<typename ForwardKey> void add_new_by_move(ForwardKey &&key, GMutablePointer value)
45 {
46 const CPPType &type = *value.type();
47 void *buffer = allocator_.allocate(type.size(), type.alignment());
48 type.move_construct(value.get(), buffer);
49 values_.add_new_as(std::forward<ForwardKey>(key), GMutablePointer{type, buffer});
50 }
51
52 /* Add a value to the container that is copy constructed from the given value. The caller remains
53 * responsible for destructing and freeing the given value. */
54 template<typename ForwardKey> void add_new_by_copy(ForwardKey &&key, GPointer value)
55 {
56 const CPPType &type = *value.type();
57 void *buffer = allocator_.allocate(type.size(), type.alignment());
58 type.copy_construct(value.get(), buffer);
59 values_.add_new_as(std::forward<ForwardKey>(key), GMutablePointer{type, buffer});
60 }
61
62 /* Add a value to the container. */
63 template<typename ForwardKey, typename T> void add_new(ForwardKey &&key, T &&value)
64 {
65 if constexpr (std::is_rvalue_reference_v<T>) {
66 this->add_new_by_move(std::forward<ForwardKey>(key), &value);
67 }
68 else {
69 this->add_new_by_copy(std::forward<ForwardKey>(key), &value);
70 }
71 }
72
73 /* Remove the value for the given name from the container and remove it. The caller is
74 * responsible for freeing it. The lifetime of the referenced memory might be bound to lifetime
75 * of the container. */
76 template<typename ForwardKey> GMutablePointer extract(const ForwardKey &key)
77 {
78 return values_.pop_as(key);
79 }
80
81 template<typename ForwardKey> GPointer lookup(const ForwardKey &key) const
82 {
83 return values_.lookup_as(key);
84 }
85
86 /* Remove the value for the given name from the container and remove it. */
87 template<typename T, typename ForwardKey> T extract(const ForwardKey &key)
88 {
89 GMutablePointer value = values_.pop_as(key);
90 const CPPType &type = *value.type();
91 BLI_assert(type.is<T>());
92 T return_value;
93 type.relocate_assign(value.get(), &return_value);
94 return return_value;
95 }
96
97 template<typename T, typename ForwardKey> const T &lookup(const ForwardKey &key) const
98 {
99 GMutablePointer value = values_.lookup_as(key);
100 BLI_assert(value.is_type<T>());
101 BLI_assert(value.get() != nullptr);
102 return *(const T *)value.get();
103 }
104
105 template<typename ForwardKey> bool contains(const ForwardKey &key) const
106 {
107 return values_.contains_as(key);
108 }
109};
110
111} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:50
void add_new_by_copy(ForwardKey &&key, GPointer value)
void add_new(ForwardKey &&key, T &&value)
void add_new_by_move(ForwardKey &&key, GMutablePointer value)
const T & lookup(const ForwardKey &key) const
bool contains(const ForwardKey &key) const
void add_new_direct(ForwardKey &&key, GMutablePointer value)
GPointer lookup(const ForwardKey &key) const
GMutablePointer extract(const ForwardKey &key)
GValueMap(LinearAllocator<> &allocator)
T extract(const ForwardKey &key)
void * allocate(const int64_t size, const int64_t alignment)
Value pop_as(const ForwardKey &key)
Definition BLI_map.hh:382
ValueIterator values() const
Definition BLI_map.hh:846
void add_new_as(ForwardKey &&key, ForwardValue &&...value)
Definition BLI_map.hh:258
const Value & lookup_as(const ForwardKey &key) const
Definition BLI_map.hh:514
bool contains_as(const ForwardKey &key) const
Definition BLI_map.hh:333