Blender V4.5
nodes/intern/geometry_nodes_bundle.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_cpp_type.hh"
6
8
9namespace blender::nodes {
10
11SocketInterfaceKey::SocketInterfaceKey(std::string identifier)
12{
13 identifiers_.append(std::move(identifier));
14}
15
16SocketInterfaceKey::SocketInterfaceKey(Vector<std::string> identifiers)
17 : identifiers_(std::move(identifiers))
18{
19 BLI_assert(!identifiers_.is_empty());
20}
21
22Span<std::string> SocketInterfaceKey::identifiers() const
23{
24 return identifiers_;
25}
26
27bool SocketInterfaceKey::matches(const SocketInterfaceKey &other) const
28{
29 for (const std::string &identifier : other.identifiers_) {
30 if (identifiers_.contains(identifier)) {
31 return true;
32 }
33 }
34 return false;
35}
36
37Bundle::Bundle() = default;
38
39Bundle::~Bundle()
40{
41 for (StoredItem &item : items_) {
42 item.type->geometry_nodes_cpp_type->destruct(item.value);
43 }
44 for (void *buffer : buffers_) {
45 MEM_freeN(buffer);
46 }
47}
48
49Bundle::Bundle(const Bundle &other)
50{
51 for (const StoredItem &item : other.items_) {
52 this->add_new(item.key, *item.type, item.value);
53 }
54}
55
56Bundle::Bundle(Bundle &&other) noexcept
57 : items_(std::move(other.items_)), buffers_(std::move(other.buffers_))
58{
59}
60
61Bundle &Bundle::operator=(const Bundle &other)
62{
63 if (this == &other) {
64 return *this;
65 }
66 this->~Bundle();
67 new (this) Bundle(other);
68 return *this;
69}
70
71Bundle &Bundle::operator=(Bundle &&other) noexcept
72{
73 if (this == &other) {
74 return *this;
75 }
76 this->~Bundle();
77 new (this) Bundle(std::move(other));
78 return *this;
79}
80
81void Bundle::add_new(SocketInterfaceKey key, const bke::bNodeSocketType &type, const void *value)
82{
83 BLI_assert(!this->contains(key));
84 BLI_assert(type.geometry_nodes_cpp_type);
85 const CPPType &cpp_type = *type.geometry_nodes_cpp_type;
86 void *buffer = MEM_mallocN_aligned(cpp_type.size, cpp_type.alignment, __func__);
87 cpp_type.copy_construct(value, buffer);
88 items_.append(StoredItem{std::move(key), &type, buffer});
89 buffers_.append(buffer);
90}
91
93 const bke::bNodeSocketType &type,
94 const void *value)
95{
96 if (this->contains(key)) {
97 return false;
98 }
99 this->add_new(key, type, value);
100 return true;
101}
102
103bool Bundle::add(SocketInterfaceKey &&key, const bke::bNodeSocketType &type, const void *value)
104{
105 if (this->contains(key)) {
106 return false;
107 }
108 this->add_new(std::move(key), type, value);
109 return true;
110}
111
112std::optional<Bundle::Item> Bundle::lookup(const SocketInterfaceKey &key) const
113{
114 for (const StoredItem &item : items_) {
115 if (item.key.matches(key)) {
116 return Item{item.type, item.value};
117 }
118 }
119 return std::nullopt;
120}
121
122bool Bundle::remove(const SocketInterfaceKey &key)
123{
124 const int removed_num = items_.remove_if([&key](StoredItem &item) {
125 if (item.key.matches(key)) {
126 item.type->geometry_nodes_cpp_type->destruct(item.value);
127 return true;
128 }
129 return false;
130 });
131 return removed_num >= 1;
132}
133
134bool Bundle::contains(const SocketInterfaceKey &key) const
135{
136 for (const StoredItem &item : items_) {
137 if (item.key.matches(key)) {
138 return true;
139 }
140 }
141 return false;
142}
143
144void Bundle::delete_self()
145{
146 MEM_delete(this);
147}
148
149} // namespace blender::nodes
#define BLI_assert(a)
Definition BLI_assert.h:46
int64_t size
void copy_construct(const void *src, void *dst) const
int64_t alignment
bool add(const SocketInterfaceKey &key, const bke::bNodeSocketType &type, const void *value)
void add_new(SocketInterfaceKey key, const bke::bNodeSocketType &type, const void *value)
std::optional< Item > lookup(const SocketInterfaceKey &key) const
bool contains(const SocketInterfaceKey &key) const
void * MEM_mallocN_aligned(size_t len, size_t alignment, const char *str)
Definition mallocn.cc:138
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
Defines a socket type.
Definition BKE_node.hh:152