Blender V5.0
NOD_multi_function.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
8
9#include "DNA_node_types.h"
10
11namespace blender::nodes {
12
14
19 private:
20 const bNode &node_;
21 const bNodeTree &tree_;
22 std::shared_ptr<mf::MultiFunction> owned_built_fn_;
23 const mf::MultiFunction *built_fn_ = nullptr;
24
25 friend NodeMultiFunctions;
26
27 public:
29
34 void set_matching_fn(const mf::MultiFunction *fn);
35 void set_matching_fn(const mf::MultiFunction &fn);
36
41 template<typename T, typename... Args> void construct_and_set_matching_fn(Args &&...args);
42
49 template<typename Fn> void construct_and_set_matching_fn_cb(Fn &&create_multi_function);
50
51 const bNode &node();
52 const bNodeTree &tree();
53 const mf::MultiFunction &function();
54};
55
60 public:
61 struct Item {
62 const mf::MultiFunction *fn = nullptr;
63 std::shared_ptr<mf::MultiFunction> owned_fn;
64 };
65
66 private:
68
69 public:
71
72 const Item &try_get(const bNode &node) const;
73};
74
75/* -------------------------------------------------------------------- */
78
80 : node_(node), tree_(tree)
81{
82}
83
85{
86 return node_;
87}
88
90{
91 return tree_;
92}
93
94inline const mf::MultiFunction &NodeMultiFunctionBuilder::function()
95{
96 return *built_fn_;
97}
98
99inline void NodeMultiFunctionBuilder::set_matching_fn(const mf::MultiFunction *fn)
100{
101 built_fn_ = fn;
102}
103
104inline void NodeMultiFunctionBuilder::set_matching_fn(const mf::MultiFunction &fn)
105{
106 built_fn_ = &fn;
107}
108
109template<typename T, typename... Args>
111{
112 owned_built_fn_ = std::make_shared<T>(std::forward<Args>(args)...);
113 built_fn_ = &*owned_built_fn_;
114}
115
116template<typename Fn>
118{
119 using T = decltype(create_multi_function());
120 T *allocated_function = new T(create_multi_function());
121 owned_built_fn_ = std::shared_ptr<T>(allocated_function);
122 built_fn_ = &*owned_built_fn_;
123}
124
126
127/* -------------------------------------------------------------------- */
130
132{
133 static Item empty_item;
134 const Item *item = map_.lookup_ptr(&node);
135 if (item == nullptr) {
136 return empty_item;
137 }
138 return *item;
139}
140
142
143} // namespace blender::nodes
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
void set_matching_fn(const mf::MultiFunction *fn)
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
NodeMultiFunctionBuilder(const bNode &node, const bNodeTree &tree)
const Item & try_get(const bNode &node) const
KDTree_3d * tree
#define T
std::shared_ptr< mf::MultiFunction > owned_fn