HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GA_ATIDict.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_ATIDict.h ( GA Library, C++)
7  *
8  * COMMENTS: Dict ATI (Attribute Type Implementation)
9  */
10 
11 #pragma once
12 
13 #ifndef __GA_ATIDict__
14 #define __GA_ATIDict__
15 
16 #include "GA_API.h"
17 #include "GA_AIFSharedDictTuple.h" // For GA_DictIndexType type
18 #include "GA_ATIBlob.h"
19 #include "GA_Attribute.h"
20 #include "GA_AttributeType.h"
21 #include "GA_PageArray.h"
22 #include "GA_Types.h"
23 
24 #include <UT/UT_ArrayMap.h>
25 #include <UT/UT_Options.h>
26 #include <UT/UT_StringHolder.h>
27 
28 #include <SYS/SYS_Inline.h>
29 #include <SYS/SYS_Types.h>
30 
31 
32 class GA_AIFJSON;
33 class GA_IndexMap;
34 class GA_Range;
35 
36 
38 {
39 public:
40  static void registerType();
42  static const UT_StringHolder &getTypeName()
43  { return theAttributeType->getTypeName(); }
45  static const GA_AttributeType &getType() { return *theAttributeType; }
46 
48  static bool isType(const GA_Attribute *attrib)
49  {
50  return attrib && &attrib->getType() == theAttributeType;
51  }
53  static GA_ATIDict *cast(GA_Attribute *attrib)
54  {
55  if (attrib && &attrib->getType() == theAttributeType)
56  return static_cast<GA_ATIDict *>(attrib);
57  return nullptr;
58  }
60  static const GA_ATIDict *cast(const GA_Attribute *attrib)
61  {
62  if (attrib && &attrib->getType() == theAttributeType)
63  return static_cast<const GA_ATIDict *>(attrib);
64  return nullptr;
65  }
66 
67  static GA_Attribute *create(const GA_IndexMap &index_map,
68  GA_AttributeScope scope,
69  const UT_StringHolder &name,
70  int tuple_size,
71  const GA_AttributeOptions *attribute_options=nullptr);
72  static GA_Attribute *create(const GA_IndexMap &index_map,
73  const UT_StringHolder &name,
74  int tuple_size)
75  { return create(index_map, GA_SCOPE_PUBLIC, name, tuple_size); }
76 
78  const GA_IndexMap &index_map,
79  GA_AttributeScope scope,
80  const UT_StringHolder &name,
81  int tuple_size);
82  ~GA_ATIDict() override;
83 
84  // AIFCopyData is implemented in GA_ATIBlob.
85  // AIFInterp is implemented in GA_ATIBlob.
87  { return myAIFSharedDictTuple; }
88 
89  /// @section JSON-GA_ATIDict JSON Schema: GA_ATIDict
90  /// @code
91  /// {
92  /// "name" : "GA_ATIDict",
93  /// "description" : "An tuple array of indexed options",
94  /// "type" : "orderedmap",
95  /// "properties": {
96  /// "size": {
97  /// "type" : "integer",
98  /// "minimum" : 1,
99  /// "description" : "Tuple size",
100  /// "default" : 1,
101  /// "optional" : true,
102  /// },
103  /// "storage": {
104  /// "type" : "string",
105  /// "description" : "Tuple storage",
106  /// "enum" : [ "int8", "int16", "int32", "int64" ],
107  /// },
108  /// "options": {
109  /// "type" : { "$ref":"GA_BlobData", },
110  /// "description" : "Array of options used in the attribute",
111  /// },
112  /// "indices": {
113  /// "type" : {"$ref":"GA_DataArrayTuple"},
114  /// "description" : "Int index for each element of the array",
115  /// },
116  /// },
117  /// }
118  /// @endcode
119  /// @see @ref JSON_FileFormat
120  const GA_AIFJSON *getAIFJSON() const override { return myAIFJSON; }
121 
122  /// Debug code
123  bool debugValidateArrayCapacity(GA_Offset sz) const override;
124 
125  /// @{
126  /// Get a string version to save writing marshalling code
127  /// This lets you use this AIF in string-aware code and get sensible
128  /// outputs.
129  /// JSON normally has returns and tabs for formatting, compact will
130  /// collapse these for a more compact output.
131  UT_StringHolder getString(GA_Offset offset, int component=0, bool compact=false) const;
132  /// @}
133 
134  /// Get a string
135  const UT_OptionsHolder &getDict(GA_Offset offset, int component=0) const;
136  const UT_OptionsHolder &getHolder(GA_Offset offset, int component=0) const
137  { return getDict(offset, component); }
138  GA_DictIndexType getDictIndex(GA_Offset offset, int component=0) const;
140  { return getDictIndex(offset, component); }
141  /// @}
142  /// @{
143  /// Set a string
144  void setDict(GA_Offset offset, const UT_OptionsHolder &opt, int component=0);
145  void setHolder(GA_Offset offset, const UT_OptionsHolder &opt, int component=0)
146  { return setDict(offset, opt, component); }
147  void setDictIndex(GA_Offset offset, GA_DictIndexType i, int component=0)
148  { tupleSet(offset, i, component); }
150  { setDictIndex(offset, i, component); }
151  /// @}
152 
153  UT_OptionsHolder lookupDict(GA_DictIndexType handle) const;
154  UT_OptionsHolder lookupOrderedDict(exint handle) const;
155  GA_DictIndexType lookupHandle(const UT_OptionsRef &opt) const;
156  GA_DictIndexType validateHandle(GA_DictIndexType h) const;
157  bool replaceDict(GA_DictIndexType handle,
158  const UT_OptionsHolder &opt);
159 
161 
162  /// NOTE: This only provides read access, since for writing,
163  /// myHandles and myStrings need to be kept in sync.
165  { return myHandles; }
166 
167  /// Returns true iff the specified page is constant-compressed,
168  /// where a single string index is stored to represent GA_PAGE_SIZE
169  /// values in the page, (or possibly fewer if only one page).
171  {
172  return myHandles.isPageConstant(pagenum);
173  }
174 
175 protected:
176  // Create a new ATIBlob
177  GA_Attribute *doClone(const GA_IndexMap &index_map,
178  const UT_StringHolder &name) const override;
179 
180 private:
181  /// @{ GA_AIFDictTuple
182  /// @warning If you call addDictReference(), at some point, you must call
183  /// delHandleReference() on the string you added.
184  GA_DictIndexType addDictReference(const UT_OptionsHolder &opt);
185  void delHandleReference(GA_DictIndexType handle);
186  bool tupleGet(GA_Offset di, GA_DictIndexType &v, int vi) const;
187  bool tupleSet(GA_Offset di, GA_DictIndexType v, int vi);
188  /// @}
189 
190  static GA_AIFSharedDictTuple *myAIFSharedDictTuple;
191  static GA_AIFJSON *myAIFJSON;
192 
193  static const GA_AttributeType *theAttributeType;
194 
195  /// @cond INTERNAL_DOX
196  friend class ga_DictCopyData;
197  friend class ga_DictMerge;
198  friend class ga_DictCompare;
199  friend class ga_DictTuple;
200  friend class ga_DictJSON;
201  friend class GA_ATIDictDelayedWriter;
202  /// @endcond
203 };
204 
205 //
206 // Designed to delay all the reference count adjustments
207 // until this object is flushed. Also maintains a local
208 // table for looking up the option indices with, thus avoiding
209 // concurrency issues.
210 //
212 {
213 public:
217 
218  void bind(GA_ATIDict *attrib);
219 
220  // The holder version should be preferred as it can store the hash
221  // and avoid hardening to the internal map.
222  void setDict(GA_Offset offset, const UT_OptionsHolder &opt, int tuple=0);
223  void setHolder(GA_Offset offset, const UT_OptionsHolder &opt, int tuple=0)
224  { setDict(offset, opt, tuple); }
225  void setDict(const GA_Range &range, const UT_OptionsHolder &opt, int tuple=0);
226  void setHolder(const GA_Range &range, const UT_OptionsHolder &opt, int tuple=0)
227  { setDict(range, opt, tuple); }
228  // Directly set the index.
229  void setDictIndex(GA_Offset offset, GA_DictIndexType idx, int tuple=0);
230  void setHolderIndex(GA_Offset offset, GA_DictIndexType idx, int tuple=0)
231  { setDictIndex(offset, idx, tuple); }
232  void setDictIndex(const GA_Range &range, GA_DictIndexType idx, int tuple=0);
233  void setHolderIndex(const GA_Range &range, GA_DictIndexType idx, int tuple=0)
234  { setDictIndex(range, idx, tuple); }
235 
236  void setPageDict(GA_PageNum pagenum, const UT_OptionsHolder &opt, int tuple=0);
237  void setPageDictIndex(GA_PageNum pagenum, GA_DictIndexType opt, int tuple=0);
238 
239  void flush();
240 
242  {
243  return myHasChanges;
244  }
245 
246  // Returns the index to be used for the dictionary, adding it to the
247  // dictionary table if necessary.
248  GA_DictIndexType addDictReference(const UT_OptionsHolder &opt);
249 
250 private:
251  GA_ATIDict *myAttribute;
252  volatile bool myHasChanges;
253 
254  UT_ArrayMap<GA_DictIndexType, exint> myRefCountChanges;
256 };
257 
258 inline GA_DictIndexType
260 {
262 
263  // See if already present...
264  // We can't just test if it is already present, we must
265  // ensure we have a copy being held onto by our table or
266  // we could have it eaten by another thread!
267  if (!opt.isEmpty())
268  {
269  index = myAttribute->addDictReference(opt);
270 
271  // Add a reference. This means we will *not* count
272  // as a reference count!
273  myRefCountChanges[index] += -1;
274  }
275 
276  return index;
277 }
278 
279 #endif
A class to manage an ordered array which has fixed offset handles.
Definition: GA_IndexMap.h:63
HandleArrayType myHandles
Definition: GA_ATIBlob.h:463
GA_DictIndexType addDictReference(const UT_OptionsHolder &opt)
Definition: GA_ATIDict.h:259
Definition of a geometry attribute.
Definition: GA_Attribute.h:198
GLenum GLint * range
Definition: glcorearb.h:1925
A specialization of GA_AIFDictTuple to access "shared strings".
const GLdouble * v
Definition: glcorearb.h:837
bool isEmpty() const
Definition: UT_Options.h:907
GA_Attribute * doClone(const GA_IndexMap &index_map, const UT_StringHolder &name) const override
Create a new ATIBlob attribute. Sub-classes must implement this.
void setHolder(const GA_Range &range, const UT_OptionsHolder &opt, int tuple=0)
Definition: GA_ATIDict.h:226
int64 exint
Definition: SYS_Types.h:125
static SYS_FORCE_INLINE const GA_AttributeType & getType()
Definition: GA_ATIDict.h:45
A simple ATI to store aribtrary "blobs" of data in an attribute.
Definition: GA_ATIBlob.h:92
#define GA_API
Definition: GA_API.h:14
Standard user attribute level.
Definition: GA_Types.h:148
static SYS_FORCE_INLINE GA_ATIDict * cast(GA_Attribute *attrib)
Definition: GA_ATIDict.h:53
SYS_FORCE_INLINE const GA_AttributeType & getType() const
Definition: GA_Attribute.h:206
void setHolderIndex(const GA_Range &range, GA_DictIndexType idx, int tuple=0)
Definition: GA_ATIDict.h:233
A range of elements in an index-map.
Definition: GA_Range.h:42
GA_Size GA_Offset
Definition: GA_Types.h:641
static SYS_FORCE_INLINE const UT_StringHolder & getTypeName()
Definition: GA_ATIDict.h:42
static void registerType()
GA_AttributeScope
Definition: GA_Types.h:142
SYS_FORCE_INLINE bool hasChanges() const
Definition: GA_ATIDict.h:241
#define GA_INVALID_DICT_INDEX
GLintptr offset
Definition: glcorearb.h:665
static GA_Attribute * create(const GA_IndexMap &index_map, const UT_StringHolder &name, int tuple_size)
Definition: GA_ATIDict.h:72
const GA_AIFSharedDictTuple * getAIFSharedDictTuple() const override
Return the attribute's shared options tuple interface or NULL.
Definition: GA_ATIDict.h:86
GA_BlobIndex GA_DictIndexType
void setHolderIndex(GA_Offset offset, GA_DictIndexType idx, int tuple=0)
Definition: GA_ATIDict.h:230
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
SYS_FORCE_INLINE bool isPageConstant(GA_PageNum pagenum) const
Definition: GA_ATIDict.h:170
void setHolder(GA_Offset offset, const UT_OptionsHolder &opt, int component=0)
Definition: GA_ATIDict.h:145
GLuint const GLchar * name
Definition: glcorearb.h:786
const HandleArrayType & getHandleData() const
Definition: GA_ATIDict.h:164
void setHolderIndex(GA_Offset offset, GA_DictIndexType i, int component=0)
Definition: GA_ATIDict.h:149
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
GA_DictIndexType getHolderIndex(GA_Offset offset, int component=0) const
Definition: GA_ATIDict.h:139
static GA_Attribute * create(const GA_IndexMap &index_map, GA_AttributeScope scope, const UT_StringHolder &name, int tuple_size, const GA_AttributeOptions *attribute_options=NULL)
GA_Size GA_PageNum
Definition: GA_Types.h:644
static SYS_FORCE_INLINE const GA_ATIDict * cast(const GA_Attribute *attrib)
Definition: GA_ATIDict.h:60
GLuint index
Definition: glcorearb.h:786
static SYS_FORCE_INLINE bool isType(const GA_Attribute *attrib)
Definition: GA_ATIDict.h:48
const GA_AIFJSON * getAIFJSON() const override
Definition: GA_ATIDict.h:120
Attribute Interface for file I/O.
Definition: GA_AIFJSON.h:39
virtual bool debugValidateArrayCapacity(GA_Offset sz) const
Debug validation of allocated array size.
void setHolder(GA_Offset offset, const UT_OptionsHolder &opt, int tuple=0)
Definition: GA_ATIDict.h:223
type
Definition: core.h:1059
const UT_OptionsHolder & getHolder(GA_Offset offset, int component=0) const
Definition: GA_ATIDict.h:136
void setDictIndex(GA_Offset offset, GA_DictIndexType i, int component=0)
Definition: GA_ATIDict.h:147
GA_PageArray< int32 > HandleArrayType
Definition: GA_ATIDict.h:160