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  // Runs this op table function on a spcific library.
98  bool runDSO(const char* lib_path);
99 
100  int entries() const
101  { return myOpCount; }
102 
103  void getOperatorList(UT_StringArray &list,
104  bool english=true) const;
105  int getOperators(OP_OperatorList &list,
106  OP_Network *net = 0,
107  bool filterhidden = false) const;
108 
109  // This returns the optype id for the given table.
110  OP_OpTypeId getOpTypeID() const;
111 
112  // This returns a unique integer for each OP_OperatorTable created
113  int getUniqueOpTypeID() const
114  { return myUniqueOpTypeID; }
115  // Before sorting, you should getOperators(list);
116  void sortOperators(OP_OperatorList &list,
119 
120  const UT_String &getName() const
121  { return myName; }
122  const UT_String &getScriptPath() const
123  { return myScriptDirectory; }
124 
125  unsigned getOperatorStatus(OP_Operator *op) const;
126  unsigned getStatus() const;
127  void setDefaultType(const char *type);
128  const char *getDefaultType() const;
129 
130  // Add an operator alias (from the OPcustomize file).
131  bool setOpRename(const char *optype, const char *newname,
132  std::ostream *err = nullptr);
133  // Display all the oprename commands.
134  void outputOpRenames(std::ostream &os) const;
135 
136  // Add an operator alias (from the OPcustomize file).
137  bool setOpAlias(const char *optype,
138  const char *alias,
139  std::ostream *err = nullptr);
140 
141  // Sets what the inital default name of the op will be.
142  // null first name will disable, reverting to the usual rules.
143  bool setOpFirstName(const char *type,
144  const char *firstname,
145  std::ostream *err = nullptr);
146  // Display all the opalias commands.
147  void outputOpAliases(std::ostream &os) const;
148  // Display all the firstname commands.
149  void outputOpFirstNames(std::ostream &os) const;
150  // Get all the aliases for a particular operator type.
151  void getOpAliases(const char *opname,
152  UT_StringArray &opaliases) const;
153  // Get the operator type represented by the supplied alias (if any).
154  const char *getOpFromAlias(const char *alias) const;
155 
156  // Add an operator override (from the OPcustomize file).
157  bool setOpOverride(const char *optype,
158  const char *override,
159  bool keep_parms, bool keep_contents,
160  std::ostream *err = nullptr);
161  // Add an operator override (from the OPcustomize file).
162  bool clearOpOverride(const char *optype,
163  std::ostream *err = nullptr);
164  // Display all the opoverride commands.
165  void outputOpOverrides(std::ostream &os) const;
166  // Get all the overridees for a particular operator type.
167  void getOpOverrides(const char *opname,
168  UT_StringArray &opoverridees) const;
169 
170  // Add an operator to the excluded op list (from the OPcustomize file).
171  bool addOpExcluded(const char *opname,
172  std::ostream *err = nullptr);
173  // Is operator excluded?
174  bool isOpExcluded(const char *opname) const;
175  // Display all the opexclude commands.
176  void outputOpExcluded(std::ostream &os) const;
177  // Add an operator to the hidden op list (from the OPcustomize file).
178  bool addOpHidden(const char *opname,
179  std::ostream *err = nullptr);
180  // Delete an operator from the hidden op list (from the OPcustomize file).
181  void delOpHidden(const char *opname);
182  // Is operator hidden?
183  bool isOpHidden(const char *opname) const;
184  // Get all hidden ops.
185  void getOpHidden(UT_StringArray &opnames) const;
186 
187  // Is operator experimental?
188  bool isOpExperimental(const UT_StringRef &opname) const;
189  // Add operator to experimental list
190  bool addOpExperimental(const char *opname,
191  std::ostream *err = nullptr);
192 
193  // Mark the operator as deprecated.
194  bool setOpDeprecated(const char *opname,
195  const char *version,
196  const char *replacement = 0,
197  std::ostream *err = nullptr);
198  // Is the operator deprecated?
199  bool isOpDeprecated(const char *opname) const;
200 
201  // Retrieve the deprecation version and possible replacement operator.
202  bool getDeprecationInfo(const char *opname,
204  UT_StringHolder &replacement) const;
205  // Display all the opdeprecate commands.
206  void outputOpDeprecated(std::ostream &os) const;
207 
208  // Sets the default color for an operator type.
209  bool setOpDefaultColor(const UT_StringHolder &optype,
210  const UT_Color &clr,
211  std::ostream *err = nullptr);
212  // Gets the default color for the supplied operator type.
213  UT_Color getOpDefaultColor(const UT_StringRef &optype) const;
214  // Determine if we have a optype specific default color
215  bool hasSpecificOpDefaultColor(
216  const UT_StringRef &optype) const;
217  // Clear all the opdefaultcolor commands.
218  void clearOpDefaultColors();
219  // Display all the opdefaultcolor commands.
220  void outputOpDefaultColors(std::ostream &os) const;
221 
222  // Sets the default shape for an operator type.
223  bool setOpDefaultShape(const UT_StringHolder &optype,
224  const UT_StringHolder &shape,
225  std::ostream *err = nullptr);
226  // Gets the default shape for the supplied operator type.
227  const UT_StringHolder &getOpDefaultShape(const UT_StringRef &optype) const;
228  // Determine if we have a optype specific default shape
229  bool hasSpecificOpDefaultShape(
230  const UT_StringRef &optype) const;
231  // Clear all the opdefaultshape commands.
232  void clearOpDefaultShapes();
233  // Display all the opdefaultshape commands.
234  void outputOpDefaultShapes(std::ostream &os) const;
235 
236  // Get or set the wire style for networks with this child node type.
237  const UT_StringHolder &getDefaultWireStyle() const;
238  void setDefaultWireStyle(const UT_StringHolder &wirestyle);
239 
240  // When no name is specified for a node, we typically use the type as the
241  // prefix for the name. However, we may want to provide a method for
242  // creating alternate default names for operators.
243  void getDefaultNodeName(const char *type, UT_String &name);
244 
245  // If there is an index file that contains scripted subnets for this op
246  // table, this function returns a pointer to that file name.
247  const char *getScriptedSubnetIndex() const;
248 
249  // Notify those who are interested that our table contents have changed.
250  // Don't do this in addOperator - only after bulk operations like
251  // loadScriptOperators or OP_Netowkr::loadScriptedOpDefs.
252  void notifyUpdateTableSinksOfUpdate();
253 
254  static inline int getLoadDSOFlag()
255  { return theLoadDSOFlag; }
256  static inline void setLoadDSOFlag(int i)
257  { theLoadDSOFlag = i; }
258  static unsigned getPermissionMask(const OP_Operator *op);
259  static void clearPermissionMask(const OP_Operator *op);
260 
261  /// Called once all basic operator types are loaded to call the python
262  /// code which will initialize node color and shape themes.
263  static void initializeOpThemes();
264 
265  /// Builds (or rebuilds) the operator type namespace hierarchy.
266  /// The optype precedence is given by the environment variable
267  /// HOUDINI_OPTYPE_NAMESPACE_HIERARCHY, which is processed by this method.
268  static void buildOpTypeNamespaceHierarchy();
269 
270  /// Obtains the value of the environment variable used to construct
271  /// the hierarchy.
272  static const char *getOpTypeNamespaceHierarchyPref();
273 
274  /// Obtains a list of available operator names that have the same base
275  /// (core) name as the given operator.
276  /// If scope network name is not NULL, the list includes only operators
277  /// whose nodes can be created in that network (otherwise all
278  /// operators are included).
279  /// The list is sorted according to the descending precedence order.
280  void getCandidateOperatorNamesInPrecedenceOrder(
281  UT_StringArray &precedence_order,
282  const char *op_name,
283  const UT_StringArray *scope_network_stack);
284 
285  /// Obtains the preferred operator name that matches the given op_name.
286  /// Any name component included in the op_name must match the returned op
287  /// type name, and any component not present in op_name is assumed to match
288  /// the returned op type. For example 'hda' will match any scope, namespace,
289  /// or version, while 'userA::hda' will match any scope and version, but the
290  /// namespace must be 'userA'. For global namespace use '::hda' and for
291  /// versionless opname use 'hda::'.
292  /// If the scope_network_stack is also given (ie, non-null) then the
293  /// returned opname must match one of the scopes listed in that array too.
294  /// Returns the name of the highest precedence operator that matches
295  /// the given op_name.
296  const char *getPreferredOperatorName(const char *opname,
297  const UT_StringArray *scope_network_stack);
298 
299  /// Returns true if the provided node name is "close enough" to the
300  /// operator type name, english name, or first name to imply what the
301  /// operator type is.
302  bool nodeNameImpliesType(OP_Operator *op,
303  const char *node_name) const;
304 
305  /// Convenience functions for the second callback in setScriptCreator().
306  static bool alwaysReuseScriptOperatorCallback(
307  const OP_ScriptOperator *script_operator,
308  const OP_OTLDefinition &new_definition);
309  static bool neverReuseScriptOperatorCallback(
310  const OP_ScriptOperator *script_operator,
311  const OP_OTLDefinition &new_definition);
312 
313 private:
314  // Functions for adding or removing a table sink.
315  void addUpdateTableSink(OP_UpdateTableSink *sink);
316  void removeUpdateTableSink(OP_UpdateTableSink *sink);
317  // Called when the operator is deleted.
318  void notifyUpdateTableSinksOfDelete();
319 
320  // Functions for adding new operator types using an OP_OTLDefinition.
321  OP_ScriptOperator *addNewOperator(const OP_OTLDefinition &definition,
322  std::ostream *err = nullptr);
323  int loadScriptIndexFile(OP_ScriptIndexFile &sif,
324  bool checkdup);
325  /// Returns the table's operator namespace hierarchy (ie, the hierarchy
326  /// of operator definitions for the optype associated with this table).
327  OP_OpNamespaceHierarchy &getOpNamespaceHierarchy()
328  { return myOpNamespaceHierarchy; }
329 
330  /// Returns the operator named 'opname' for instantiating a node
331  /// inside the parent. If 'opname' is an ambiguous and unqualified opname
332  /// (ie core name witouth namespace), the preferred operator matching
333  /// that opname is returned. However when loading a network or if
334  /// the exact_name flag is true, then an operator with the exact 'opname'
335  /// is returned (or NULL).
336  OP_Operator *getPreferredOperator(OP_Network *parent,
337  const char *opname, bool exact_opname);
338 
339  UT_String myName;
340  UT_String myScriptDirectory;
341  UT_String myDefaultType;
342  UT_String myScriptedSubnetIndex;
343  UT_Color myDefaultColor;
344  UT_StringHolder myDefaultShape;
345  UT_StringHolder myDefaultWireStyle;
346  OP_Operator *myPrimarySubnet;
347  int myOpCount;
348  int myUniqueOpTypeID;
349  static int theLoadDSOFlag;
350 
351  OP_ScriptCreatorFunc myScriptCreatorFunc;
352  OP_ScriptCanReuseFunc myScriptCanReuseFunc;
353  UT_ValArray<OP_ScriptIndexFile *> myScriptIndexFiles;
354  UT_ValArray<OP_UpdateTableSink *> myUpdateTableSinks;
355 
356  // Data structure to store information on operators, without affecting
357  // the operator definition itself. This also allows us to store overrides
358  // for operators that have not been loaded yet.
359  class OpInfo
360  {
361  public:
362  OpInfo();
363  bool isEmpty() const;
364 
365  OP_Operator *myOperator;
366  OP_Operator *myOriginalOperator;
367  UT_StringHolder myCreationName;
368  UT_StringHolder myOriginalEnglishName;
369 
370  // Deprecation support
371  UT_StringHolder myDeprecatedVersion;
372  UT_StringHolder myDeprecatedReplacement;
373 
374  // Flags
375  bool myIsHidden:1;
376  bool myIsExcluded:1;
377  bool myIsExperimental:1;
378  bool myIsOverridden:1;
379  };
380 
381  UT_SymbolMap<OpInfo> myOperators;
382  UT_SymbolMap<UT_StringHolder> myOpAliases;
383  UT_SymbolMap<UT_StringHolder> myOpOverrides;
384  UT_StringMap<UT_Color> myOpDefaultColors;
385  UT_StringMap<UT_StringHolder> myOpDefaultShapes;
386 
387  OP_OpNamespaceHierarchy myOpNamespaceHierarchy;
388  static UT_String theOpTypeNamespaceHierarchyPref;
389  static bool theOpThemesInitialized;
390 
391  friend class OP_UpdateTableSink;
392 };
393 
395 {
396 public:
399  { removeAllTableSinks(); }
400 
401  virtual void tableUpdated(OP_OperatorTable *table) = 0;
403  { removeUpdateTableSink(table); }
404 
405 protected:
407  {
408  if( !table ) return;
409  table->addUpdateTableSink(this);
410  myOpTables.append(table, 1);
411  }
413  {
414  if( !table ) return;
415  table->removeUpdateTableSink(this);
416  myOpTables.findAndRemove(table);
417  }
419  {
420  for( int i = myOpTables.entries(); i --> 0; )
421  removeUpdateTableSink(myOpTables(i));
422  }
423 
424 private:
425  OP_OperatorTableList myOpTables;
426 };
427 
428 #endif
bool(* OP_ScriptCanReuseFunc)(const OP_ScriptOperator *script_operator, const OP_OTLDefinition &new_definition)
virtual ~OP_UpdateTableSink()
OutGridT const XformOp bool bool
OP_Operator * getPrimarySubnetOperator()
static void setLoadDSOFlag(int i)
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
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)