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 *curr_is;
88  int is_binary;
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, int binary, bool compiled,
248  int scope,
249  const CH_ChannelList *channels = nullptr) const;
250  int saveToFile(const char *nm) const;
251 
252  template <typename FPREAL_TYPE>
253  bool load(UT_IStream &is, const char *path = 0,
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  // int anyModified() const;
266  // void clearModified();
267 
268  int getChanged() const { return myModifiedFlag; }
269  virtual void setChanged(CH_CHANGE_TYPE how, void *data = nullptr);
270 
271  void setExpose( bool onoff );
272  bool getExpose() const { return myExpose; }
273 
274  //
275  // Remove and delete all channels
276  //
277  void clearAndDestroy();
278 
279  void initManager(CH_Manager *manager);
280 
282  { myVariables[0] = table; }
283 
285  { return myManager->getGlobalStart(); }
287  { return myManager->getGlobalEnd(); }
288 
289  // returns the 'range' of the collection - the lowest start time and the
290  // highest end time. Returns false if the collection has no 'range'.
291  // Single expressions do not set the range, but will set the timedep
292  // flag if necessary. The timedep flag is set if even one of the channels
293  // is time dependent.
294  bool getCollectionRange(fpreal &start, fpreal &end,
295  bool &timedep, int thread);
296 
298  { myVariables[3] = table; }
299 
300  // This method should only be called by CH_Manager. It signifies
301  // this collection is dependent on a param function and will be
302  // calledback on modification until removeParamDependency is called.
303 
304  void addedParamDependency();
305  void removeParamDependency();
306 
307  // Returns the xform order for this collection given a specific channel.
308  // This defaults to XYZ, but can be overridden by OP_Node types
309  // to handle their specific orders, such as with Objects.
310  virtual void getXformOrder(UT_XformOrder &xord, const CH_Channel *chp) const;
311 
312  // The "external" flag marks this CH_Collection object as not belonging to
313  // any node (ie. standalone).
314  // @{
315  bool getExternal() const
316  { return myExternal; }
317  void setExternal(bool yesno)
318  { myExternal = yesno; }
319  // @}
320 
321  // Set the alias on a channel -- returns true if the alias was
322  // successfully set and false otherwise
323  bool setChannelAlias(CH_Channel &chp,
324  const UT_StringHolder &alias);
325 
326  // Returns true if s represents a valid Channel name and false otherwise
327  static bool isValidChannelName(const char *s);
328 
329  // Returns a valid pointer if this instance is of an OP_Channel and
330  // nullptr otherwise.
331  virtual OP_Channels *castToOPChannels() { return nullptr; }
332  virtual const OP_Channels *castToOPChannels() const { return nullptr; }
333 
334  // Overridden by OP_Channels to add dependencies for local variables
335  virtual void addLocalVarDependency(int table, int index,
336  void *ref_id);
337 
338  CH_ExprLanguage getExprLanguage() const;
339  void setExprLanguage(CH_ExprLanguage language);
340 
341  virtual void getLocalVariables(UT_StringArray &out_vars);
342 
343 protected:
344 
345  // checkExpressionError() is split up into a checking part and an actual
346  // error handling part, evaluationError() so that the checking part can
347  // be inlined.
348  SYS_FORCE_INLINE void
349  checkEvaluationError(const CH_Channel *chp, int thread) const
350  {
351  int err = ev_GetErrNo(thread);
352  if (err != EV_NOERROR)
353  evaluationError(chp, err, thread);
354  }
355 
357  { myVariables[2] = table; }
358  const CH_Collection *getRoot() const;
359  //
360  // Don't publicize our implementation:
361  //
362  const CH_ChannelList *getChannels() const { return &myChannels; }
363 
364  virtual void notifyChannelRenameDependents(const char *old_name,
365  const char *new_name);
366 
367 private:
368  CH_Manager *myManager;
369  CH_LocalVariable *myVariables[4]; // Local var tables
370  CH_ChannelMap myChannelMap;
371  CH_ChannelList myChannels; // For easy traversal
373  myChannelAliasesTable;
374 
375  int myModifiedFlag; // My modified flag
376 
377  CH_ExprLanguage myExprLanguage;
378 
379  char myParamDependent; // relies on param function
380 
381  bool myExternal;
382  bool myExpose;
383 
384  // evaluationError() generates the appropriate call to opError() and then
385  // clears the expression error state.
386  void evaluationError(const CH_Channel *chp, int err,
387  int thread) const;
388 
389  void deleteChannel(exint index); // Delete from array
390 
391  template <typename FPREAL_TYPE>
392  CH_Channel * loadChannelRange(UT_IStream &is, const char *name,
393  const CH_LoadInfo &load_info);
394 
395  friend class CH_Manager;
396 };
397 
398 CH_API extern void
399 CHcollectionsToOPchannels( CH_CollectionList const& collections,
400  UT_ValArray< OP_Channels* > &op_channels );
401 
402 #endif
GLdouble s
Definition: glew.h:1390
CH_Manager * getManager() const
#define SETUPCLASSPACKETID(classname)
Definition: UT_Packet.h:90
#define EV_NOERROR
Definition: EX_Error.h:18
int(* CHfindCallBack)(CH_Collection *grp, CH_Channel *chp, void *)
Definition: CH_Types.h:31
GLuint const GLchar * name
Definition: glew.h:1814
CH_ExprLanguage
FMT_CONSTEXPR auto begin(const C &c) -> decltype(c.begin())
Definition: format.h:251
GT_API const UT_StringHolder time
GLuint index
Definition: glew.h:1814
GLuint const GLfloat * val
Definition: glew.h:2794
OIIO_API bool rename(string_view from, string_view to, std::string &err)
void setExternal(bool yesno)
GLenum GLsizei GLenum GLenum const void * table
Definition: glew.h:4940
bool getExpose() const
void updateGlobalVariables(CH_LocalVariable *table)
GLenum GLint GLuint mask
Definition: glew.h:1845
void addLocalVariables(CH_LocalVariable *table)
void addDynamicVariables(CH_LocalVariable *table)
const CH_Channel * getChannel(int idx) const
long long int64
Definition: SYS_Types.h:111
fpreal getGlobalEnd() const
CH_CHANGE_TYPE
bool getExternal() const
CH_LOCAL_VARIABLE
Definition: CH_Collection.h:45
int64 exint
Definition: SYS_Types.h:120
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
void getFullPath(const CH_Channel *chp, UT_String &p, bool use_alias=false) const
const GLuint GLenum const void * binary
Definition: glew.h:3502
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
GLuint GLuint end
Definition: glew.h:1253
GLuint const GLuint * names
Definition: glew.h:2690
CH_Channel * getChannel(int idx)
*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
fpreal getTolerance() const
const CH_ChannelList * getChannels() const
CH_API void CHcollectionsToOPchannels(CH_CollectionList const &collections, UT_ValArray< OP_Channels * > &op_channels)
virtual OP_Channels * castToOPChannels()
unsigned int uint
Definition: SYS_Types.h:44
GLuint start
Definition: glew.h:1253
SYS_FORCE_INLINE void checkEvaluationError(const CH_Channel *chp, int thread) const
GLsizei const GLchar *const * path
Definition: glew.h:6461
fpreal getGlobalStart() const
GLfloat GLfloat p
Definition: glew.h:16321
void adopt(char *s)
Definition: UT_String.h:275
double fpreal
Definition: SYS_Types.h:276
UT_ArrayStringMap< CH_Channel * > CH_ChannelMap
Definition: CH_Collection.h:40
#define CH_API
Definition: CH_API.h:10
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:47
GLuint64EXT * result
Definition: glew.h:14007
exint entries() const
virtual const OP_Channels * castToOPChannels() const
GLubyte * pattern
Definition: glew.h:5711
int getChanged() const
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)