HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDG_Port.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * COMMENTS:
7  */
8 
9 #ifndef __PDG_PORT_H__
10 #define __PDG_PORT_H__
11 
12 #include "PDG_API.h"
13 
14 #include "PDG_Conditional.h"
15 #include "PDG_EvaluationContext.h"
16 #include "PDG_Types.h"
17 
18 #include <PDGT/PDGT_Value.h>
19 
20 #include <PY/PY_CPythonAPI.h>
21 #include <PY/PY_CompiledCode.h>
22 
23 #include <UT/UT_Array.h>
24 #include <UT/UT_StringArray.h>
25 #include <UT/UT_StringHolder.h>
26 #include <UT/UT_StringMap.h>
27 #include <UT/UT_VectorTypes.h>
28 #include <UT/UT_WorkBuffer.h>
29 
30 #include <type_traits>
31 
32 class PDG_Node;
33 class PDG_NodeInterface;
35 
37 {
38 public:
40  {
41  pdg_Connection(PDG_Port* port, bool is_ref)
42  : myConnectedPort(port)
43  , myIsReference(is_ref)
44  {
45  }
46 
49  };
50 
53 
54 public:
56  const PDG_WorkItemDataType* data_type,
57  PDG_PortType portType,
58  const UT_StringHolder& name,
59  const UT_StringHolder& label,
60  int size,
61  bool ordered = true,
62  bool custom = false,
63  bool builtin = false);
64 
65  virtual ~PDG_Port();
66 
67  int64 getMemoryUsage(bool inclusive) const;
68 
69  void addTag(const UT_StringHolder& tag);
70 
71  bool connect(
72  PDG_Port* port,
73  bool reference,
74  bool inverse = true);
75  bool disconnect(PDG_Port* port, bool inverse = true);
76  bool disconnectAll();
77 
78  bool isConnected();
79  bool canConnectTo(PDG_Port* port) const;
80  bool isConnectedTo(
81  PDG_Port* port,
82  bool reference) const;
83 
84  void connectedNodes(
85  PDG_NodeArray& nodes,
86  bool filter_bypassed,
87  bool filter_reference) const;
88  int connectionCount(
89  bool filter_bypassed,
90  bool filter_reference) const;
91  const ConnectionArray& connections() const;
92  void connectedPorts(
93  PDG_PortArray& ports,
94  bool filter_bypassed,
95  bool filter_reference) const;
96 
97  PDG_Node* node() const;
98 
99  const PDG_WorkItemDataType* workItemDataType() const;
100  void setWorkItemDataType(
101  const PDG_WorkItemDataType* data_type);
102  PDG_PortType portType() const;
103 
104  PDGT_Value* value(int index);
105  const PDGT_Value* value(int index) const;
106  PDGT_Value* defaultValue(int index);
107  const PDGT_Value* defaultValue(int index) const;
108 
109  void setExpression(int index,
110  const UT_StringHolder& expression,
111  bool multi);
112  void setIsAppParm(int index, bool app_parm);
113  const UT_StringHolder& expression(int index) const;
114  bool isAppParm(int index) const;
115  const ExpressionVars& expressionVars() const;
116  bool hasExpressionVars() const;
117  bool addExpressionVar(const UT_StringHolder& var_name,
118  const UT_StringHolder& var_type);
119  void setExpressionVars(const ExpressionVars& vars);
120 
121  bool multiExpression(int index) const;
122  bool hasExpression(int index) const;
123 
124  bool isEnabled() const;
125  void setIsEnabled(bool enabled);
126 
127  bool hasWorkItemExpression() const;
128  const UT_StringArray& expressionAttribs() const;
129  void resetExpressionAttribs();
130  void setExpressionAttribs(const UT_StringArray& attribs);
131 
132  int evaluationOrigin() const;
133  void setEvaluationOrigin(int origin);
134 
135  void setDisplayType(int display_type);
136  void addDisplayType(PDG_PortDisplayType display_type);
137  int displayType() const;
138  bool hasDisplayType(PDG_PortDisplayType display_type);
139 
140  void setDisplayMenuList(const UT_StringArray& list);
141  const UT_StringArray& displayMenuList() const;
142 
143  void setDisplayMenuScript(
144  const UT_StringHolder& script);
145  const UT_StringHolder& displayMenuScript() const;
146 
147  const UT_StringArray& tags() const;
148  const UT_StringHolder& name() const;
149  const UT_StringHolder& label() const;
150 
151  const PDG_Conditional& cookCondition() const;
152  void setCookCondition(const UT_StringHolder& pattern);
153 
154  const UT_StringHolder& disableCondition() const;
155  void setDisableCondition(
156  const UT_StringHolder& condition);
157 
158  bool hasTag(const UT_StringHolder& tag) const;
159 
160  void setDataType(PDGT_Value::DataType type);
162 
163  UT_StringArray resultTypeTags(bool strip_prefix) const;
164 
165  bool isOrdered() const;
166  bool isCustom() const;
167  bool isBuiltin() const;
168  int size() const;
169 
170  UT_StringHolder dataTypeAsString() const;
171 
172  static void splitName(const UT_StringHolder& full_name,
173  UT_StringHolder& node,
174  UT_StringHolder& port);
175 
176  bool evaluate(
177  int index,
178  fpreal& result,
179  const PDG_WorkItem* work_item,
180  UT_WorkBuffer& errors) const;
181  bool evaluate(
183  const PDG_WorkItem* work_item,
184  UT_WorkBuffer& errors) const;
185 
186  bool evaluate(
187  int index,
188  exint& result,
189  const PDG_WorkItem* work_item,
190  UT_WorkBuffer& errors) const;
191  bool evaluate(
192  int index,
193  bool& result,
194  const PDG_WorkItem* work_item,
195  UT_WorkBuffer& errors) const;
197  bool evaluate(
198  int index,
199  T& result,
200  const PDG_WorkItem* work_item,
201  UT_WorkBuffer& errors)
202  {
203  exint temp;
204  bool eval = evaluate(
205  index, temp, work_item, errors);
206 
207  if (eval)
208  {
209  result = (T)(temp);
210  return true;
211  }
212 
213  return false;
214  }
215 
216  bool evaluate(
218  const PDG_WorkItem* work_item,
219  UT_WorkBuffer& errors) const;
220 
221  bool evaluate(
222  int index,
224  const PDG_WorkItem* work_item,
225  UT_WorkBuffer& errors) const;
226  bool evaluate(
228  const PDG_WorkItem* work_item,
229  UT_WorkBuffer& errors) const;
230 
231  bool evaluateRaw(
232  int index,
234  const PDG_WorkItem* work_item,
235  UT_WorkBuffer& errors) const;
236  bool evaluateRaw(
238  const PDG_WorkItem* work_item,
239  UT_WorkBuffer& errors) const;
240 
241 
242  UT_StringHolder expressionFile(int index) const;
243 
244 private:
245  UT_StringHolder postReplace(const UT_StringHolder& input,
246  const PDG_WorkItem* work_item,
247  bool always_replace) const;
248  exint postReplace(exint input,
249  const PDG_WorkItem* work_item,
250  bool always_replace) const;
251  fpreal postReplace(fpreal input,
252  const PDG_WorkItem* work_item,
253  bool always_replace) const;
254 
255  bool appEval(const UT_StringHolder& name,
256  int index,
257  bool expand,
258  fpreal& result,
259  const PDG_WorkItem* work_item,
260  int thread,
261  UT_WorkBuffer& errors) const;
262  bool appEval(const UT_StringHolder& name,
263  int index,
264  bool expand,
265  exint& result,
266  const PDG_WorkItem* work_item,
267  int thread,
268  UT_WorkBuffer& errors) const;
269  bool appEval(const UT_StringHolder& name,
270  int index,
271  bool expand,
273  const PDG_WorkItem* work_item,
274  int thread,
275  UT_WorkBuffer& errors) const;
276 
277  bool canEvaluate(int index,
278  UT_WorkBuffer& errors) const;
279  bool isParent(const PDG_WorkItem* work_item) const;
280 
281  template <typename T>
282  bool evaluateTyped(int index,
283  bool expand,
284  T& result,
285  const PDG_WorkItem* work_item,
286  UT_WorkBuffer& errors) const
287  {
288  if (index < 0)
289  index += myPortValues.size();
290 
291  if (!canEvaluate(index, errors))
292  return false;
293 
294  const pdg_PortValue& val = myPortValues[index];
295  int thread = SYSgetSTID();
296  T temp;
297 
298  // set the scope parent state to true if we want to use
299  // a workitem that is not from this node.
301  thread, work_item, isParent(work_item));
302  const PDG_WorkItem* local_item =
303  PDG_EvaluationContext::activeWorkItem(thread);
304 
305  if (val.isAppParm())
306  {
307  if (!appEval(
308  myName, index, expand, result, local_item, thread, errors))
309  {
311  return false;
312  }
313 
314  return true;
315  }
316 
317  if (!val.hasExpression())
318  {
319  val.myValue.value(temp);
320  result = postReplace(temp, local_item, true);
321  return true;
322  }
323 
324  if (PDG_EvaluationContext::evaluate(thread, *val.myPyExpression,
325  temp, errors) != PDG_EvaluationContext::eResultSuccess)
326  {
327  errors.appendFormat(" ({})", expressionFile(index));
328  return false;
329  }
330 
331  result = postReplace(temp, local_item, true);
332  return true;
333  }
334 
335  template <typename T>
336  bool evaluateArray(bool expand,
337  UT_Array<T>& result,
338  const PDG_WorkItem* work_item,
339  UT_WorkBuffer& errors) const
340  {
341  if (myPortType != PDG_PortType::eParameter)
342  {
343  errors.appendFormat("Port '{}' is not a parameter", myName);
344  return false;
345  }
346 
347  int thread = SYSgetSTID();
349  thread, work_item, isParent(work_item));
350  const PDG_WorkItem* local_item =
352 
353  int index = 0;
354  for (auto&& port_val : myPortValues)
355  {
356  T temp;
357  if (port_val.isAppParm())
358  {
359  if (!appEval(myName, index, expand, temp,
360  local_item, thread, errors))
361  {
363  thread, true);
364  return false;
365  }
366  }
367  else if (!port_val.hasExpression())
368  port_val.myValue.value(temp);
369  else
370  {
372  thread,
373  *port_val.myPyExpression,
374  temp, errors) != PDG_EvaluationContext::eResultSuccess)
375  {
376  errors.appendFormat(" ({})", expressionFile(result.size()));
377  return false;
378  }
379  }
380  result.append(postReplace(temp, local_item, true));
381  index++;
382  }
383 
384  return true;
385  }
386 
387 private:
388  struct pdg_PortValue
389  {
390  pdg_PortValue()
391  : myPyExpression(nullptr)
392  , myMultiExpression(false)
393  , myIsAppParm(false)
394  {
395  }
396 
397  ~pdg_PortValue()
398  {
399  if (myPyExpression && PY_Py_IsInitialized())
400  delete myPyExpression;
401  }
402 
403  int64 getMemoryUsage(bool inclusive) const
404  {
405  int64 mem = inclusive ? sizeof(*this) : 0;
406  mem += myExpression.getMemoryUsage(false);
407  if (myPyExpression)
408  mem += myPyExpression->getMemoryUsage(true);
409  return mem;
410  }
411 
412  inline bool hasExpression() const { return myExpression.length() > 0; }
413  inline bool isAppParm() const { return myIsAppParm; }
414 
415  PDGT_Value myValue;
416  UT_StringHolder myExpression;
417  PY_CompiledCode* myPyExpression;
418 
419  bool myMultiExpression;
420  bool myIsAppParm;
421  };
422 
423  PDG_NodeInterface* myOwner;
424  ConnectionArray myConnections;
425  const PDG_WorkItemDataType* myWorkItemDataType;
426 
427  PDG_Conditional myCookCondition;
428 
429  PDG_PortType myPortType;
430  UT_StringHolder myName;
431  UT_StringHolder myLabel;
432  UT_StringHolder myDisableCondition;
433 
434  UT_Array<pdg_PortValue> myPortValues;
435 
436  UT_StringArray myTags;
437  UT_StringArray myExpressionAttribs;
438  ExpressionVars myExpressionVars;
439 
440  int mySize;
441  int myEvaluationOrigin;
442 
443  bool myEnabledFlag;
444  bool myOrderedFlag;
445  bool myCustomFlag;
446  bool myBuiltinFlag;
447  bool myHasExpressionVars;
448 
449  PDGT_Value::DataType myDataType;
450  int myDisplayType;
451  UT_StringArray myDisplayMenuList;
452  UT_StringHolder myDisplayMenuScript;
453 };
454 
455 #endif /* __PDG_PORT_H__ */
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
GLsizei const GLfloat * value
Definition: glcorearb.h:824
#define PDG_API
Definition: PDG_API.h:23
int64 exint
Definition: SYS_Types.h:125
PDG_Port * myConnectedPort
Definition: PDG_Port.h:47
static void updateParameterEvaluation(int thread, bool has_error)
Updates parameter error state.
GLenum GLenum GLsizei const GLuint GLboolean enabled
Definition: glcorearb.h:2539
**But if you need a result
Definition: thread.h:613
exint size() const
Definition: UT_Array.h:646
Scoped setter of local work item.
size_t appendFormat(const char *fmt, const Args &...args)
static const PDG_WorkItem * activeWorkItem(int thread)
The script succeeded without any exceptions.
PDG_PortType
Enumeration of node port types.
Definition: PDG_Types.h:347
long long int64
Definition: SYS_Types.h:116
static EvaluationResult evaluate(int thread, const PY_CompiledCode &expression, fpreal &result, UT_WorkBuffer &errors)
Primitive Python eval methods (loat, int, string)
HUSD_API bool eval(VtValue &val, T &ret_val)
GLuint const GLchar * name
Definition: glcorearb.h:786
GLushort pattern
Definition: glad.h:2583
exint append()
Definition: UT_Array.h:142
GLsizeiptr size
Definition: glcorearb.h:664
**Note that the tasks the is the thread number *for the or if it s being executed by a non pool thread(this *can happen in cases where the whole pool is occupied and the calling *thread contributes to running the work load).**Thread pool.Have fun
SYS_API int SYSgetSTID()
pdg_Connection(PDG_Port *port, bool is_ref)
Definition: PDG_Port.h:41
fpreal64 fpreal
Definition: SYS_Types.h:277
GLuint index
Definition: glcorearb.h:786
GLuint GLfloat * val
Definition: glcorearb.h:1608
bool evaluate(int index, T &result, const PDG_WorkItem *work_item, UT_WorkBuffer &errors)
Definition: PDG_Port.h:197
PDG_PortDisplayType
Definition: PDG_Types.h:322
type
Definition: core.h:1059
HUSD_API const char * dataType()