HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GA_AIFSharedDictTuple.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: GA_AIFSharedDictTuple.h ( GA Library, C++)
7  *
8  * COMMENTS: Attribute Interface class for options index table methods
9  */
10 
11 #ifndef __GA_AIFSharedDictTuple__
12 #define __GA_AIFSharedDictTuple__
13 
14 #include "GA_API.h"
15 
16 #include "GA_BlobContainer.h"
17 #include "GA_Types.h"
18 
19 #include <UT/UT_Array.h>
20 #include <UT/UT_VectorTypes.h>
21 #include <UT/UT_Options.h>
22 
23 #include <stddef.h>
24 
25 
26 class GA_Attribute;
27 class GA_Range;
28 
29 
30 /// Index type
31 /// WARNING: We assume that GA_DictIndexType and GA_BlobIndexType
32 /// are binary compatible, so do not change this!
34 #define GA_INVALID_DICT_INDEX GA_DictIndexType(-1)
35 
37 {
38 public:
40  : myCapacity(0)
41  , myEntries(0)
42  {
43  }
45 
46  GA_Size getEntries() const { return myEntries; }
47  GA_Size getCapacity() const { return myCapacity; }
48 
49  void setEntries(GA_Size n) { myEntries = n; }
50  void setCapacity(GA_Size n) { myCapacity = n; }
51 private:
52  GA_Size myEntries, myCapacity;
53 };
54 
55 /// @brief A specialization of GA_AIFDictTuple to access "shared strings"
56 ///
57 /// This class provides the interface to access string table data. Each
58 /// attribute type may provide this interface if it makes sense.
60 {
61 public:
63  virtual ~GA_AIFSharedDictTuple();
64 
65  /// Query information about the string storage.
66  virtual bool getStatistics(const GA_Attribute *attrib,
67  GA_DictTableStatistics &stats) const = 0;
68 
69  /// Compact the string storage
70  virtual bool compactStorage(GA_Attribute *attrib) const = 0;
71 
72  /// Extract data from the string table. This will extract all the unique
73  /// strings which are referenced by the attribute.
74  /// The string handles are guaranteed to be in ascending order, but
75  /// may or may not be contiguous.
76  virtual bool extractDicts(const GA_Attribute *attrib,
78  UT_IntArray &handles) const = 0;
79 
80  /// Extract all of the unique string handles of the attribute.
81  /// The string handles are guaranteed to be in ascending order, but
82  /// may or may not be contiguous.
83  virtual bool extractHandles(const GA_Attribute *attrib,
84  UT_IntArray &handles) const = 0;
85 
86 public:
87  // --------------------------------------------------------------
88  // Interface to deal with string handles.
89  // --------------------------------------------------------------
90 
91  /// Return the number of entries in the shared string table
92  GA_Size getTableEntries(const GA_Attribute *attrib) const
93  {
95  if (attrib && getStatistics(attrib, stats))
96  return stats.getCapacity();
97  return 0;
98  }
99 
100  /// It's possible that there are invalid handle indexes mixed with the
101  /// valid handles. When iterating over all the handles, you can call @c
102  /// validateTableHandle() which will ensure that there's a string
103  /// associated with the given handle.
104  virtual GA_DictIndexType validateTableHandle(const GA_Attribute *attrib,
105  GA_DictIndexType index) const = 0;
106 
107  /// Get a string from the string table (without going through an attribute)
108  virtual UT_OptionsHolder getTableDict(const GA_Attribute *attrib,
109  GA_DictIndexType handle) const = 0;
110 
111  /// Get the handle (index) corresponding to the given string, returning -1
112  /// if none.
113  virtual GA_DictIndexType getTableHandle(const GA_Attribute *attrib,
114  const UT_OptionsRef &opt) const = 0;
115 
116  /// GA_DictIndexType indices may not be contiguous, this method allows
117  /// you to get a string given an ordered index. Strings will be defined
118  /// for all contiguous strings. This may be an expensive operation, it's
119  /// better to access strings by their index if possible.
120  ///
121  /// This method will return a NULL pointer past the end of the string table
122  virtual UT_OptionsHolder getTableOrderedDict(const GA_Attribute *a,
123  exint index) const = 0;
124 
125 
126  /// Replace a string in the shared string table with a new value. Warning,
127  /// it's possible that the table will "collapse" after the replacement.
128  /// For example: @code
129  /// table := [ "foo", "bar" ]
130  /// table.replaceString(1, "foo") # Replace "bar" with "foo"
131  /// table := [ "foo" ]
132  /// @endcode
133  /// In the above example, all elements which originally referenced "bar"
134  /// will now reference "foo". This means that trying to swap two values
135  /// will not work as expected.
136  virtual bool replaceTableDict(GA_Attribute *attrib,
138  const UT_OptionsHolder &opt) const = 0;
139 
140  /// Class to iterate over all the options in the shared options table
142  {
143  public:
145  : myAIF(NULL)
146  , myAttrib(NULL)
147  , myCount(0)
148  , myIndex(0)
149  {
150  }
153  {
154  myAIF = src.myAIF;
155  myAttrib = src.myAttrib;
156  myDict = src.myDict;
157  myCount = src.myCount;
158  myIndex = src.myIndex;
159  return *this;
160  }
161  bool operator==(const iterator &src)
162  {
163  if (!src.myAIF)
164  return atEnd();
165  if (!myAIF)
166  return src.atEnd();
167  return myAIF == src.myAIF &&
168  myAttrib == src.myAttrib &&
169  myCount == src.myCount &&
170  myIndex == src.myIndex;
171  }
172  iterator &operator++() { advance(); return *this; }
173  iterator &operator++(int) { advance(); return *this; }
174 
175  void rewind()
176  {
177  myIndex = 0;
178  setupDict();
179  }
180  bool atEnd() const { return myIndex >= myCount; }
181  void advance()
182  {
183  myIndex++;
184  setupDict();
185  }
186  GA_Size getCount() const { return myCount; }
187  GA_Size getIndex() const { return myIndex; }
189  { return GA_DictIndexType(myIndex); }
190  UT_OptionsHolder getDict() const { return myDict; }
191  private:
192  void setupDict()
193  {
194  while (myIndex < myCount)
195  {
196  if (myAIF->validateTableHandle(myAttrib,GA_DictIndexType(myIndex)) >= 0)
197  {
198  myDict = myAIF->getTableDict(myAttrib, GA_DictIndexType(myIndex));
199  UT_ASSERT_P(!myDict.isEmpty());
200  break;
201  }
202  myIndex++;
203  }
204  }
205  iterator(const GA_AIFSharedDictTuple *aif,
206  const GA_Attribute *a)
207  : myAIF(aif)
208  , myAttrib(a)
209  , myIndex(0)
210  , myCount(aif ? aif->getTableEntries(a) : 0)
211  {
212  setupDict();
213  }
214 
215  const GA_AIFSharedDictTuple *myAIF;
216  const GA_Attribute *myAttrib;
217  UT_OptionsHolder myDict;
218  GA_Size myCount;
219  GA_Size myIndex;
220  friend class GA_AIFSharedDictTuple;
221  };
222 
223  iterator begin(const GA_Attribute *a) const
224  { return iterator(this, a); }
225  iterator end() const
226  { return iterator(); }
227 
228 protected:
229  /// Add (or increment) reference to a string
231  const UT_OptionsHolder &opt) const = 0;
232  /// Decrement reference to a handle
234  GA_DictIndexType handle) const = 0;
235 
236 public:
237  // --------------------------------------------------------------
238  // Tuple Interface
239  // --------------------------------------------------------------
240 
241  /// Query the tuple size
242  virtual int getTupleSize(const GA_Attribute *attrib) const = 0;
243 
244  /// Set the tuple size
245  virtual bool setTupleSize(GA_Attribute *attrib, int size) const = 0;
246 
247  /// Get a string version to save writing marshalling code
248  /// This lets you use this AIF in string-aware code and get sensible
249  /// outputs.
250  /// JSON normally has returns and tabs for formatting, onlyspaces will
251  /// collapse these for a more compact output.
252  UT_StringHolder getString(const GA_Attribute *attrib, GA_Offset offset, int component=0, bool onlyspaces=false) const;
253 
254  /// Get a single string from the array for a single tuple of an element.
255  virtual UT_OptionsHolder getDict(const GA_Attribute *attrib, GA_Offset ai,
256  int tuple_index=0) const;
257 
258  /// Get the handle from the array for a single tuple of an element
259  virtual GA_DictIndexType getHandle(const GA_Attribute *attrib,
260  GA_Offset ai,
261  int tuple_index=0) const = 0;
262 
263  /// Get the full tuple of indices for a single element
264  virtual bool getHandles(const GA_Attribute *attrib, GA_Offset ai,
265  GA_DictIndexType *handles,
266  int count, int start=0) const;
267 
268  /// Set a single component for a single element.
269  virtual bool setDict(GA_Attribute *attrib, GA_Offset ai,
270  const UT_OptionsHolder &opt,
271  int tuple_index) const;
272  /// Set a single component for a range of elements.
273  virtual bool setDict(GA_Attribute *attrib, const GA_Range &ai,
274  const UT_OptionsHolder &opt,
275  int tuple_index) const;
276  /// Set a single component for a single element.
277  virtual bool setHandle(GA_Attribute *attrib, GA_Offset ai,
279  int tuple_index) const = 0;
280  /// Set a single component for a range of elements.
281  virtual bool setHandle(GA_Attribute *attrib, const GA_Range &ai,
283  int tuple_index) const;
284 
285  /// Set multiple components for a single element
286  virtual bool setHandles(GA_Attribute *attrib, GA_Offset ai,
287  const GA_DictIndexType *handles,
288  int count, int start=0) const;
289  /// Set multiple components for a range of elements
290  virtual bool setHandles(GA_Attribute *attrib, const GA_Range &ai,
291  const GA_DictIndexType *handles,
292  int count, int start=0) const;
293 };
294 
295 #endif
296 
UT_StringHolder getString(const GA_Attribute *attrib, GA_Offset offset, int component=0, bool onlyspaces=false) const
virtual void delHandleReference(GA_Attribute *attribute, GA_DictIndexType handle) const =0
Decrement reference to a handle.
Definition of a geometry attribute.
Definition: GA_Attribute.h:198
A specialization of GA_AIFDictTuple to access "shared strings".
virtual bool setTupleSize(GA_Attribute *attrib, int size) const =0
Set the tuple size.
GLuint start
Definition: glcorearb.h:475
int64 exint
Definition: SYS_Types.h:125
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
#define GA_API
Definition: GA_API.h:14
exint GA_Size
Defines the bit width for index and offset types in GA.
Definition: GA_Types.h:235
A range of elements in an index-map.
Definition: GA_Range.h:42
GA_Size getTableEntries(const GA_Attribute *attrib) const
Return the number of entries in the shared string table.
GA_Size GA_Offset
Definition: GA_Types.h:641
GLdouble n
Definition: glcorearb.h:2008
GLintptr offset
Definition: glcorearb.h:665
GA_DictIndexType getHandle() const
virtual int getTupleSize(const GA_Attribute *attrib) const =0
Query the tuple size.
iterator begin(const GA_Attribute *a) const
UT_IndexedHashMapItemId GA_BlobIndex
GA_BlobIndex GA_DictIndexType
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:155
virtual UT_OptionsHolder getDict(const GA_Attribute *attrib, GA_Offset ai, int tuple_index=0) const
Get a single string from the array for a single tuple of an element.
Class to iterate over all the options in the shared options table.
virtual bool setDict(GA_Attribute *attrib, GA_Offset ai, const UT_OptionsHolder &opt, int tuple_index) const
Set a single component for a single element.
virtual GA_DictIndexType getHandle(const GA_Attribute *attrib, GA_Offset ai, int tuple_index=0) const =0
Get the handle from the array for a single tuple of an element.
UT_OptionsHolder getDict() const
virtual bool setHandles(GA_Attribute *attrib, GA_Offset ai, const GA_DictIndexType *handles, int count, int start=0) const
Set multiple components for a single element.
virtual bool setHandle(GA_Attribute *attrib, GA_Offset ai, GA_DictIndexType handle, int tuple_index) const =0
Set a single component for a single element.
GLsizeiptr size
Definition: glcorearb.h:664
virtual GA_DictIndexType addDictReference(GA_Attribute *attribute, const UT_OptionsHolder &opt) const =0
Add (or increment) reference to a string.
GLuint index
Definition: glcorearb.h:786
virtual bool getHandles(const GA_Attribute *attrib, GA_Offset ai, GA_DictIndexType *handles, int count, int start=0) const
Get the full tuple of indices for a single element.
OIIO_API bool attribute(string_view name, TypeDesc type, const void *val)
iterator & operator=(const iterator &src)
bool operator==(const iterator &src)
GLint GLsizei count
Definition: glcorearb.h:405
GLenum src
Definition: glcorearb.h:1793