HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GA_ATIStringArray.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_ATIStringArray.h ( GA Library, C++)
7  *
8  * COMMENTS: String Array ATI (Attribute Type Implementation)
9  */
10 
11 #pragma once
12 
13 #ifndef __GA_ATIStringArray__
14 #define __GA_ATIStringArray__
15 
16 #include "GA_API.h"
17 #include "GA_ATIBlobArray.h"
18 #include "GA_AIFSharedStringArray.h" // For GA_StringIndexType type
19 #include "GA_Types.h"
20 
21 #include <UT/UT_Array.h>
22 #include <UT/UT_Map.h>
23 #include <UT/UT_StringHolder.h>
24 #include <UT/UT_StringMap.h>
25 #include <UT/UT_VectorTypes.h>
26 
27 #include <iosfwd>
28 #include <stddef.h>
29 
30 
31 class GA_AIFJSON;
32 class GA_AIFStringArray;
33 class GA_Attribute;
34 class GA_AttributeType;
35 class GA_IndexMap;
36 
37 class UT_StringArray;
38 class UT_IStream;
39 class UT_Options;
40 
42 {
43 public:
44  static void registerType();
45 
47  static const UT_StringHolder &getTypeName()
48  { return theAttributeType->getTypeName(); }
50  static const GA_AttributeType &getType() { return *theAttributeType; }
51 
53  static bool isType(const GA_Attribute *attrib)
54  {
55  return attrib && &attrib->getType() == theAttributeType;
56  }
59  {
60  if (attrib && &attrib->getType() == theAttributeType)
61  return static_cast<GA_ATIStringArray *>(attrib);
62  return NULL;
63  }
65  static const GA_ATIStringArray *cast(const GA_Attribute *attrib)
66  {
67  if (attrib && &attrib->getType() == theAttributeType)
68  return static_cast<const GA_ATIStringArray *>(attrib);
69  return NULL;
70  }
71 
72  static GA_Attribute *create(const GA_IndexMap &index_map,
73  GA_AttributeScope scope,
74  const UT_StringHolder &name,
75  const GA_AttributeOptions *attribute_options=NULL);
76  static GA_Attribute *create(const GA_IndexMap &index_map,
77  const UT_StringHolder &name)
78  { return create(index_map, GA_SCOPE_PUBLIC, name); }
79 
81  const GA_IndexMap &index_map,
82  GA_AttributeScope scope,
83  const UT_StringHolder &name,
84  int tuple_size);
85  ~GA_ATIStringArray() override;
86 
87  // AIFCopyData is implemented in GA_ATIBlob.
89  { return myAIFSharedStringArray; }
90 
91  /// @section JSON-GA_ATIStringArray JSON Schema: GA_ATIStringArray
92  /// @code
93  /// {
94  /// "name" : "GA_ATIStringArray",
95  /// "description" : "An tuple array of indexed strings",
96  /// "type" : "orderedmap",
97  /// "properties": {
98  /// "storage": {
99  /// "type" : "string",
100  /// "description" : "Tuple storage",
101  /// "enum" : [ "int8", "int16", "int32", "int64" ],
102  /// },
103  /// "strings": {
104  /// "type" : { "$ref":"GA_BlobData", },
105  /// "description" : "Array of strings used in the attribute",
106  /// },
107  /// "indices": {
108  /// "type" : {"$ref":"GA_DataArrayTuple"},
109  /// "description" : "Int index for each element of the array",
110  /// },
111  /// },
112  /// }
113  /// @endcode
114  /// @see @ref JSON_FileFormat
115  const GA_AIFJSON *getAIFJSON() const override { return myAIFJSON; }
116 
117  /// @{
118  /// Get a string
119  void getString(UT_StringArray &strings, GA_Offset offset) const;
120  void getString(UT_Array<UT_StringHolder> &strings, GA_Offset offset) const;
121  void getStringIndex(UT_Array<GA_StringIndexType> &indices, GA_Offset offset) const;
122  /// @}
123  /// @{
124  /// Set a string
125  void setString(GA_Offset offset, const UT_StringArray &strings);
126  void setString(GA_Offset offset, const UT_Array<UT_StringHolder> &strings);
127  void setStringIndex(GA_Offset offset, const UT_Array<GA_StringIndexType> &indices);
128  /// @}
129 
130 protected:
131  // Create a new ATIBlob
132  GA_Attribute *doClone(const GA_IndexMap &index_map,
133  const UT_StringHolder &name) const override;
134 private:
135  const UT_StringHolder &lookupString(GA_StringIndexType handle) const;
136  const UT_StringHolder &lookupOrderedString(exint handle) const;
137  GA_StringIndexType lookupHandle(const char *s) const;
138  GA_StringIndexType validateHandle(GA_StringIndexType h) const;
139  bool replaceString(GA_StringIndexType handle,
140  const char *string);
141 
142  /// @{ GA_AIFStringTuple
143  /// @warning If you call addStringReference(), at some point, you must call
144  /// delHandleReference() on the string you added.
145  GA_StringIndexType addStringReference(const char *str);
146  void delHandleReference(GA_StringIndexType handle);
147  bool tupleGet(GA_Offset di, GA_StringIndexType &v, int vi) const;
148  bool tupleSet(GA_Offset di, GA_StringIndexType v, int vi);
149  /// @}
150 
151  static GA_AIFSharedStringArray *myAIFSharedStringArray;
152  static GA_AIFJSON *myAIFJSON;
153 
154  static const GA_AttributeType *theAttributeType;
155 
156  /// @cond INTERNAL_DOX
157  friend class ga_StringArray;
158  friend class ga_StringJSON;
159  friend class GA_ATIStringArrayDelayedWriter;
160  /// @endcond
161 };
162 
163 //
164 // Designed to delay all the reference count adjustments
165 // until this object is flushed. Also maintains a local
166 // table for looking up the string indices with, thus avoiding
167 // concurrency issues.
168 //
170 {
171 public:
175 
176  void bind(GA_ATIStringArray *attrib);
177 
178 
179  // The index cache is provided to avoid having to potentially resize
180  // this inside this function.
181  void setString(GA_Offset offset, const UT_StringArray &strings, UT_Array<GA_StringIndexType> &indexcache);
182  void setString(GA_Offset offset, const UT_Array<UT_StringHolder> &strings, UT_Array<GA_StringIndexType> &indexcache);
183  // Directly set the index.
184  void setStringIndex(GA_Offset offset, const UT_Array<GA_StringIndexType> &idx);
185 
186  void flush();
187 
189  {
190  return myHasChanges;
191  }
192 
193  // Returns the index to be used for the string, adding it to the string
194  // table if necessary.
195  GA_StringIndexType addStringReference(const UT_StringHolder &str);
196 
197 private:
198  void setString(GA_Offset offset, const UT_StringHolder *strings, exint n, UT_Array<GA_StringIndexType> &indexcache);
199 
200  GA_ATIStringArray *myAttribute;
201  volatile bool myHasChanges;
202 
203  UT_Map<GA_StringIndexType, exint> myRefCountChanges;
204  UT_StringMap<GA_StringIndexType> myStringToIndex;
205 };
206 
207 inline GA_StringIndexType
209 {
210  // See if already present...
211  // We can't just test if it is already present, we must
212  // ensure we have a copy being held onto by our table or
213  // we could have it eaten by another thread!
214  GA_StringIndexType index = myAttribute->addStringReference(str);
215 
216  // Add a reference. This means we will *not* count
217  // as a reference count!
218  myRefCountChanges[index] += -1;
219 
220  return index;
221 }
222 
223 // NOTE: It may seem strange to define GA_ATIBlobArray::isType() and cast()
224 // in GA_ATIStringArray.h, but GA_ATIBlobArray::isType() and cast() need
225 // GA_ATIStringArray::isType(), and GA_ATIStringArray inherits from GA_ATIBlobArray,
226 // so these are defined here, and GA_ATIStringArray.h is included
227 // in GA_ATIBlobArray.h, below the class.
228 bool
230 {
231  if (!attrib)
232  return false;
233 
234  if (&attrib->getType() == theAttributeType)
235  return true;
236  if (GA_ATIStringArray::isType(attrib))
237  return true;
238  if (dynamic_cast<const GA_ATIBlobArray *>(attrib))
239  return true;
240  return false;
241 
242 }
245 {
246  if (isType(attrib))
247  return static_cast<GA_ATIBlobArray *>(attrib);
248  return NULL;
249 }
250 const GA_ATIBlobArray *
252 {
253  if (isType(attrib))
254  return static_cast<const GA_ATIBlobArray *>(attrib);
255  return NULL;
256 }
257 
258 #endif
A class to manage an ordered array which has fixed offset handles.
Definition: GA_IndexMap.h:63
Definition of a geometry attribute.
Definition: GA_Attribute.h:198
static SYS_FORCE_INLINE const UT_StringHolder & getTypeName()
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
const GLdouble * v
Definition: glcorearb.h:837
static SYS_FORCE_INLINE const GA_ATIStringArray * cast(const GA_Attribute *attrib)
int64 exint
Definition: SYS_Types.h:125
GLdouble s
Definition: glad.h:3009
GA_StringIndexType addStringReference(const UT_StringHolder &str)
#define GA_API
Definition: GA_API.h:14
Standard user attribute level.
Definition: GA_Types.h:149
SYS_FORCE_INLINE const GA_AttributeType & getType() const
Definition: GA_Attribute.h:206
GA_Size GA_Offset
Definition: GA_Types.h:646
GA_AttributeScope
Definition: GA_Types.h:143
GLdouble n
Definition: glcorearb.h:2008
GLintptr offset
Definition: glcorearb.h:665
static SYS_FORCE_INLINE GA_ATIStringArray * cast(GA_Attribute *attrib)
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
const GA_AIFSharedStringArray * getAIFSharedStringArray() const override
Return the attribute's shared string array interface or NULL.
A specialization of GA_AIFStringArray to access "shared strings".
static SYS_FORCE_INLINE const GA_AttributeType & getType()
GLuint const GLchar * name
Definition: glcorearb.h:786
GLsizei const GLchar *const * strings
Definition: glcorearb.h:1933
static void registerType()
static SYS_FORCE_INLINE bool isType(const GA_Attribute *attrib)
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
A map of string to various well defined value types.
Definition: UT_Options.h:84
A simple ATI to store aribtrary "blobs" of data in an attribute.
static SYS_FORCE_INLINE GA_ATIBlobArray * cast(GA_Attribute *attrib)
GLuint index
Definition: glcorearb.h:786
GA_BlobIndex GA_StringIndexType
static GA_Attribute * create(const GA_IndexMap &index_map, GA_AttributeScope scope, const UT_StringHolder &name, const GA_AttributeOptions *attribute_options=NULL)
Attribute Interface for file I/O.
Definition: GA_AIFJSON.h:39
const GA_AIFJSON * getAIFJSON() const override
type
Definition: core.h:1059
static GA_Attribute * create(const GA_IndexMap &index_map, const UT_StringHolder &name)
GA_Attribute * doClone(const GA_IndexMap &index_map, const UT_StringHolder &name) const override
Create a new ATIBlob attribute. Sub-classes must implement this.
static SYS_FORCE_INLINE bool isType(const GA_Attribute *attrib)
SYS_FORCE_INLINE bool hasChanges() const