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 
75 #define CAST_OPCHANNELS(collection) ((collection) ? (collection)->castToOPChannels() : nullptr)
76 
78 {
79 // Starting at the current input stream position, tries to load
80 // the information about the next collection from the chn or bchn file
81 // that the input stream represents.
82 public:
84  bool begin(UT_StringHolder &path);
85  bool end();
86  bool nextCollection(UT_StringHolder &path);
87 
88 private:
89  UT_IStream *myCurrIs;
90  int myIsBinary;
91 };
92 
94 {
95 public:
96  CH_Collection(CH_Manager *manager);
97  CH_Collection(CH_Manager *manager, const CH_Collection &from);
98  virtual ~CH_Collection();
99 
100  virtual int64 getMemoryUsage(bool inclusive) const;
101 
102  SETUPCLASSPACKETID("CH_Collection");
103 
104  CH_Manager *getManager() const { return myManager; }
105  fpreal getTolerance() const { return myManager->getTolerance(); }
106 
107  virtual int canAccess(uint mask) const;
108  virtual int canAccessChannel(uint mask, const CH_Channel *chp) const;
109 
110  exint entries() const { return myChannelMap.size(); }
111 
112  //
113  // When getting channels, it's the channel name that is passed,
114  // NOT the path
115  //
116  const CH_Channel *getChannel(const UT_StringRef &name) const;
117  CH_Channel *getChannel(const UT_StringRef &name);
118  const CH_Channel *getChannel(int idx) const { return myChannels(idx); }
119  CH_Channel *getChannel(int idx) { return myChannels(idx); }
120 
121  int getChannels(CH_ChannelList &chans,
122  int scope=0) const;
123 
124  int getChannels(CH_ChannelList &chans,
125  const char *pattern) const;
126 
127  int getChannelNames(CH_NameList &list, int scope=0) const;
128  int getChannelCount(int scoped = 0) const;
129 
130  //
131  // Get a collection or channel given a relative path from this collection
132  //
133  virtual CH_Collection *findCollection(const char *path) const = 0;
134  CH_Channel *findChannel (const char *path) const;
135  CH_Channel *findOrCreateChannel (const char *path);
136 
137 
138  //
139  // When adding a channel, adding by name is the safest method
140  //
141  CH_Channel *addChannel(const UT_StringHolder &name);
142  CH_Channel *addChannel(CH_Channel *chp);
143 
144  CH_Channel *dupChannel(const UT_StringRef &name);
145  virtual CH_Channel *dupChannel(const CH_Channel &from);
146  CH_Channel *dupTempChannel(const CH_Channel &from);
147 
148  // The following method will rename a channel. Only "spare" channels are
149  // allowed to be re-named. The renaming will fail if the channel is not a
150  // spare channel, or if the channel isn't found.
151  virtual int rename(const char *from_name, const char *to_name,
152  const char *to_alias);
153  virtual int rename(CH_Channel *from, const char *to_name,
154  const char *to_alias);
155 
156  void deleteChannel(CH_Channel *chp);
157  int deleteChannels();
158 
159  //
160  // Methods to get a channel evaluated
161  //
163  bool no_disabling /*=false*/, int thread);
164  void evaluate(CH_Channel *chp, fpreal from, fpreal to,
165  fpreal *vals, int nsamples,
166  bool no_disabling /*=false*/,
167  bool use_cache /*=false*/, int thread);
168  void evaluateString(UT_StringHolder &result,
169  CH_Channel *chp, fpreal time,
170  int thread);
171 
172  //
173  // Reparses all the expressions. Should be done if the local
174  // variables have changed.
175  //
176  void unresolveLocalVars(int thread);
177 
178  //
179  // Now, some common mirrors for system calls
180  //
181  virtual UT_StringHolder getFullPath() const = 0;
182  void getFullPath(UT_String &path) const;
183  void getFullPath(const CH_Channel *chp, UT_StringHolder &p,
184  bool use_alias=false) const;
185  void getFullPath(const CH_Channel *chp, UT_String &p,
186  bool use_alias=false) const
187  {
189  getFullPath(chp, s, use_alias);
190  p.adopt(s);
191  }
192 
193  //
194  // channelFind() & collectionFind() will traverse the channel tree
195  // for the given path, calling the callback on every match of the
196  // specified type. For example: /o*/g*/t?
197  //
198  void channelFind(const char *path, CHfindCallBack callback,
199  void *data = nullptr) const;
200 
201  // These methods will do word finding/replacement in all expressions of
202  // all channels.
203  void findString(const char *word, bool fullword,
204  bool usewildcards, CHfindCallBack callback,
205  void *data = nullptr);
206  int findString(const char *word, bool fullword,
207  bool usewildcards) const;
208  int changeString(const char *from, const char *to,
209  bool fullword, int thread);
210 
211  //
212  // The following pure virtuals must be defined by a sub-class.
213  // These provide the collection name and the channel hierarchy.
214  //
215  virtual const UT_StringHolder &getName() const = 0;
216 
217  virtual int getUniqueId() const;
218  virtual CH_Collection * lookupCollection(int id) const;
219 
220  virtual const CH_Collection *getParent() const = 0;
221  virtual CH_Collection *getParent() = 0;
222 
223  virtual const CH_Collection *getChild(const char *name) const = 0;
224  virtual CH_Collection *getChild(const char *name) = 0;
225 
226 
227  // This is a mean of accessing the node owner from CH library
228  // It's used only the know if the owner is valid and OP_Node isn't used
229  virtual const void *getOwner() const = 0;
230  virtual void *getOwner() = 0;
231 
232  //
233  // These methods only get child collections, not channels
234  virtual int getChildren(CH_NameList &names) const =0;
235  virtual int getChildren(CH_CollectionList &grps) const =0;
236 
237 
238  virtual bool getVariableValue(UT_String &str,
239  int table, int index,
240  int thread);
241  virtual bool getVariableValue(fpreal &val,
242  int table, int index,
243  int thread);
244 
245  virtual const CH_LocalVariable *resolveVariable(const char *name, int &tab);
246 
247  //
248  // I/O methods
249  //
250  int save(std::ostream &os, bool binary, bool compiled,
251  bool scoped, bool pending_state,
252  const CH_ChannelList *channels) const;
253  int saveToFile(const char *nm) const;
254 
255  template <typename FPREAL_TYPE>
256  bool load(UT_IStream &is, const char *path = nullptr,
257  const CH_LoadInfo *load_info = nullptr,
258  CH_ChannelList *loadedChannels = nullptr,
259  bool delete_keys = false);
260  int loadFromFile(const char *nm);
261 
262  //
263  // Keyframe management & convenience function
264  //
265  fpreal findKey(fpreal gtime, int dir) const;
266  virtual void saveKeyFrameForUndo(CH_Channel *chp, fpreal gtime) = 0;
267 
268  /// Save the entire channel for undo
269  virtual void saveChannelForUndo(CH_Channel *chp) = 0;
270 
271  // int anyModified() const;
272  // void clearModified();
273 
274  int getChanged() const { return myModifiedFlag; }
275  virtual void setChanged(CH_CHANGE_TYPE how, void *data = nullptr);
276 
277  void setExpose( bool onoff );
278  bool getExpose() const { return myExpose; }
279 
280  //
281  // Remove and delete all channels
282  //
283  void clearAndDestroy();
284 
285  void initManager(CH_Manager *manager);
286 
288  { myVariables[0] = table; }
289 
291  { return myManager->getGlobalStart(); }
293  { return myManager->getGlobalEnd(); }
294 
295  // returns the 'range' of the collection - the lowest start time and the
296  // highest end time. Returns false if the collection has no 'range'.
297  // Single expressions do not set the range, but will set the timedep
298  // flag if necessary. The timedep flag is set if even one of the channels
299  // is time dependent.
300  bool getCollectionRange(fpreal &start, fpreal &end,
301  bool &timedep, int thread);
302 
304  { myVariables[3] = table; }
305 
306  // This method should only be called by CH_Manager. It signifies
307  // this collection is dependent on a param function and will be
308  // calledback on modification until removeParamDependency is called.
309 
310  void addedParamDependency();
311  void removeParamDependency();
312 
313  // Returns the xform order for this collection given a specific channel.
314  // This defaults to XYZ, but can be overridden by OP_Node types
315  // to handle their specific orders, such as with Objects.
316  virtual void getXformOrder(UT_XformOrder &xord, const CH_Channel *chp) const;
317 
318  // The "external" flag marks this CH_Collection object as not belonging to
319  // any node (ie. standalone).
320  // @{
321  bool getExternal() const
322  { return myExternal; }
323  void setExternal(bool yesno)
324  { myExternal = yesno; }
325  // @}
326 
327  /// These evaluators are for external collections to allow ch()
328  /// to work.
329  virtual double chWithinExternal(const char *channelpath, fpreal time) const
330  { return 0.0; }
331  virtual UT_StringHolder chsWithinExternal(const char *channelpath, fpreal time) const
332  { return {}; }
333 
334 
335  // Set the alias on a channel -- returns true if the alias was
336  // successfully set and false otherwise
337  bool setChannelAlias(CH_Channel &chp,
338  const UT_StringHolder &alias);
339 
340  // Returns true if s represents a valid Channel name and false otherwise
341  static bool isValidChannelName(const char *s);
342 
343  // Returns a valid pointer if this instance is of an OP_Channel and
344  // nullptr otherwise.
345  virtual OP_Channels *castToOPChannels() { return nullptr; }
346  virtual const OP_Channels *castToOPChannels() const { return nullptr; }
347 
348  // Overridden by OP_Channels to add dependencies for local variables
349  virtual void addLocalVarDependency(int table, int index,
350  void *ref_id);
351 
352  CH_ExprLanguage getExprLanguage() const;
353  void setExprLanguage(CH_ExprLanguage language);
354 
355  virtual void getLocalVariables(UT_StringArray &out_vars);
356 
357 protected:
358 
359  // checkExpressionError() is split up into a checking part and an actual
360  // error handling part, evaluationError() so that the checking part can
361  // be inlined.
362  SYS_FORCE_INLINE void
363  checkEvaluationError(const CH_Channel *chp, int thread) const
364  {
365  int err = ev_GetErrNo(thread);
366  if (err != EV_NOERROR)
367  evaluationError(chp, err, thread);
368  }
369 
371  { myVariables[2] = table; }
372  const CH_Collection *getRoot() const;
373  //
374  // Don't publicize our implementation:
375  //
376  const CH_ChannelList *getChannels() const { return &myChannels; }
377 
378  virtual void notifyChannelRenameDependents(const char *old_name,
379  const char *new_name);
380 
381 public:
382  static const char *theValidChannelNameWordBreaks;
383 
384 private:
385  CH_Manager *myManager;
386  CH_LocalVariable *myVariables[4]; // Local var tables
387  CH_ChannelMap myChannelMap;
388  CH_ChannelList myChannels; // For easy traversal
390  myChannelAliasesTable;
391 
392  int myModifiedFlag; // My modified flag
393 
394  CH_ExprLanguage myExprLanguage;
395 
396  char myParamDependent; // relies on param function
397 
398  bool myExternal;
399  bool myExpose;
400 
401  // evaluationError() generates the appropriate call to opError() and then
402  // clears the expression error state.
403  void evaluationError(const CH_Channel *chp, int err,
404  int thread) const;
405 
406  void deleteChannel(exint index); // Delete from array
407 
408  template <typename FPREAL_TYPE>
409  CH_Channel * loadChannelRange(UT_IStream &is, const char *name,
410  const CH_LoadInfo &load_info);
411 
412  friend class CH_Manager;
413 };
414 
415 CH_API extern void
416 CHcollectionsToOPchannels( CH_CollectionList const& collections,
417  UT_ValArray< OP_Channels* > &op_channels );
418 
419 #endif
CH_Manager * getManager() const
#define SETUPCLASSPACKETID(classname)
Definition: UT_Packet.h:90
CE_API bool evaluate(const CE_OSDRefinement &topo, const CE_OSDEvaluator &eval, const cl::Buffer &coarse, const CE_OSDPatchCoords &patch_coords, const CE_FloatArray *refined_data, cl::Buffer &out, cl::Buffer *du, cl::Buffer *dv)
#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
virtual double chWithinExternal(const char *channelpath, fpreal time) const
GLdouble s
Definition: glad.h:3009
void updateGlobalVariables(CH_LocalVariable *table)
virtual UT_StringHolder chsWithinExternal(const char *channelpath, fpreal time) const
**But if you need a result
Definition: thread.h:622
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
static const char * theValidChannelNameWordBreaks
void adopt(char *s)
Definition: UT_String.h:287
**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
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:587
OIIO_UTIL_API bool rename(string_view from, string_view to, std::string &err)
fpreal64 fpreal
Definition: SYS_Types.h:283
#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:1821
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)