HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CH_Collection.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: Channel library (C++)
7  *
8  * COMMENTS: Collection for channel library
9  *
10  */
11 
12 #ifndef __CH_Collection_h__
13 #define __CH_Collection_h__
14 
15 #include "CH_API.h"
16 #include "CH_EventManager.h"
17 #include "CH_ExprLanguage.h"
18 #include "CH_Manager.h"
19 #include "CH_Types.h"
20 #include <EXPR/EX_Error.h>
21 #include <UT/UT_Array.h>
22 #include <UT/UT_ArrayStringMap.h>
23 #include <UT/UT_StringHolder.h>
24 #include <UT/UT_UniquePtr.h>
25 #include <UT/UT_ValArray.h>
26 #include <UT/UT_Packet.h>
27 #include <SYS/SYS_Inline.h>
28 #include <SYS/SYS_Types.h>
29 
30 #include <iosfwd>
31 
32 
33 class CH_Channel;
34 class CH_LocalVariable;
35 
36 class UT_IStream;
37 class UT_String;
38 class UT_StringArray;
39 class UT_XformOrder;
40 class PRM_Parm;
41 
42 
44 
45 typedef enum {
66 
67 enum {
69 };
70 
71 // Forward declare of OP_Channels so that we can provide a mechanism to
72 // cast to it safely.
73 class OP_Channels;
74 
76 {
77 // Starting at the current input stream position, tries to load
78 // the information about the next collection from the chn or bchn file
79 // that the input stream represents.
80 public:
82  bool begin(UT_StringHolder &path);
83  bool end();
84  bool nextCollection(UT_StringHolder &path);
85 
86 private:
87  UT_IStream *myCurrIs;
88  int myIsBinary;
89 };
90 
92 {
93 public:
94  CH_Collection(CH_Manager *manager);
95  CH_Collection(CH_Manager *manager, const CH_Collection &from);
96  virtual ~CH_Collection();
97 
98  virtual int64 getMemoryUsage(bool inclusive) const;
99 
100  SETUPCLASSPACKETID("CH_Collection");
101 
102  CH_Manager *getManager() const { return myManager; }
103  fpreal getTolerance() const { return myManager->getTolerance(); }
104 
105  virtual int canAccess(uint mask) const;
106  virtual int canAccessChannel(uint mask, const CH_Channel *chp) const;
107 
108  exint entries() const { return myChannelMap.size(); }
109 
110  //
111  // When getting channels, it's the channel name that is passed,
112  // NOT the path
113  //
114  const CH_Channel *getChannel(const UT_StringRef &name) const;
115  CH_Channel *getChannel(const UT_StringRef &name);
116  const CH_Channel *getChannel(int idx) const { return myChannels(idx); }
117  CH_Channel *getChannel(int idx) { return myChannels(idx); }
118 
119  int getChannels(CH_ChannelList &chans,
120  int scope=0) const;
121 
122  int getChannels(CH_ChannelList &chans,
123  const char *pattern) const;
124 
125  int getChannelNames(CH_NameList &list, int scope=0) const;
126  int getChannelCount(int scoped = 0) const;
127 
128  //
129  // Get a collection or channel given a relative path from this collection
130  //
131  virtual CH_Collection *findCollection(const char *path) const = 0;
132  CH_Channel *findChannel (const char *path) const;
133  CH_Channel *findOrCreateChannel (const char *path);
134 
135 
136  //
137  // When adding a channel, adding by name is the safest method since
138  //
139  CH_Channel *addChannel(const UT_StringHolder &name);
140  CH_Channel *addChannel(CH_Channel *chp);
141 
142  CH_Channel *dupChannel(const UT_StringRef &name);
143  virtual CH_Channel *dupChannel(const CH_Channel &from);
144 
145  // The following method will rename a channel. Only "spare" channels are
146  // allowed to be re-named. The renaming will fail if the channel is not a
147  // spare channel, or if the channel isn't found.
148  virtual int rename(const char *from_name, const char *to_name,
149  const char *to_alias);
150  virtual int rename(CH_Channel *from, const char *to_name,
151  const char *to_alias);
152 
153  void deleteChannel(CH_Channel *chp);
154  int deleteChannels();
155 
156  //
157  // Methods to get a channel evaluated
158  //
159  fpreal evaluate(CH_Channel *chp, fpreal time,
160  bool no_disabling /*=false*/, int thread);
161  void evaluate(CH_Channel *chp, fpreal from, fpreal to,
162  fpreal *vals, int nsamples,
163  bool no_disabling /*=false*/,
164  bool use_cache /*=false*/, int thread);
165  void evaluateString(UT_String &result,
166  CH_Channel *chp, fpreal time,
167  int thread);
168 
169  //
170  // Reparses all the expressions. Should be done if the local
171  // variables have changed.
172  //
173  void unresolveLocalVars(int thread);
174 
175  //
176  // Now, some common mirrors for system calls
177  //
178  virtual UT_StringHolder getFullPath() const = 0;
179  void getFullPath(UT_String &path) const;
180  void getFullPath(const CH_Channel *chp, UT_StringHolder &p,
181  bool use_alias=false) const;
182  void getFullPath(const CH_Channel *chp, UT_String &p,
183  bool use_alias=false) const
184  {
186  getFullPath(chp, s, use_alias);
187  p.adopt(s);
188  }
189 
190  //
191  // channelFind() & collectionFind() will traverse the channel tree
192  // for the given path, calling the callback on every match of the
193  // specified type. For example: /o*/g*/t?
194  //
195  void channelFind(const char *path, CHfindCallBack callback,
196  void *data = nullptr) const;
197 
198  // These methods will do word finding/replacement in all expressions of
199  // all channels.
200  void findString(const char *word, bool fullword,
201  bool usewildcards, CHfindCallBack callback,
202  void *data = nullptr);
203  int findString(const char *word, bool fullword,
204  bool usewildcards) const;
205  int changeString(const char *from, const char *to,
206  bool fullword, int thread);
207 
208  //
209  // The following pure virtuals must be defined by a sub-class.
210  // These provide the collection name and the channel hierarchy.
211  //
212  virtual const UT_String &getName() const = 0;
213 
214  virtual int getUniqueId() const;
215  virtual CH_Collection * lookupCollection(int id) const;
216 
217  virtual const CH_Collection *getParent() const = 0;
218  virtual CH_Collection *getParent() = 0;
219 
220  virtual const CH_Collection *getChild(const char *name) const = 0;
221  virtual CH_Collection *getChild(const char *name) = 0;
222 
223 
224  // This is a mean of accessing the node owner from CH library
225  // It's used only the know if the owner is valid and OP_Node isn't used
226  virtual const void *getOwner() const = 0;
227  virtual void *getOwner() = 0;
228 
229  //
230  // These methods only get child collections, not channels
231  virtual int getChildren(CH_NameList &names) const =0;
232  virtual int getChildren(CH_CollectionList &grps) const =0;
233 
234 
235  virtual bool getVariableValue(UT_String &str,
236  int table, int index,
237  int thread);
238  virtual bool getVariableValue(fpreal &val,
239  int table, int index,
240  int thread);
241 
242  virtual const CH_LocalVariable *resolveVariable(const char *name, int &tab);
243 
244  //
245  // I/O methods
246  //
247  int save(std::ostream &os, bool binary, bool compiled,
248  bool scoped, bool pending_state,
249  const CH_ChannelList *channels) const;
250  int saveToFile(const char *nm) const;
251 
252  template <typename FPREAL_TYPE>
253  bool load(UT_IStream &is, const char *path = nullptr,
254  const CH_LoadInfo *load_info = nullptr,
255  CH_ChannelList *loadedChannels = nullptr,
256  bool delete_keys = false);
257  int loadFromFile(const char *nm);
258 
259  //
260  // Keyframe management & convenience function
261  //
262  fpreal findKey(fpreal gtime, int dir) const;
263  virtual void saveKeyFrameForUndo(CH_Channel *chp, fpreal gtime) = 0;
264 
265  /// Save the entire channel for undo
266  virtual void saveChannelForUndo(CH_Channel *chp) = 0;
267 
268  // int anyModified() const;
269  // void clearModified();
270 
271  int getChanged() const { return myModifiedFlag; }
272  virtual void setChanged(CH_CHANGE_TYPE how, void *data = nullptr);
273 
274  void setExpose( bool onoff );
275  bool getExpose() const { return myExpose; }
276 
277  //
278  // Remove and delete all channels
279  //
280  void clearAndDestroy();
281 
282  void initManager(CH_Manager *manager);
283 
285  { myVariables[0] = table; }
286 
288  { return myManager->getGlobalStart(); }
290  { return myManager->getGlobalEnd(); }
291 
292  // returns the 'range' of the collection - the lowest start time and the
293  // highest end time. Returns false if the collection has no 'range'.
294  // Single expressions do not set the range, but will set the timedep
295  // flag if necessary. The timedep flag is set if even one of the channels
296  // is time dependent.
297  bool getCollectionRange(fpreal &start, fpreal &end,
298  bool &timedep, int thread);
299 
301  { myVariables[3] = table; }
302 
303  // This method should only be called by CH_Manager. It signifies
304  // this collection is dependent on a param function and will be
305  // calledback on modification until removeParamDependency is called.
306 
307  void addedParamDependency();
308  void removeParamDependency();
309 
310  // Returns the xform order for this collection given a specific channel.
311  // This defaults to XYZ, but can be overridden by OP_Node types
312  // to handle their specific orders, such as with Objects.
313  virtual void getXformOrder(UT_XformOrder &xord, const CH_Channel *chp) const;
314 
315  // The "external" flag marks this CH_Collection object as not belonging to
316  // any node (ie. standalone).
317  // @{
318  bool getExternal() const
319  { return myExternal; }
320  void setExternal(bool yesno)
321  { myExternal = yesno; }
322  // @}
323 
324  // Set the alias on a channel -- returns true if the alias was
325  // successfully set and false otherwise
326  bool setChannelAlias(CH_Channel &chp,
327  const UT_StringHolder &alias);
328 
329  // Returns true if s represents a valid Channel name and false otherwise
330  static bool isValidChannelName(const char *s);
331 
332  // Returns a valid pointer if this instance is of an OP_Channel and
333  // nullptr otherwise.
334  virtual OP_Channels *castToOPChannels() { return nullptr; }
335  virtual const OP_Channels *castToOPChannels() const { return nullptr; }
336 
337  // Overridden by OP_Channels to add dependencies for local variables
338  virtual void addLocalVarDependency(int table, int index,
339  void *ref_id);
340 
341  CH_ExprLanguage getExprLanguage() const;
342  void setExprLanguage(CH_ExprLanguage language);
343 
344  virtual void getLocalVariables(UT_StringArray &out_vars);
345 
346 protected:
347 
348  // checkExpressionError() is split up into a checking part and an actual
349  // error handling part, evaluationError() so that the checking part can
350  // be inlined.
351  SYS_FORCE_INLINE void
352  checkEvaluationError(const CH_Channel *chp, int thread) const
353  {
354  int err = ev_GetErrNo(thread);
355  if (err != EV_NOERROR)
356  evaluationError(chp, err, thread);
357  }
358 
360  { myVariables[2] = table; }
361  const CH_Collection *getRoot() const;
362  //
363  // Don't publicize our implementation:
364  //
365  const CH_ChannelList *getChannels() const { return &myChannels; }
366 
367  virtual void notifyChannelRenameDependents(const char *old_name,
368  const char *new_name);
369 
370 private:
371  CH_Manager *myManager;
372  CH_LocalVariable *myVariables[4]; // Local var tables
373  CH_ChannelMap myChannelMap;
374  CH_ChannelList myChannels; // For easy traversal
376  myChannelAliasesTable;
377 
378  int myModifiedFlag; // My modified flag
379 
380  CH_ExprLanguage myExprLanguage;
381 
382  char myParamDependent; // relies on param function
383 
384  bool myExternal;
385  bool myExpose;
386 
387  // evaluationError() generates the appropriate call to opError() and then
388  // clears the expression error state.
389  void evaluationError(const CH_Channel *chp, int err,
390  int thread) const;
391 
392  void deleteChannel(exint index); // Delete from array
393 
394  template <typename FPREAL_TYPE>
395  CH_Channel * loadChannelRange(UT_IStream &is, const char *name,
396  const CH_LoadInfo &load_info);
397 
398  friend class CH_Manager;
399 };
400 
401 CH_API extern void
402 CHcollectionsToOPchannels( CH_CollectionList const& collections,
403  UT_ValArray< OP_Channels* > &op_channels );
404 
405 #endif
CH_Manager * getManager() const
#define SETUPCLASSPACKETID(classname)
Definition: UT_Packet.h:90
#define EV_NOERROR
Definition: EX_Error.h:18
CH_ExprLanguage
GT_API const UT_StringHolder time
Transformation order of scales, rotates, and translates.
Definition: UT_XformOrder.h:23
GLuint start
Definition: glcorearb.h:475
void setExternal(bool yesno)
const GLuint GLenum const void * binary
Definition: glcorearb.h:1924
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
int64 exint
Definition: SYS_Types.h:125
bool getExpose() const
GLdouble s
Definition: glad.h:3009
void updateGlobalVariables(CH_LocalVariable *table)
**But if you need a result
Definition: thread.h:613
void addLocalVariables(CH_LocalVariable *table)
void addDynamicVariables(CH_LocalVariable *table)
GU_API GA_Offset getParent(const GU_Detail *gdp, const GA_Offset &node)
const CH_Channel * getChannel(int idx) const
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
fpreal getGlobalEnd() const
GU_API GA_OffsetArray getChildren(const GU_Detail *gdp, const GA_Offset &node, bool recurse=false)
CH_CHANGE_TYPE
bool getExternal() const
CH_LOCAL_VARIABLE
Definition: CH_Collection.h:45
PXL_API const char * getName(const ColorSpace *space)
Return the name of the color space.
void getFullPath(const CH_Channel *chp, UT_String &p, bool use_alias=false) const
GLuint GLuint end
Definition: glcorearb.h:475
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
GLint GLuint mask
Definition: glcorearb.h:124
CH_Channel * getChannel(int idx)
long long int64
Definition: SYS_Types.h:116
fpreal getTolerance() const
const CH_ChannelList * getChannels() const
GLuint const GLchar * name
Definition: glcorearb.h:786
GLushort pattern
Definition: glad.h:2583
CH_API void CHcollectionsToOPchannels(CH_CollectionList const &collections, UT_ValArray< OP_Channels * > &op_channels)
virtual OP_Channels * castToOPChannels()
GLenum GLenum GLsizei void * table
Definition: glad.h:5129
SYS_FORCE_INLINE void checkEvaluationError(const CH_Channel *chp, int thread) const
fpreal getGlobalStart() const
void adopt(char *s)
Definition: UT_String.h:277
**Note that the tasks the is the thread number *for the or if it s being executed by a non pool thread(this *can happen in cases where the whole pool is occupied and the calling *thread contributes to running the work load).**Thread pool.Have fun
int(*)(CH_Collection *grp, CH_Channel *chp, void *) CHfindCallBack
Definition: CH_Types.h:31
UT_ArrayStringMap< CH_Channel * > CH_ChannelMap
Definition: CH_Collection.h:40
OIIO_UTIL_API bool rename(string_view from, string_view to, std::string &err)
fpreal64 fpreal
Definition: SYS_Types.h:277
#define CH_API
Definition: CH_API.h:10
GLuint index
Definition: glcorearb.h:786
GLuint GLfloat * val
Definition: glcorearb.h:1608
exint entries() const
virtual const OP_Channels * castToOPChannels() const
unsigned int uint
Definition: SYS_Types.h:45
int getChanged() const
Definition: format.h:895
ImageBuf OIIO_API channels(const ImageBuf &src, int nchannels, cspan< int > channelorder, cspan< float > channelvalues={}, cspan< std::string > newchannelnames={}, bool shuffle_channel_names=false, int nthreads=0)
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:483