Blender V5.3
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
8
9#pragma once
10
11#include "FN_multi_function.hh"
12
13#include "DNA_node_types.h"
14
15namespace blender::nodes {
16
18
23 private:
24 const bNode &node_;
25 const bNodeTree &tree_;
26 std::shared_ptr<mf::MultiFunction> owned_built_fn_;
27 const mf::MultiFunction *built_fn_ = nullptr;
32 std::shared_ptr<const bNodeTree> shared_tree_;
33
34 friend NodeMultiFunctions;
35
36 public:
38 const bNodeTree &tree,
39 std::shared_ptr<const bNodeTree> shared_tree = nullptr);
40
45 void set_matching_fn(const mf::MultiFunction *fn);
46 void set_matching_fn(const mf::MultiFunction &fn);
47
52 template<typename T, typename... Args> void construct_and_set_matching_fn(Args &&...args);
53
60 template<typename Fn> void construct_and_set_matching_fn_cb(Fn &&create_multi_function);
61
62 const bNode &node();
63 const bNodeTree &tree();
64 const mf::MultiFunction &function();
65
71 const std::shared_ptr<const bNodeTree> &shared_tree();
72};
73
78 public:
79 struct Item {
80 const mf::MultiFunction *fn = nullptr;
81 std::shared_ptr<mf::MultiFunction> owned_fn;
82 };
83
84 private:
86
87 public:
88 NodeMultiFunctions(const bNodeTree &tree,
89 const std::shared_ptr<const bNodeTree> &shared_tree = nullptr);
90
91 const Item &try_get(const bNode &node) const;
92};
93
94/* -------------------------------------------------------------------- */
97
99 const bNode &node, const bNodeTree &tree, std::shared_ptr<const bNodeTree> shared_tree)
100 : node_(node), tree_(tree), shared_tree_(std::move(shared_tree))
101{
102}
103
105{
106 return node_;
107}
108
110{
111 return tree_;
112}
113
114inline const std::shared_ptr<const bNodeTree> &NodeMultiFunctionBuilder::shared_tree()
115{
116 return shared_tree_;
117}
118
119inline const mf::MultiFunction &NodeMultiFunctionBuilder::function()
120{
121 return *built_fn_;
122}
123
124inline void NodeMultiFunctionBuilder::set_matching_fn(const mf::MultiFunction *fn)
125{
126 built_fn_ = fn;
127}
128
129inline void NodeMultiFunctionBuilder::set_matching_fn(const mf::MultiFunction &fn)
130{
131 built_fn_ = &fn;
132}
133
134template<typename T, typename... Args>
136{
137 owned_built_fn_ = std::make_shared<T>(std::forward<Args>(args)...);
138 built_fn_ = &*owned_built_fn_;
139}
140
141template<typename Fn>
143{
144 using T = decltype(create_multi_function());
145 T *allocated_function = new T(create_multi_function());
146 owned_built_fn_ = std::shared_ptr<T>(allocated_function);
147 built_fn_ = &*owned_built_fn_;
148}
149
151
152/* -------------------------------------------------------------------- */
155
157{
158 static Item empty_item;
159 const Item *item = map_.lookup_ptr(&node);
160 if (item == nullptr) {
161 return empty_item;
162 }
163 return *item;
164}
165
167
168} // namespace blender::nodes
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
void set_matching_fn(const mf::MultiFunction *fn)
NodeMultiFunctionBuilder(const bNode &node, const bNodeTree &tree, std::shared_ptr< const bNodeTree > shared_tree=nullptr)
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
const std::shared_ptr< const bNodeTree > & shared_tree()
const Item & try_get(const bNode &node) const
NodeMultiFunctions(const bNodeTree &tree, const std::shared_ptr< const bNodeTree > &shared_tree=nullptr)
#define T
std::shared_ptr< mf::MultiFunction > owned_fn