HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OP_OperatorTable.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_OperatorTable.h (C++)
7  *
8  * COMMENTS: This class is simply a symbol table of OP_Operator's.
9  *
10  */
11 
12 #ifndef __OP_OperatorTable_h__
13 #define __OP_OperatorTable_h__
14 
15 #include "OP_API.h"
16 #include "OP_OTLDefinition.h"
17 #include "OP_Operator.h"
19 #include <UT/UT_NonCopyable.h>
20 #include <UT/UT_String.h>
21 #include <UT/UT_StringMap.h>
22 #include <UT/UT_SymbolTable.h>
23 #include <UT/UT_ValArray.h>
24 #include <UT/UT_Color.h>
25 #include <iosfwd>
26 
27 class OP_Node;
28 class OP_Network;
29 class OP_OperatorTable;
30 class OP_ScriptOperator;
31 class OP_ScriptIndexFile;
33 
36 typedef OP_ScriptOperator *(*OP_ScriptCreatorFunc)
37  (const OP_OTLDefinition &definition);
38 typedef bool (*OP_ScriptCanReuseFunc)
39  (const OP_ScriptOperator *script_operator,
40  const OP_OTLDefinition &new_definition);
41 
43 {
44 public:
45  OP_OperatorTable(const char *table_name, const char *script_directory);
47 
48  static int getAllOperatorTables(OP_OperatorTableList &list);
49 
50  /// Creates a new node of a given type inside a parent and names it
51  /// as a give name.
52  /// @param exact_type If true, the operator name parameter 'type',
53  /// is used verbatim to lookup the operator. Otherwise,
54  /// a preferred operator name that matches 'type'
55  /// specification is found first and then is used to lookup
56  /// the operator. For example, "hda" may match "hda::2.0",
57  /// which is the preferred operator definition.
58  OP_Node *createNode(OP_Network *parent, const char *type,
59  const char *name = 0,
60  int *aliasedScriptedOp = 0,
61  bool exact_type = false);
62 
63  OP_Operator *getOperator(const char *name) const;
64  bool addOperator(OP_Operator *op,
65  std::ostream *err = nullptr);
66  void removeOperator(OP_Operator *op);
67 
69  { return myPrimarySubnet; }
70 
71  void setScriptCreator(OP_ScriptCreatorFunc creator_func,
72  OP_ScriptCanReuseFunc reuse_func);
73  void addScriptIndexFile(const char *indexpath,
74  const char *indexfile,
75  const char *classid,
76  const char *extrainfo,
77  int defaultMinInputs,
78  int defaultMaxInputs,
79  bool issubnet,
80  bool ispython);
81  bool loadScriptOperator(const char *opname, UT_IStream &is,
82  OP_OTLLibrary *addToLib);
83  bool addOperatorIfNew(const OP_OTLDefinition &definition);
84  bool canReuseOperator(
85  const OP_OTLDefinition &new_definition,
86  const OP_Operator *op = 0) const;
87 
88  void requestReload();
89 
90  // Runs the DSO installation function on our currently loaded dso's inthe
91  // dso search path.
92  void runDSOInstall();
93 
94  // Loads the given dso file and runs the installation function
95  bool loadDSO(const char *dso_file);
96 
97  int entries() const
98  { return myOpCount; }
99 
100  void getOperatorList(UT_StringArray &list,
101  bool english=true) const;
102  int getOperators(OP_OperatorList &list,
103  OP_Network *net = 0,
104  bool filterhidden = false) const;
105 
106  // This returns the optype id for the given table.
107  OP_OpTypeId getOpTypeID() const;
108 
109  // This returns a unique integer for each OP_OperatorTable created
110  int getUniqueOpTypeID() const
111  { return myUniqueOpTypeID; }
112  // Before sorting, you should getOperators(list);
113  void sortOperators(OP_OperatorList &list,
116 
117  const UT_String &getName() const
118  { return myName; }
119  const UT_String &getScriptPath() const
120  { return myScriptDirectory; }
121 
122  unsigned getOperatorStatus(OP_Operator *op) const;
123  unsigned getStatus() const;
124  void setDefaultType(const char *type);
125  const char *getDefaultType() const;
126 
127  // Add an operator alias (from the OPcustomize file).
128  bool setOpRename(const char *optype, const char *newname,
129  std::ostream *err = nullptr);
130  // Display all the oprename commands.
131  void outputOpRenames(std::ostream &os) const;
132 
133  // Add an operator alias (from the OPcustomize file).
134  bool setOpAlias(const char *optype,
135  const char *alias,
136  std::ostream *err = nullptr);
137 
138  // Sets what the inital default name of the op will be.
139  // null first name will disable, reverting to the usual rules.
140  bool setOpFirstName(const char *type,
141  const char *firstname,
142  std::ostream *err = nullptr);
143  // Display all the opalias commands.
144  void outputOpAliases(std::ostream &os) const;
145  // Display all the firstname commands.
146  void outputOpFirstNames(std::ostream &os) const;
147  // Get all the aliases for a particular operator type.
148  void getOpAliases(const char *opname,
149  UT_StringArray &opaliases) const;
150  // Get the operator type represented by the supplied alias (if any).
151  const char *getOpFromAlias(const char *alias) const;
152 
153  // Add an operator override (from the OPcustomize file).
154  bool setOpOverride(const char *optype,
155  const char *override,
156  bool keep_parms, bool keep_contents,
157  std::ostream *err = nullptr);
158  // Add an operator override (from the OPcustomize file).
159  bool clearOpOverride(const char *optype,
160  std::ostream *err = nullptr);
161  // Display all the opoverride commands.
162  void outputOpOverrides(std::ostream &os) const;
163  // Get all the overridees for a particular operator type.
164  void getOpOverrides(const char *opname,
165  UT_StringArray &opoverridees) const;
166 
167  // Add an operator to the excluded op list (from the OPcustomize file).
168  bool addOpExcluded(const char *opname,
169  std::ostream *err = nullptr);
170  // Is operator excluded?
171  bool isOpExcluded(const char *opname) const;
172  // Display all the opexclude commands.
173  void outputOpExcluded(std::ostream &os) const;
174  // Add an operator to the hidden op list (from the OPcustomize file).
175  bool addOpHidden(const char *opname,
176  std::ostream *err = nullptr);
177  // Delete an operator from the hidden op list (from the OPcustomize file).
178  void delOpHidden(const char *opname);
179  // Is operator hidden?
180  bool isOpHidden(const char *opname) const;
181  // Get all hidden ops.
182  void getOpHidden(UT_StringArray &opnames) const;
183 
184  // Is operator experimental?
185  bool isOpExperimental(const UT_StringRef &opname) const;
186  // Add operator to experimental list
187  bool addOpExperimental(const char *opname,
188  std::ostream *err = nullptr);
189 
190  // Mark the operator as deprecated.
191  bool setOpDeprecated(const char *opname,
192  const char *version,
193  const char *replacement = 0,
194  std::ostream *err = nullptr);
195  // Is the operator deprecated?
196  bool isOpDeprecated(const char *opname) const;
197 
198  // Retrieve the deprecation version and possible replacement operator.
199  bool getDeprecationInfo(const char *opname,
201  UT_StringHolder &replacement) const;
202  // Display all the opdeprecate commands.
203  void outputOpDeprecated(std::ostream &os) const;
204 
205  // Sets the default color for an operator type.
206  bool setOpDefaultColor(const UT_StringHolder &optype,
207  const UT_Color &clr,
208  std::ostream *err = nullptr);
209  // Gets the default color for the supplied operator type.
210  UT_Color getOpDefaultColor(const UT_StringRef &optype) const;
211  // Determine if we have a optype specific default color
212  bool hasSpecificOpDefaultColor(
213  const UT_StringRef &optype) const;
214  // Clear all the opdefaultcolor commands.
215  void clearOpDefaultColors();
216  // Display all the opdefaultcolor commands.
217  void outputOpDefaultColors(std::ostream &os) const;
218 
219  // Sets the default shape for an operator type.
220  bool setOpDefaultShape(const UT_StringHolder &optype,
221  const UT_StringHolder &shape,
222  std::ostream *err = nullptr);
223  // Gets the default shape for the supplied operator type.
224  const UT_StringHolder &getOpDefaultShape(const UT_StringRef &optype) const;
225  // Determine if we have a optype specific default shape
226  bool hasSpecificOpDefaultShape(
227  const UT_StringRef &optype) const;
228  // Clear all the opdefaultshape commands.
229  void clearOpDefaultShapes();
230  // Display all the opdefaultshape commands.
231  void outputOpDefaultShapes(std::ostream &os) const;
232 
233  // Get or set the wire style for networks with this child node type.
234  const UT_StringHolder &getDefaultWireStyle() const;
235  void setDefaultWireStyle(const UT_StringHolder &wirestyle);
236 
237  // When no name is specified for a node, we typically use the type as the
238  // prefix for the name. However, we may want to provide a method for
239  // creating alternate default names for operators.
240  void getDefaultNodeName(const char *type, UT_String &name);
241 
242  // If there is an index file that contains scripted subnets for this op
243  // table, this function returns a pointer to that file name.
244  const char *getScriptedSubnetIndex() const;
245 
246  // Notify those who are interested that our table contents have changed.
247  // Don't do this in addOperator - only after bulk operations like
248  // loadScriptOperators or OP_Netowkr::loadScriptedOpDefs.
249  void notifyUpdateTableSinksOfUpdate();
250 
251  static inline int getLoadDSOFlag()
252  { return theLoadDSOFlag; }
253  static inline void setLoadDSOFlag(int i)
254  { theLoadDSOFlag = i; }
255  static unsigned getPermissionMask(const OP_Operator *op);
256  static void clearPermissionMask(const OP_Operator *op);
257 
258  /// Called once all basic operator types are loaded to call the python
259  /// code which will initialize node color and shape themes.
260  static void initializeOpThemes();
261 
262  /// Builds (or rebuilds) the operator type namespace hierarchy.
263  /// The optype precedense is given by the environment variable
264  /// HOUDINI_OPTYPE_NAMESPACE_HIERARCHY, which is processed by this method.
265  static void buildOpTypeNamespaceHierarchy();
266 
267  /// Obtains the value of the environment variable used to construct
268  /// the hierarchy.
269  static const char *getOpTypeNamespaceHierarchyPref();
270 
271  /// Obtains a list of available operator names that have the same base
272  /// (core) name as the given operator.
273  /// If scope network name is not NULL, the list includes only operators
274  /// whose nodes can be created in that network (otherwise all
275  /// operators are included).
276  /// The list is sorted according to the descending precedence order.
277  void getCandidateOperatorNamesInPrecedenceOrder(
278  UT_StringArray &precedence_order,
279  const char *op_name,
280  const UT_StringArray *scope_network_stack);
281 
282  /// Obtains the preferred operator name that matches the given op_name.
283  /// Any name component included in the op_name must match the returned op
284  /// type name, and any component not present in op_name is assumed to match
285  /// the returned op type. For example 'hda' will match any scope, namespace,
286  /// or version, while 'userA::hda' will match any scope and version, but the
287  /// namespace must be 'userA'. For global namespace use '::hda' and for
288  /// versionless opname use 'hda::'.
289  /// If the scope_network_stack is also given (ie, non-null) then the
290  /// returned opname must match one of the scopes listed in that array too.
291  /// Returns the name of the highest precedence operator that matches
292  /// the given op_name.
293  const char *getPreferredOperatorName(const char *opname,
294  const UT_StringArray *scope_network_stack);
295 
296  /// Returns true if the provided node name is "close enough" to the
297  /// operator type name, english name, or first name to imply what the
298  /// operator type is.
299  bool nodeNameImpliesType(OP_Operator *op,
300  const UT_String &node_name) const;
301 
302  /// Convenience functions for the second callback in setScriptCreator().
303  static bool alwaysReuseScriptOperatorCallback(
304  const OP_ScriptOperator *script_operator,
305  const OP_OTLDefinition &new_definition);
306  static bool neverReuseScriptOperatorCallback(
307  const OP_ScriptOperator *script_operator,
308  const OP_OTLDefinition &new_definition);
309 
310 private:
311  // Functions for adding or removing a table sink.
312  void addUpdateTableSink(OP_UpdateTableSink *sink);
313  void removeUpdateTableSink(OP_UpdateTableSink *sink);
314  // Called when the operator is deleted.
315  void notifyUpdateTableSinksOfDelete();
316 
317  // Functions for adding new operator types using an OP_OTLDefinition.
318  OP_ScriptOperator *addNewOperator(const OP_OTLDefinition &definition,
319  std::ostream *err = nullptr);
320  int loadScriptIndexFile(OP_ScriptIndexFile &sif,
321  bool checkdup);
322  /// Returns the table's operator namespace hierarchy (ie, the hierarchy
323  /// of operator definitions for the optype associated with this table).
324  OP_OpNamespaceHierarchy &getOpNamespaceHierarchy()
325  { return myOpNamespaceHierarchy; }
326 
327  /// Returns the operator named 'opname' for instantiating a node
328  /// inside the parent. If 'opname' is an ambiguous and unqualified opname
329  /// (ie core name witouth namespace), the preferred operator matching
330  /// that opname is returned. However when loading a network or if
331  /// the exact_name flag is true, then an operator with the exact 'opname'
332  /// is returned (or NULL).
333  OP_Operator *getPreferredOperator(OP_Network *parent,
334  const char *opname, bool exact_opname);
335 
336  UT_String myName;
337  UT_String myScriptDirectory;
338  UT_String myDefaultType;
339  UT_String myScriptedSubnetIndex;
340  UT_Color myDefaultColor;
341  UT_StringHolder myDefaultShape;
342  UT_StringHolder myDefaultWireStyle;
343  OP_Operator *myPrimarySubnet;
344  int myOpCount;
345  int myUniqueOpTypeID;
346  static int theLoadDSOFlag;
347 
348  OP_ScriptCreatorFunc myScriptCreatorFunc;
349  OP_ScriptCanReuseFunc myScriptCanReuseFunc;
350  UT_ValArray<OP_ScriptIndexFile *> myScriptIndexFiles;
351  UT_ValArray<OP_UpdateTableSink *> myUpdateTableSinks;
352 
353  // Data structure to store information on operators, without affecting
354  // the operator definition itself. This also allows us to store overrides
355  // for operators that have not been loaded yet.
356  class OpInfo
357  {
358  public:
359  OpInfo();
360  bool isEmpty() const;
361 
362  OP_Operator *myOperator;
363  OP_Operator *myOriginalOperator;
364  UT_StringHolder myCreationName;
365  UT_StringHolder myOriginalEnglishName;
366 
367  // Deprecation support
368  UT_StringHolder myDeprecatedVersion;
369  UT_StringHolder myDeprecatedReplacement;
370 
371  // Flags
372  bool myIsHidden:1;
373  bool myIsExcluded:1;
374  bool myIsExperimental:1;
375  bool myIsOverridden:1;
376  };
377 
378  UT_SymbolMap<OpInfo> myOperators;
379  UT_SymbolMap<UT_StringHolder> myOpAliases;
380  UT_SymbolMap<UT_StringHolder> myOpOverrides;
381  UT_StringMap<UT_Color> myOpDefaultColors;
382  UT_StringMap<UT_StringHolder> myOpDefaultShapes;
383 
384  OP_OpNamespaceHierarchy myOpNamespaceHierarchy;
385  static UT_String theOpTypeNamespaceHierarchyPref;
386  static bool theOpThemesInitialized;
387 
388  friend class OP_UpdateTableSink;
389 };
390 
392 {
393 public:
396  { removeAllTableSinks(); }
397 
398  virtual void tableUpdated(OP_OperatorTable *table) = 0;
400  { removeUpdateTableSink(table); }
401 
402 protected:
404  {
405  if( !table ) return;
406  table->addUpdateTableSink(this);
407  myOpTables.append(table, 1);
408  }
410  {
411  if( !table ) return;
412  table->removeUpdateTableSink(this);
413  myOpTables.findAndRemove(table);
414  }
416  {
417  for( int i = myOpTables.entries(); i --> 0; )
418  removeUpdateTableSink(myOpTables(i));
419  }
420 
421 private:
422  OP_OperatorTableList myOpTables;
423 };
424 
425 #endif
bool(* OP_ScriptCanReuseFunc)(const OP_ScriptOperator *script_operator, const OP_OTLDefinition &new_definition)
virtual ~OP_UpdateTableSink()
OP_Operator * getPrimarySubnetOperator()
static void setLoadDSOFlag(int i)
static int getLoadDSOFlag()
int getUniqueOpTypeID() const
OP_OpTypeId
Definition: OP_OpTypeId.h:18
GLuint const GLchar * name
Definition: glcorearb.h:786
GLenum GLenum GLsizei void * table
Definition: glad.h:5129
UT_SymbolMap< OP_OperatorTable * > OP_OperatorTableMap
UT_ValArray< OP_OperatorTable * > OP_OperatorTableList
GT_API const UT_StringHolder version
virtual void tableDeleted(OP_OperatorTable *table)
OP_ScriptOperator *(* OP_ScriptCreatorFunc)(const OP_OTLDefinition &definition)
const UT_String & getName() const
void removeUpdateTableSink(OP_OperatorTable *table)
const UT_String & getScriptPath() const
#define OP_API
Definition: OP_API.h:10
int entries() const
void addUpdateTableSink(OP_OperatorTable *table)
type
Definition: core.h:1059