Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NOD_multi_function.hh « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 21a94d9192b489eb5770582028290954de448c15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* SPDX-License-Identifier: GPL-2.0-or-later */

#pragma once

#include "FN_multi_function.hh"

#include "DNA_node_types.h"

#include "NOD_derived_node_tree.hh"

namespace blender::nodes {

using namespace fn::multi_function_types;

class NodeMultiFunctions;

/**
 * Utility class to help nodes build a multi-function for themselves.
 */
class NodeMultiFunctionBuilder : NonCopyable, NonMovable {
 private:
  const bNode &node_;
  const bNodeTree &tree_;
  std::shared_ptr<MultiFunction> owned_built_fn_;
  const MultiFunction *built_fn_ = nullptr;

  friend NodeMultiFunctions;

 public:
  NodeMultiFunctionBuilder(const bNode &node, const bNodeTree &tree);

  /**
   * Assign a multi-function for the current node. The input and output parameters of the function
   * have to match the available sockets in the node.
   */
  void set_matching_fn(const MultiFunction *fn);
  void set_matching_fn(const MultiFunction &fn);

  /**
   * Utility method for creating and assigning a multi-function when it can't have a static
   * lifetime.
   */
  template<typename T, typename... Args> void construct_and_set_matching_fn(Args &&...args);

  const bNode &node();
  const bNodeTree &tree();
};

/**
 * Gives access to multi-functions for all nodes in a node tree that support them.
 */
class NodeMultiFunctions {
 public:
  struct Item {
    const MultiFunction *fn = nullptr;
    std::shared_ptr<MultiFunction> owned_fn;
  };

 private:
  Map<const bNode *, Item> map_;

 public:
  NodeMultiFunctions(const DerivedNodeTree &tree);

  const Item &try_get(const DNode &node) const;
};

/* -------------------------------------------------------------------- */
/** \name #NodeMultiFunctionBuilder Inline Methods
 * \{ */

inline NodeMultiFunctionBuilder::NodeMultiFunctionBuilder(const bNode &node, const bNodeTree &tree)
    : node_(node), tree_(tree)
{
}

inline const bNode &NodeMultiFunctionBuilder::node()
{
  return node_;
}

inline const bNodeTree &NodeMultiFunctionBuilder::tree()
{
  return tree_;
}

inline void NodeMultiFunctionBuilder::set_matching_fn(const MultiFunction *fn)
{
  built_fn_ = fn;
}

inline void NodeMultiFunctionBuilder::set_matching_fn(const MultiFunction &fn)
{
  built_fn_ = &fn;
}

template<typename T, typename... Args>
inline void NodeMultiFunctionBuilder::construct_and_set_matching_fn(Args &&...args)
{
  owned_built_fn_ = std::make_shared<T>(std::forward<Args>(args)...);
  built_fn_ = &*owned_built_fn_;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name #NodeMultiFunctions Inline Methods
 * \{ */

inline const NodeMultiFunctions::Item &NodeMultiFunctions::try_get(const DNode &node) const
{
  static Item empty_item;
  const Item *item = map_.lookup_ptr(node.bnode());
  if (item == nullptr) {
    return empty_item;
  }
  return *item;
}

/** \} */

}  // namespace blender::nodes