HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OP_InputChangeHelper.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  * NAME: OP_InputChangeHelper.h (OP Library, C++)
7  *
8  * COMMENTS: Helper class for keeping track of whether node inputs have
9  * changed.
10  *
11  * HOW TO USE THIS CLASS
12  * =====================
13  *
14  * SETUP:
15  * - Override OP_Node::inputConnectChanged() to call
16  * OP_InputChangeHelper::inputConnectChanged() as well. Don't forget to
17  * delegate to the base class.
18  * - In the OP_Node::deleteCookedData() and OP_Node::bypassMe() overrides,
19  * call OP_InputChangeHelper::resetChangedSourceFlags(). Any other calls
20  * to delete the node's data should also call resetChangedSourceFlags().
21  *
22  * Now, when you want to mark an input as being used, call useInput(). To query
23  * if an input is dirty without clearing it, use checkChangedSourceFlags().
24  */
25 
26 #ifndef __OP_INPUTCHANGEHELPER_H__
27 #define __OP_INPUTCHANGEHELPER_H__
28 
29 #include "OP_API.h"
30 #include <UT/UT_Assert.h>
31 #include <UT/UT_IntArray.h>
32 #include "OP_Node.h"
33 
35 {
36 public:
37 
38  /// Mark all inputs as dirty. This is the default.
40  {
41  myDuplicateCounts.setCapacity(0);
42  }
43 
44  /// Mark the given input as dirty.
45  /// NOTE: Users of this class should override
46  /// OP_Node::inputConnectChanged() to call this.
47  void inputConnectChanged(const OP_Node &node, int which_input)
48  {
49  if (which_input >= 0)
50  setDuplicateCount(which_input, -1, node.maxInputs());
51  else
52  resetChangedSourceFlags();
53  }
54 
55  /// Use the given input on the node. This updates source flags to mark the
56  /// input as now clean, returning whether the input changes.
57  /// If force is true, then we treat the input as always dirty.
58  /// Returns false if input has errors and we'll add an input error in that
59  /// case.
60  bool useInput(const OP_Node &node, unsigned idx, bool &changed, bool force)
61  {
62  // checkChangedSourceFlags() returns if input has an error, "changed"
63  // is updated.
64  if (!checkChangedSourceFlags(node, idx, changed))
65  return false;
66 
67  if (force || changed)
68  {
69  OP_Node * src = node.getInput(idx);
70 
71  setDuplicateCount(idx, src->getCookCount(), node.maxInputs());
72 
73  changed = true; // we may have entered with (force == true)
74  }
75  // else not forced, and not changed
76 
77  return true;
78  }
79 
80  /// Similar to useInput() except it doesn't affect the flags. It's used to
81  /// simply query whether the given input has changed. Returns false if
82  /// there's errors on the input. "changed" is updated.
83  bool checkChangedSourceFlags(const OP_Node &node, unsigned idx,
84  bool &changed)
85  {
86  OP_Node * src;
87 
88  src = node.getInput(idx);
89  if (!src || src->error() >= UT_ERROR_ABORT)
90  {
91  changed = true;
92  return false;
93  }
94 
95  changed = (src->getCookCount() != getDuplicateCount(idx));
96  return true;
97  }
98 
99  int64 getMemoryUsage(bool inclusive) const
100  {
101  int64 mem = inclusive ? sizeof(*this) : 0;
102  mem += myDuplicateCounts.getMemoryUsage(false);
103  return mem;
104  }
105 
106 private:
107  int getDuplicateCount(int idx)
108  {
109  if (idx >= 0 && idx < myDuplicateCounts.entries())
110  return myDuplicateCounts(idx);
111  return -1;
112  }
113  void setDuplicateCount(int idx, int count, int max_inputs)
114  {
115  if (idx >= 0 && idx < max_inputs)
116  {
117  while (idx >= myDuplicateCounts.entries())
118  myDuplicateCounts.append(-1);
119  myDuplicateCounts(idx) = count;
120  }
121  else // clear all duplicate counts
122  {
123  UT_ASSERT(!"Untested code path");
124  myDuplicateCounts.entries(0);
125  }
126  }
127 
128 private:
129  UT_IntArray myDuplicateCounts;
130 };
131 
132 #endif // __OP_INPUTCHANGEHELPER_H__
void resetChangedSourceFlags()
Mark all inputs as dirty. This is the default.
virtual unsigned maxInputs() const
Maximum inputs that can be connected to a node.
bool useInput(const OP_Node &node, unsigned idx, bool &changed, bool force)
virtual OP_ERROR error(OP_Context &context)
void inputConnectChanged(const OP_Node &node, int which_input)
long long int64
Definition: SYS_Types.h:107
int getCookCount(void) const
Definition: OP_Node.h:1880
OP_Node * getInput(unsigned idx, bool mark_used=false) const
Returns the node connected to a particular input (may be null).
GLint GLsizei count
Definition: glcorearb.h:404
int64 getMemoryUsage(bool inclusive) const
bool checkChangedSourceFlags(const OP_Node &node, unsigned idx, bool &changed)
#define OP_API
Definition: OP_API.h:10
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:126
GLenum src
Definition: glcorearb.h:1792