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