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