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(void) 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  // Add an operator to the excluded op list (from the OPcustomize file).
153  bool addOpExcluded(const char *opname,
154  std::ostream *err = nullptr);
155  // Is operator excluded?
156  bool isOpExcluded(const char *opname) const;
157  // Display all the opexclude commands.
158  void outputOpExcluded(std::ostream &os) const;
159  // Add an operator to the hidden op list (from the OPcustomize file).
160  bool addOpHidden(const char *opname,
161  std::ostream *err = nullptr);
162  // Delete an operator from the hidden op list (from the OPcustomize file).
163  void delOpHidden(const char *opname);
164  // Is operator hidden?
165  bool isOpHidden(const char *opname) const;
166  // Get all hidden ops.
167  void getOpHidden(UT_StringArray &opnames) const;
168 
169  // Mark the operator as deprecated.
170  bool setOpDeprecated(const char *opname,
171  const char *version,
172  const char *replacement = 0,
173  std::ostream *err = nullptr);
174  // Is the operator deprecated?
175  bool isOpDeprecated(const char *opname) const;
176 
177  // Retrieve the deprecation version and possible replacement operator.
178  bool getDeprecationInfo(const char *opname,
180  UT_StringHolder &replacement) const;
181  // Display all the opdeprecate commands.
182  void outputOpDeprecated(std::ostream &os) const;
183 
184  // Sets the default color for an operator type.
185  bool setOpDefaultColor(const UT_StringHolder &optype,
186  const UT_Color &clr,
187  std::ostream *err = nullptr);
188  // Gets the default color for the supplied operator type.
189  UT_Color getOpDefaultColor(const UT_StringRef &optype) const;
190  // Determine if we have a optype specific default color
191  bool hasSpecificOpDefaultColor(
192  const UT_StringRef &optype) const;
193  // Clear all the opdefaultcolor commands.
194  void clearOpDefaultColors();
195  // Display all the opdefaultcolor commands.
196  void outputOpDefaultColors(std::ostream &os) const;
197 
198  // Sets the default shape for an operator type.
199  bool setOpDefaultShape(const UT_StringHolder &optype,
200  const UT_StringHolder &shape,
201  std::ostream *err = nullptr);
202  // Gets the default shape for the supplied operator type.
203  const UT_StringHolder &getOpDefaultShape(const UT_StringRef &optype) const;
204  // Determine if we have a optype specific default shape
205  bool hasSpecificOpDefaultShape(
206  const UT_StringRef &optype) const;
207  // Clear all the opdefaultshape commands.
208  void clearOpDefaultShapes();
209  // Display all the opdefaultshape commands.
210  void outputOpDefaultShapes(std::ostream &os) const;
211 
212  // Get or set the wire style for networks with this child node type.
213  const UT_StringHolder &getDefaultWireStyle() const;
214  void setDefaultWireStyle(const UT_StringHolder &wirestyle);
215 
216  // When no name is specified for a node, we typically use the type as the
217  // prefix for the name. However, we may want to provide a method for
218  // creating alternate default names for operators.
219  void getDefaultNodeName(const char *type, UT_String &name);
220 
221  // If there is an index file that contains scripted subnets for this op
222  // table, this function returns a pointer to that file name.
223  const char *getScriptedSubnetIndex() const;
224 
225  // Notify those who are interested that our table contents have changed.
226  // Don't do this in addOperator - only after bulk operations like
227  // loadScriptOperators or OP_Netowkr::loadScriptedOpDefs.
228  void notifyUpdateTableSinksOfUpdate();
229 
230  static inline int getLoadDSOFlag()
231  { return theLoadDSOFlag; }
232  static inline void setLoadDSOFlag(int i)
233  { theLoadDSOFlag = i; }
234  static unsigned getPermissionMask(const OP_Operator *op);
235  static void clearPermissionMask(const OP_Operator *op);
236 
237  /// Called once all basic operator types are loaded to call the python
238  /// code which will initialize node color and shape themes.
239  static void initializeOpThemes();
240 
241  /// Builds (or rebuilds) the operator type namespace hierarchy.
242  /// The optype precedense is given by the environment variable
243  /// HOUDINI_OPTYPE_NAMESPACE_HIERARCHY, which is processed by this method.
244  static void buildOpTypeNamespaceHierarchy();
245 
246  /// Obtains the value of the environment variable used to construct
247  /// the hierarchy.
248  static const char *getOpTypeNamespaceHierarchyPref();
249 
250  /// Obtains a list of available operator names that have the same base
251  /// (core) name as the given operator.
252  /// If scope network name is not NULL, the list includes only operators
253  /// whose nodes can be created in that network (otherwise all
254  /// operators are included).
255  /// The list is sorted according to the descending precedence order.
256  void getCandidateOperatorNamesInPrecedenceOrder(
257  UT_StringArray &precedence_order,
258  const char *op_name,
259  const UT_StringArray *scope_network_stack);
260 
261  /// Obtains the preferred operator name that matches the given op_name.
262  /// Any name component included in the op_name must match the returned op
263  /// type name, and any component not present in op_name is assumed to match
264  /// the returned op type. For example 'hda' will match any scope, namespace,
265  /// or version, while 'userA::hda' will match any scope and version, but the
266  /// namespace must be 'userA'. For global namespace use '::hda' and for
267  /// versionless opname use 'hda::'.
268  /// If the scope_network_stack is also given (ie, non-null) then the
269  /// returned opname must match one of the scopes listed in that array too.
270  /// Returns the name of the highest precedence operator that matches
271  /// the given op_name.
272  const char *getPreferredOperatorName(const char *opname,
273  const UT_StringArray *scope_network_stack);
274 
275  /// Returns true if the provided node name is "close enough" to the
276  /// operator type name, english name, or first name to imply what the
277  /// operator type is.
278  bool nodeNameImpliesType(OP_Operator *op,
279  const UT_String &node_name) const;
280 
281  /// Convenience functions for the second callback in setScriptCreator().
282  static bool alwaysReuseScriptOperatorCallback(
283  const OP_ScriptOperator *script_operator,
284  const OP_OTLDefinition &new_definition);
285  static bool neverReuseScriptOperatorCallback(
286  const OP_ScriptOperator *script_operator,
287  const OP_OTLDefinition &new_definition);
288 
289 private:
290  // Functions for adding or removing a table sink.
291  void addUpdateTableSink(OP_UpdateTableSink *sink);
292  void removeUpdateTableSink(OP_UpdateTableSink *sink);
293  // Called when the operator is deleted.
294  void notifyUpdateTableSinksOfDelete();
295 
296  // Functions for adding new operator types using an OP_OTLDefinition.
297  OP_ScriptOperator *addNewOperator(const OP_OTLDefinition &definition,
298  std::ostream *err = nullptr);
299  int loadScriptIndexFile(OP_ScriptIndexFile &sif,
300  bool checkdup);
301  /// Returns the table's operator namespace hierarchy (ie, the hierarchy
302  /// of operator definitions for the optype associated with this table).
303  OP_OpNamespaceHierarchy &getOpNamespaceHierarchy()
304  { return myOpNamespaceHierarchy; }
305 
306  /// Returns the operator named 'opname' for instantiating a node
307  /// inside the parent. If 'opname' is an ambiguous and unqualified opname
308  /// (ie core name witouth namespace), the preferred operator matching
309  /// that opname is returned. However when loading a network or if
310  /// the exact_name flag is true, then an operator with the exact 'opname'
311  /// is returned (or NULL).
312  OP_Operator *getPreferredOperator(OP_Network *parent,
313  const char *opname, bool exact_opname);
314 
315  UT_String myName;
316  UT_String myScriptDirectory;
317  UT_String myDefaultType;
318  UT_String myScriptedSubnetIndex;
319  UT_Color myDefaultColor;
320  UT_StringHolder myDefaultShape;
321  UT_StringHolder myDefaultWireStyle;
322  OP_Operator *myPrimarySubnet;
323  int myOpCount;
324  int myUniqueOpTypeID;
325  static int theLoadDSOFlag;
326 
327  OP_ScriptCreatorFunc myScriptCreatorFunc;
328  OP_ScriptCanReuseFunc myScriptCanReuseFunc;
329  UT_ValArray<OP_ScriptIndexFile *> myScriptIndexFiles;
330  UT_ValArray<OP_UpdateTableSink *> myUpdateTableSinks;
331 
332  // Data structure to store information on operators, without affecting
333  // the operator definition itself. This also allows us to store overrides
334  // for operators that have not been loaded yet.
335  class OpInfo
336  {
337  public:
338  OpInfo();
339  bool isEmpty() const;
340 
341  OP_Operator *myOperator;
342  UT_StringHolder myCreationName;
343  UT_StringHolder myOriginalEnglishName;
344 
345  // Deprecation support
346  UT_StringHolder myDeprecatedVersion;
347  UT_StringHolder myDeprecatedReplacement;
348 
349  // Flags
350  bool myIsHidden:1;
351  bool myIsExcluded:1;
352  };
353 
354  UT_SymbolMap<OpInfo> myOperators;
355  UT_SymbolMap<UT_StringHolder> myOpAliases;
356  UT_StringMap<UT_Color> myOpDefaultColors;
357  UT_StringMap<UT_StringHolder> myOpDefaultShapes;
358 
359  OP_OpNamespaceHierarchy myOpNamespaceHierarchy;
360  static UT_String theOpTypeNamespaceHierarchyPref;
361  static bool theOpThemesInitialized;
362 
363  friend class OP_UpdateTableSink;
364 };
365 
367 {
368 public:
371  { removeAllTableSinks(); }
372 
373  virtual void tableUpdated(OP_OperatorTable *table) = 0;
375  { removeUpdateTableSink(table); }
376 
377 protected:
379  {
380  if( !table ) return;
381  table->addUpdateTableSink(this);
382  myOpTables.append(table, 1);
383  }
385  {
386  if( !table ) return;
387  table->removeUpdateTableSink(this);
388  myOpTables.findAndRemove(table);
389  }
391  {
392  for( int i = myOpTables.entries(); i --> 0; )
393  removeUpdateTableSink(myOpTables(i));
394  }
395 
396 private:
397  OP_OperatorTableList myOpTables;
398 };
399 
400 #endif
bool(* OP_ScriptCanReuseFunc)(const OP_ScriptOperator *script_operator, const OP_OTLDefinition &new_definition)
virtual ~OP_UpdateTableSink()
GLuint const GLchar * name
Definition: glew.h:1814
GLsizei GLenum GLboolean sink
Definition: glew.h:4966
GLenum GLsizei GLenum GLenum const void * table
Definition: glew.h:4940
OP_Operator * getPrimarySubnetOperator()
static void setLoadDSOFlag(int i)
static int getLoadDSOFlag()
int getUniqueOpTypeID() const
OP_OpTypeId
Definition: OP_OpTypeId.h:18
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
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)