Blender  V2.93
node.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "graph/node_type.h"
20 
21 #include "util/util_array.h"
22 #include "util/util_map.h"
23 #include "util/util_param.h"
24 
26 
27 class MD5Hash;
28 struct Node;
29 struct NodeType;
30 struct Transform;
31 
32 /* Note: in the following macros we use "type const &" instead of "const type &"
33  * to avoid issues when pasting a pointer type. */
34 #define NODE_SOCKET_API_BASE_METHODS(type_, name, string_name) \
35  const SocketType *get_##name##_socket() const \
36  { \
37  static const SocketType *socket = type->find_input(ustring(string_name)); \
38  return socket; \
39  } \
40  bool name##_is_modified() const \
41  { \
42  const SocketType *socket = get_##name##_socket(); \
43  return socket_is_modified(*socket); \
44  } \
45  void tag_##name##_modified() \
46  { \
47  const SocketType *socket = get_##name##_socket(); \
48  socket_modified |= socket->modified_flag_bit; \
49  } \
50  type_ const &get_##name() const \
51  { \
52  const SocketType *socket = get_##name##_socket(); \
53  return get_socket_value<type_>(this, *socket); \
54  }
55 
56 #define NODE_SOCKET_API_BASE(type_, name, string_name) \
57  protected: \
58  type_ name; \
59 \
60  public: \
61  NODE_SOCKET_API_BASE_METHODS(type_, name, string_name)
62 
63 #define NODE_SOCKET_API(type_, name) \
64  NODE_SOCKET_API_BASE(type_, name, #name) \
65  void set_##name(type_ value) \
66  { \
67  const SocketType *socket = get_##name##_socket(); \
68  this->set(*socket, value); \
69  }
70 
71 #define NODE_SOCKET_API_ARRAY(type_, name) \
72  NODE_SOCKET_API_BASE(type_, name, #name) \
73  void set_##name(type_ &value) \
74  { \
75  const SocketType *socket = get_##name##_socket(); \
76  this->set(*socket, value); \
77  } \
78  type_ &get_##name() \
79  { \
80  const SocketType *socket = get_##name##_socket(); \
81  return get_socket_value<type_>(this, *socket); \
82  }
83 
84 #define NODE_SOCKET_API_STRUCT_MEMBER(type_, name, member) \
85  NODE_SOCKET_API_BASE_METHODS(type_, name##_##member, #name "." #member) \
86  void set_##name##_##member(type_ value) \
87  { \
88  const SocketType *socket = get_##name##_##member##_socket(); \
89  this->set(*socket, value); \
90  }
91 
92 /* Node */
93 
94 struct NodeOwner {
95  virtual ~NodeOwner();
96 };
97 
98 struct Node {
99  explicit Node(const NodeType *type, ustring name = ustring());
100  virtual ~Node() = 0;
101 
102  /* set values */
103  void set(const SocketType &input, bool value);
104  void set(const SocketType &input, int value);
105  void set(const SocketType &input, uint value);
106  void set(const SocketType &input, float value);
107  void set(const SocketType &input, float2 value);
108  void set(const SocketType &input, float3 value);
109  void set(const SocketType &input, const char *value);
110  void set(const SocketType &input, ustring value);
111  void set(const SocketType &input, const Transform &value);
112  void set(const SocketType &input, Node *value);
113 
114  /* set array values. the memory from the input array will taken over
115  * by the node and the input array will be empty after return */
116  void set(const SocketType &input, array<bool> &value);
117  void set(const SocketType &input, array<int> &value);
118  void set(const SocketType &input, array<float> &value);
119  void set(const SocketType &input, array<float2> &value);
120  void set(const SocketType &input, array<float3> &value);
121  void set(const SocketType &input, array<ustring> &value);
122  void set(const SocketType &input, array<Transform> &value);
123  void set(const SocketType &input, array<Node *> &value);
124 
125  /* get values */
126  bool get_bool(const SocketType &input) const;
127  int get_int(const SocketType &input) const;
128  uint get_uint(const SocketType &input) const;
129  float get_float(const SocketType &input) const;
130  float2 get_float2(const SocketType &input) const;
131  float3 get_float3(const SocketType &input) const;
132  ustring get_string(const SocketType &input) const;
133  Transform get_transform(const SocketType &input) const;
134  Node *get_node(const SocketType &input) const;
135 
136  /* get array values */
137  const array<bool> &get_bool_array(const SocketType &input) const;
138  const array<int> &get_int_array(const SocketType &input) const;
139  const array<float> &get_float_array(const SocketType &input) const;
140  const array<float2> &get_float2_array(const SocketType &input) const;
141  const array<float3> &get_float3_array(const SocketType &input) const;
142  const array<ustring> &get_string_array(const SocketType &input) const;
143  const array<Transform> &get_transform_array(const SocketType &input) const;
144  const array<Node *> &get_node_array(const SocketType &input) const;
145 
146  /* generic values operations */
147  bool has_default_value(const SocketType &input) const;
148  void set_default_value(const SocketType &input);
149  bool equals_value(const Node &other, const SocketType &input) const;
150  void copy_value(const SocketType &input, const Node &other, const SocketType &other_input);
151  void set_value(const SocketType &input, const Node &other, const SocketType &other_input);
152 
153  /* equals */
154  bool equals(const Node &other) const;
155 
156  /* compute hash of node and its socket values */
157  void hash(MD5Hash &md5);
158 
159  /* Get total size of this node. */
160  size_t get_total_size_in_bytes() const;
161 
162  /* Type testing, taking into account base classes. */
163  bool is_a(const NodeType *type);
164 
165  bool socket_is_modified(const SocketType &input) const;
166 
167  bool is_modified();
168 
169  void tag_modified();
170  void clear_modified();
171 
172  void print_modified_sockets() const;
173 
174  ustring name;
175  const NodeType *type;
176 
177  const NodeOwner *get_owner() const;
178  void set_owner(const NodeOwner *owner_);
179 
180  protected:
181  const NodeOwner *owner;
182 
183  template<typename T> static T &get_socket_value(const Node *node, const SocketType &socket)
184  {
185  return (T &)*(((char *)node) + socket.struct_offset);
186  }
187 
189 
190  template<typename T> void set_if_different(const SocketType &input, T value);
191 
192  template<typename T> void set_if_different(const SocketType &input, array<T> &value);
193 };
194 
unsigned int uint
Definition: BLI_sys_types.h:83
OperationNode * node
#define CCL_NAMESPACE_END
#define T
uint64_t SocketModifiedFlags
Definition: node_type.h:29
Definition: node.h:94
virtual ~NodeOwner()
Definition: node.cpp:29
Definition: node.h:98
bool has_default_value(const SocketType &input) const
Definition: node.cpp:307
const array< float3 > & get_float3_array(const SocketType &input) const
Definition: node.cpp:281
bool is_modified()
Definition: node.cpp:781
static T & get_socket_value(const Node *node, const SocketType &socket)
Definition: node.h:183
bool equals(const Node &other) const
Definition: node.cpp:546
const array< float > & get_float_array(const SocketType &input) const
Definition: node.cpp:269
const array< int > & get_int_array(const SocketType &input) const
Definition: node.cpp:263
const NodeOwner * owner
Definition: node.h:181
float get_float(const SocketType &input) const
Definition: node.cpp:210
Transform get_transform(const SocketType &input) const
Definition: node.cpp:244
void set(const SocketType &input, bool value)
Definition: node.cpp:70
const NodeType * type
Definition: node.h:175
void set_value(const SocketType &input, const Node &other, const SocketType &other_input)
Definition: node.cpp:385
float3 get_float3(const SocketType &input) const
Definition: node.cpp:222
void set_default_value(const SocketType &input)
Definition: node.cpp:314
const array< bool > & get_bool_array(const SocketType &input) const
Definition: node.cpp:257
void copy_value(const SocketType &input, const Node &other, const SocketType &other_input)
Definition: node.cpp:334
const array< Node * > & get_node_array(const SocketType &input) const
Definition: node.cpp:299
ustring name
Definition: node.h:174
size_t get_total_size_in_bytes() const
Definition: node.cpp:692
SocketModifiedFlags socket_modified
Definition: node.h:188
bool get_bool(const SocketType &input) const
Definition: node.cpp:192
float2 get_float2(const SocketType &input) const
Definition: node.cpp:216
void clear_modified()
Definition: node.cpp:791
const array< ustring > & get_string_array(const SocketType &input) const
Definition: node.cpp:287
const array< float2 > & get_float2_array(const SocketType &input) const
Definition: node.cpp:275
void hash(MD5Hash &md5)
Definition: node.cpp:592
bool socket_is_modified(const SocketType &input) const
Definition: node.cpp:776
ustring get_string(const SocketType &input) const
Definition: node.cpp:228
bool is_a(const NodeType *type)
Definition: node.cpp:755
void set_if_different(const SocketType &input, T value)
Definition: node.cpp:796
Node * get_node(const SocketType &input) const
Definition: node.cpp:250
virtual ~Node()=0
Definition: node.cpp:51
const NodeOwner * get_owner() const
Definition: node.cpp:765
void tag_modified()
Definition: node.cpp:786
uint get_uint(const SocketType &input) const
Definition: node.cpp:204
Node(const NodeType *type, ustring name=ustring())
Definition: node.cpp:33
int get_int(const SocketType &input) const
Definition: node.cpp:198
const array< Transform > & get_transform_array(const SocketType &input) const
Definition: node.cpp:293
bool equals_value(const Node &other, const SocketType &input) const
Definition: node.cpp:482
void print_modified_sockets() const
Definition: node.cpp:818
void set_owner(const NodeOwner *owner_)
Definition: node.cpp:770
int struct_offset
Definition: node_type.h:87