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 
147  // The following method will rename a channel. Only "spare" channels are
148  // allowed to be re-named. The renaming will fail if the channel is not a
149  // spare channel, or if the channel isn't found.
150  virtual int rename(const char *from_name, const char *to_name,
151  const char *to_alias);
152  virtual int rename(CH_Channel *from, const char *to_name,
153  const char *to_alias);
154 
155  void deleteChannel(CH_Channel *chp);
156  int deleteChannels();
157 
158  //
159  // Methods to get a channel evaluated
160  //
161  fpreal evaluate(CH_Channel *chp, fpreal time,
162  bool no_disabling /*=false*/, int thread);
163  void evaluate(CH_Channel *chp, fpreal from, fpreal to,
164  fpreal *vals, int nsamples,
165  bool no_disabling /*=false*/,
166  bool use_cache /*=false*/, int thread);
167  void evaluateString(UT_String &result,
168  CH_Channel *chp, fpreal time,
169  int thread);
170 
171  //
172  // Reparses all the expressions. Should be done if the local
173  // variables have changed.
174  //
175  void unresolveLocalVars(int thread);
176 
177  //
178  // Now, some common mirrors for system calls
179  //
180  virtual UT_StringHolder getFullPath() const = 0;
181  void getFullPath(UT_String &path) const;
182  void getFullPath(const CH_Channel *chp, UT_StringHolder &p,
183  bool use_alias=false) const;
184  void getFullPath(const CH_Channel *chp, UT_String &p,
185  bool use_alias=false) const
186  {
188  getFullPath(chp, s, use_alias);
189  p.adopt(s);
190  }
191 
192  //
193  // channelFind() & collectionFind() will traverse the channel tree
194  // for the given path, calling the callback on every match of the
195  // specified type. For example: /o*/g*/t?
196  //
197  void channelFind(const char *path, CHfindCallBack callback,
198  void *data = nullptr) const;
199 
200  // These methods will do word finding/replacement in all expressions of
201  // all channels.
202  void findString(const char *word, bool fullword,
203  bool usewildcards, CHfindCallBack callback,
204  void *data = nullptr);
205  int findString(const char *word, bool fullword,
206  bool usewildcards) const;
207  int changeString(const char *from, const char *to,
208  bool fullword, int thread);
209 
210  //
211  // The following pure virtuals must be defined by a sub-class.
212  // These provide the collection name and the channel hierarchy.
213  //
214  virtual const UT_StringHolder &getName() const = 0;
215 
216  virtual int getUniqueId() const;
217  virtual CH_Collection * lookupCollection(int id) const;
218 
219  virtual const CH_Collection *getParent() const = 0;
220  virtual CH_Collection *getParent() = 0;
221 
222  virtual const CH_Collection *getChild(const char *name) const = 0;
223  virtual CH_Collection *getChild(const char *name) = 0;
224 
225 
226  // This is a mean of accessing the node owner from CH library
227  // It's used only the know if the owner is valid and OP_Node isn't used
228  virtual const void *getOwner() const = 0;
229  virtual void *getOwner() = 0;
230 
231  //
232  // These methods only get child collections, not channels
233  virtual int getChildren(CH_NameList &names) const =0;
234  virtual int getChildren(CH_CollectionList &grps) const =0;
235 
236 
237  virtual bool getVariableValue(UT_String &str,
238  int table, int index,
239  int thread);
240  virtual bool getVariableValue(fpreal &val,
241  int table, int index,
242  int thread);
243 
244  virtual const CH_LocalVariable *resolveVariable(const char *name, int &tab);
245 
246  //
247  // I/O methods
248  //
249  int save(std::ostream &os, bool binary, bool compiled,
250  bool scoped, bool pending_state,
251  const CH_ChannelList *channels) const;
252  int saveToFile(const char *nm) const;
253 
254  template <typename FPREAL_TYPE>
255  bool load(UT_IStream &is, const char *path = nullptr,
256  const CH_LoadInfo *load_info = nullptr,
257  CH_ChannelList *loadedChannels = nullptr,
258  bool delete_keys = false);
259  int loadFromFile(const char *nm);
260 
261  //
262  // Keyframe management & convenience function
263  //
264  fpreal findKey(fpreal gtime, int dir) const;
265  virtual void saveKeyFrameForUndo(CH_Channel *chp, fpreal gtime) = 0;
266 
267  /// Save the entire channel for undo
268  virtual void saveChannelForUndo(CH_Channel *chp) = 0;
269 
270  // int anyModified() const;
271  // void clearModified();
272 
273  int getChanged() const { return myModifiedFlag; }
274  virtual void setChanged(CH_CHANGE_TYPE how, void *data = nullptr);
275 
276  void setExpose( bool onoff );
277  bool getExpose() const { return myExpose; }
278 
279  //
280  // Remove and delete all channels
281  //
282  void clearAndDestroy();
283 
284  void initManager(CH_Manager *manager);
285 
287  { myVariables[0] = table; }
288 
290  { return myManager->getGlobalStart(); }
292  { return myManager->getGlobalEnd(); }
293 
294  // returns the 'range' of the collection - the lowest start time and the
295  // highest end time. Returns false if the collection has no 'range'.
296  // Single expressions do not set the range, but will set the timedep
297  // flag if necessary. The timedep flag is set if even one of the channels
298  // is time dependent.
299  bool getCollectionRange(fpreal &start, fpreal &end,
300  bool &timedep, int thread);
301 
303  { myVariables[3] = table; }
304 
305  // This method should only be called by CH_Manager. It signifies
306  // this collection is dependent on a param function and will be
307  // calledback on modification until removeParamDependency is called.
308 
309  void addedParamDependency();
310  void removeParamDependency();
311 
312  // Returns the xform order for this collection given a specific channel.
313  // This defaults to XYZ, but can be overridden by OP_Node types
314  // to handle their specific orders, such as with Objects.
315  virtual void getXformOrder(UT_XformOrder &xord, const CH_Channel *chp) const;
316 
317  // The "external" flag marks this CH_Collection object as not belonging to
318  // any node (ie. standalone).
319  // @{
320  bool getExternal() const
321  { return myExternal; }
322  void setExternal(bool yesno)
323  { myExternal = yesno; }
324  // @}
325 
326  // Set the alias on a channel -- returns true if the alias was
327  // successfully set and false otherwise
328  bool setChannelAlias(CH_Channel &chp,
329  const UT_StringHolder &alias);
330 
331  // Returns true if s represents a valid Channel name and false otherwise
332  static bool isValidChannelName(const char *s);
333 
334  // Returns a valid pointer if this instance is of an OP_Channel and
335  // nullptr otherwise.
336  virtual OP_Channels *castToOPChannels() { return nullptr; }
337  virtual const OP_Channels *castToOPChannels() const { return nullptr; }
338 
339  // Overridden by OP_Channels to add dependencies for local variables
340  virtual void addLocalVarDependency(int table, int index,
341  void *ref_id);
342 
343  CH_ExprLanguage getExprLanguage() const;
344  void setExprLanguage(CH_ExprLanguage language);
345 
346  virtual void getLocalVariables(UT_StringArray &out_vars);
347 
348 protected:
349 
350  // checkExpressionError() is split up into a checking part and an actual
351  // error handling part, evaluationError() so that the checking part can
352  // be inlined.
353  SYS_FORCE_INLINE void
354  checkEvaluationError(const CH_Channel *chp, int thread) const
355  {
356  int err = ev_GetErrNo(thread);
357  if (err != EV_NOERROR)
358  evaluationError(chp, err, thread);
359  }
360 
362  { myVariables[2] = table; }
363  const CH_Collection *getRoot() const;
364  //
365  // Don't publicize our implementation:
366  //
367  const CH_ChannelList *getChannels() const { return &myChannels; }
368 
369  virtual void notifyChannelRenameDependents(const char *old_name,
370  const char *new_name);
371 
372 public:
373  static const char *theValidChannelNameWordBreaks;
374 
375 private:
376  CH_Manager *myManager;
377  CH_LocalVariable *myVariables[4]; // Local var tables
378  CH_ChannelMap myChannelMap;
379  CH_ChannelList myChannels; // For easy traversal
381  myChannelAliasesTable;
382 
383  int myModifiedFlag; // My modified flag
384 
385  CH_ExprLanguage myExprLanguage;
386 
387  char myParamDependent; // relies on param function
388 
389  bool myExternal;
390  bool myExpose;
391 
392  // evaluationError() generates the appropriate call to opError() and then
393  // clears the expression error state.
394  void evaluationError(const CH_Channel *chp, int err,
395  int thread) const;
396 
397  void deleteChannel(exint index); // Delete from array
398 
399  template <typename FPREAL_TYPE>
400  CH_Channel * loadChannelRange(UT_IStream &is, const char *name,
401  const CH_LoadInfo &load_info);
402 
403  friend class CH_Manager;
404 };
405 
406 CH_API extern void
407 CHcollectionsToOPchannels( CH_CollectionList const& collections,
408  UT_ValArray< OP_Channels* > &op_channels );
409 
410 #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: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:284
**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:278
#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)
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:566