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