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,
137  GA_DictIndexType handle,
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 
174  SYS_DEPRECATED_REPLACE(22.0, prefix increment operator)
175  iterator &operator++(int) { advance(); return *this; }
176 
177  void rewind()
178  {
179  myIndex = 0;
180  setupDict();
181  }
182  bool atEnd() const { return myIndex >= myCount; }
183  void advance()
184  {
185  myIndex++;
186  setupDict();
187  }
188  GA_Size getCount() const { return myCount; }
189  GA_Size getIndex() const { return myIndex; }
191  { return GA_DictIndexType(myIndex); }
192  UT_OptionsHolder getDict() const { return myDict; }
193  private:
194  void setupDict()
195  {
196  while (myIndex < myCount)
197  {
198  if (myAIF->validateTableHandle(myAttrib,GA_DictIndexType(myIndex)) >= 0)
199  {
200  myDict = myAIF->getTableDict(myAttrib, GA_DictIndexType(myIndex));
201  UT_ASSERT_P(!myDict.isEmpty());
202  break;
203  }
204  myIndex++;
205  }
206  }
207  iterator(const GA_AIFSharedDictTuple *aif,
208  const GA_Attribute *a)
209  : myAIF(aif)
210  , myAttrib(a)
211  , myIndex(0)
212  , myCount(aif ? aif->getTableEntries(a) : 0)
213  {
214  setupDict();
215  }
216 
217  const GA_AIFSharedDictTuple *myAIF;
218  const GA_Attribute *myAttrib;
219  UT_OptionsHolder myDict;
220  GA_Size myCount;
221  GA_Size myIndex;
222  friend class GA_AIFSharedDictTuple;
223  };
224 
225  iterator begin(const GA_Attribute *a) const
226  { return iterator(this, a); }
227  iterator end() const
228  { return iterator(); }
229 
230 protected:
231  /// Add (or increment) reference to a string
233  const UT_OptionsHolder &opt) const = 0;
234  /// Decrement reference to a handle
236  GA_DictIndexType handle) const = 0;
237 
238 public:
239  // --------------------------------------------------------------
240  // Tuple Interface
241  // --------------------------------------------------------------
242 
243  /// Query the tuple size
244  virtual int getTupleSize(const GA_Attribute *attrib) const = 0;
245 
246  /// Set the tuple size
247  virtual bool setTupleSize(GA_Attribute *attrib, int size) const = 0;
248 
249  /// Get a string version to save writing marshalling code
250  /// This lets you use this AIF in string-aware code and get sensible
251  /// outputs.
252  /// JSON normally has returns and tabs for formatting, onlyspaces will
253  /// collapse these for a more compact output.
254  UT_StringHolder getString(const GA_Attribute *attrib, GA_Offset offset, int component=0, bool onlyspaces=false) const;
255 
256  /// Get a single string from the array for a single tuple of an element.
257  virtual UT_OptionsHolder getDict(const GA_Attribute *attrib, GA_Offset ai,
258  int tuple_index=0) const;
259 
260  /// Get the handle from the array for a single tuple of an element
261  virtual GA_DictIndexType getHandle(const GA_Attribute *attrib,
262  GA_Offset ai,
263  int tuple_index=0) const = 0;
264 
265  /// Get the full tuple of indices for a single element
266  virtual bool getHandles(const GA_Attribute *attrib, GA_Offset ai,
267  GA_DictIndexType *handles,
268  int count, int start=0) const;
269 
270  /// Set a single component for a single element.
271  virtual bool setDict(GA_Attribute *attrib, GA_Offset ai,
272  const UT_OptionsHolder &opt,
273  int tuple_index) const;
274  /// Set a single component for a range of elements.
275  virtual bool setDict(GA_Attribute *attrib, const GA_Range &ai,
276  const UT_OptionsHolder &opt,
277  int tuple_index) const;
278  /// Set a single component for a single element.
279  virtual bool setHandle(GA_Attribute *attrib, GA_Offset ai,
280  GA_DictIndexType handle,
281  int tuple_index) const = 0;
282  /// Set a single component for a range of elements.
283  virtual bool setHandle(GA_Attribute *attrib, const GA_Range &ai,
284  GA_DictIndexType handle,
285  int tuple_index) const;
286 
287  /// Set multiple components for a single element
288  virtual bool setHandles(GA_Attribute *attrib, GA_Offset ai,
289  const GA_DictIndexType *handles,
290  int count, int start=0) const;
291  /// Set multiple components for a range of elements
292  virtual bool setHandles(GA_Attribute *attrib, const GA_Range &ai,
293  const GA_DictIndexType *handles,
294  int count, int start=0) const;
295 };
296 
297 #endif
298 
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:203
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:243
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.
#define SYS_DEPRECATED_REPLACE(__V__, __R__)
GA_Size GA_Offset
Definition: GA_Types.h:653
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:164
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