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