HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GT_VtStringArray.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef _GUSD_GT_VTSTRINGARRAY_H_
25 #define _GUSD_GT_VTSTRINGARRAY_H_
26 
27 
28 #include <GT/GT_DataArray.h>
29 
30 #include "pxr/pxr.h"
31 #include "pxr/usd/sdf/assetPath.h"
32 #include "pxr/usd/sdf/path.h"
33 #include "pxr/base/vt/array.h"
34 #include "pxr/base/tf/token.h"
35 
37 
38 /** GT_DataArray implementation wrapping VtArray for
39  string-like types. I.e., std::string, TfToken, et. al.
40 
41  @warning: The GT_String (const char*) pointer returned for all empty
42  strings is always the NULL pointer. This includes std::string, which
43  can't be constructed from a NULL string. Be careful when reconstring
44  source objects! */
45 template <class T>
47 {
48 public:
50  typedef T ValueType;
52 
53 
55  GusdGT_VtStringArray(const ArrayType& array);
56 
57  ~GusdGT_VtStringArray() override {}
58 
59  const char* className() const override
60  { return "GusdGT_VtStringArray"; }
61 
62  const T& operator()(GT_Offset o) const
63  {
64  UT_ASSERT_P(o >= 0 && o <= _size);
65  return _data[o];
66  }
67 
68  const ArrayType& operator*() const { return _array; }
69 
70  /* Non-virtual string accessor.*/
72  { return _GetString((*this)(o)); }
73 
74  /** Swap our array contents with another array.*/
75  void swap(ArrayType& o);
76 
77  GT_DataArrayHandle harden() const override;
78 
79  GT_String getS(GT_Offset o, int idx=0) const override
80  { return getString(o); }
81 
82  /** Indexed strings are not currently supported in Vt.*/
83  GT_Size getStringIndexCount() const override
84  { return -1; }
85  GT_Offset getStringIndex(GT_Offset, int) const override
86  { return -1; }
88  UT_IntArray&) const override {}
89 
90  /** Indexed dictionaries are not currently supported in Vt.*/
91  GT_Size getDictIndexCount() const override
92  { return -1; }
93  GT_Offset getDictIndex(GT_Offset, int) const override
94  { return -1; }
96  UT_IntArray&) const override {}
97 
98  GT_Storage getStorage() const override
99  { return GT_STORE_STRING; }
100  GT_Size getTupleSize() const override { return 1; }
101  GT_Size entries() const override { return _size; }
102  GT_Type getTypeInfo() const override
103  { return GT_TYPE_NONE; }
104  int64 getMemoryUsage() const override
105  { return sizeof(*this) + sizeof(T)*_size; }
106 
107 protected:
108  /** Update our @a _data member to point at the array data.
109  This must be called after any operation that changes
110  the contents of @a _array.*/
111  void _UpdateDataPointer(bool makeUnique);
112 
113  /** Return a GT_String from one of our elems.
114  This must be specialized per element type.*/
115  GT_String _GetString(const T& o) const;
116 
118  { return o.empty() ? NULL : o.c_str(); }
119 
120 private:
121  /** No numeric accessors supported.*/
122  uint8 getU8(GT_Offset, int idx) const override { return 0; }
123  int32 getI32(GT_Offset, int idx) const override { return 0; }
124  fpreal32 getF32(GT_Offset, int idx) const override { return 0; }
125 
126 private:
127  ArrayType _array;
128  GT_Size _size;
129  const T* _data; /*! Raw pointer to the underlying data.
130  This is held in order to avoid referencing
131  checks on every array lookup.*/
132 };
133 
134 
135 template <class T>
137  : _array(array), _size(array.size())
138 {
139  _UpdateDataPointer(false);
140 }
141 
142 
143 template <class T>
145  : _size(0), _data(NULL)
146 {}
147 
148 
149 template <class T>
150 void
152 {
153  /* Access a non-const pointer to make the array unique.*/
154  _data = makeUnique ? _array.data() : _array.cdata();
155  UT_ASSERT(_size == 0 || _data != NULL);
156 }
157 
158 
159 template <class T>
160 void
162 {
163  _array.swap(o);
164  _size = _array.size();
165  _UpdateDataPointer(false);
166 }
167 
168 
169 template <class T>
172 {
173  This* copy = new This(_array);
174  copy->_UpdateDataPointer(true);
175  return GT_DataArrayHandle(copy);
176 }
177 
178 
179 template <>
180 GT_String
182 { return _GetStringFromStdString(o); }
183 
184 
185 template <>
186 GT_String
188 { return _GetStringFromStdString(o.GetString()); }
189 
190 
191 template <>
192 GT_String
194 { return _GetStringFromStdString(o.GetString()); }
195 
196 
197 template <>
198 GT_String
200 { return _GetStringFromStdString(o.GetAssetPath()); }
201 
202 
203 
204 template <>
205 int64
207 {
208  int64 sz = sizeof(*this) + sizeof(ValueType)*_size;
209  for(GT_Size i = 0; i < _size; ++i)
210  sz += _data[i].size()*sizeof(char);
211  return sz;
212 }
213 
214 
215 template <>
216 int64
218 {
219  int64 sz = sizeof(*this) + sizeof(ValueType)*_size;
220  for(GT_Size i = 0; i < _size; ++i) {
221  const SdfAssetPath& p = _data[i];
222  sz += (p.GetAssetPath().size() +
223  p.GetResolvedPath().size())*sizeof(char);
224  }
225  return sz;
226 }
227 
228 /** TODO: Specialization of getMemoryUsage for SdPath? */
229 
230 
235 
237 
238 #endif /*_GUSD_GT_VTSTRINGARRAY_H_*/
GT_Storage
Definition: GT_Types.h:19
int int32
Definition: SYS_Types.h:39
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
GusdGT_VtStringArray< SdfAssetPath > GusdGT_VtAssetPathArray
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GT_Size getTupleSize() const override
Number of elements for each array element.
GT_Type
Definition: GT_Types.h:36
std::string const & GetString() const
Return the string that this token represents.
Definition: token.h:207
GusdGT_VtStringArray< std::string > GusdGT_VtStdStringArray
const std::string & GetResolvedPath() const &
Definition: assetPath.h:144
void _UpdateDataPointer(bool makeUnique)
GT_Size getDictIndexCount() const override
GT_Storage getStorage() const override
Type of data stored in the array.
float fpreal32
Definition: SYS_Types.h:200
const ArrayType & operator*() const
unsigned char uint8
Definition: SYS_Types.h:36
GT_Offset getDictIndex(GT_Offset, int) const override
Definition: token.h:87
int64 getMemoryUsage() const override
Abstract data class for an array of float, int or string data.
Definition: GT_DataArray.h:40
const T & operator()(GT_Offset o) const
UT_IntrusivePtr< GT_DataArray > GT_DataArrayHandle
Definition: GT_DataArray.h:32
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:155
const std::string & GetAssetPath() const &
Return the asset path.
Definition: assetPath.h:130
GT_Type getTypeInfo() const override
Return "type" information for the data. This defaults to GT_TYPE_NONE.
GT_String getString(GT_Offset o) const
GT_Offset getStringIndex(GT_Offset, int) const override
int64 GT_Offset
Definition: GT_Types.h:129
long long int64
Definition: SYS_Types.h:116
GT_DataArrayHandle harden() const override
Create a "hardened" version of the array.
Definition: types.h:173
Definition: path.h:291
SDF_API const std::string & GetString() const
void getIndexedStrings(UT_StringArray &, UT_IntArray &) const override
bool empty() const
method name that maches std::string
int64 GT_Size
Definition: GT_Types.h:128
GLsizeiptr size
Definition: glcorearb.h:664
GT_Size getStringIndexCount() const override
GT_Size entries() const override
Number of entries in the array.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
GusdGT_VtStringArray< TfToken > GusdGT_VtTokenArray
void swap(ArrayType &o)
GT_String _GetString(const T &o) const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GT_String getS(GT_Offset o, int idx=0) const override
GusdGT_VtStringArray< SdfPath > GusdGT_VtPathArray
const char * className() const override
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
~GusdGT_VtStringArray() override
GT_String _GetStringFromStdString(const std::string &o) const
void getIndexedDicts(UT_Array< UT_OptionsHolder > &, UT_IntArray &) const override
GusdGT_VtStringArray< T > This