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 
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  GT_Storage getStorage() const override
91  { return GT_STORE_STRING; }
92  GT_Size getTupleSize() const override { return 1; }
93  GT_Size entries() const override { return _size; }
94  GT_Type getTypeInfo() const override
95  { return GT_TYPE_NONE; }
96  int64 getMemoryUsage() const override
97  { return sizeof(*this) + sizeof(T)*_size; }
98 
99 protected:
100  /** Update our @a _data member to point at the array data.
101  This must be called after any operation that changes
102  the contents of @a _array.*/
103  void _UpdateDataPointer(bool makeUnique);
104 
105  /** Return a GT_String from one of our elems.
106  This must be specialized per element type.*/
107  GT_String _GetString(const T& o) const;
108 
110  { return o.empty() ? NULL : o.c_str(); }
111 
112 private:
113  /** No numeric accessors supported.*/
114  uint8 getU8(GT_Offset, int idx) const override { return 0; }
115  int32 getI32(GT_Offset, int idx) const override { return 0; }
116  fpreal32 getF32(GT_Offset, int idx) const override { return 0; }
117 
118 private:
119  ArrayType _array;
120  GT_Size _size;
121  const T* _data; /*! Raw pointer to the underlying data.
122  This is held in order to avoid referencing
123  checks on every array lookup.*/
124 };
125 
126 
127 template <class T>
129  : _array(array), _size(array.size())
130 {
131  _UpdateDataPointer(false);
132 }
133 
134 
135 template <class T>
137  : _size(0), _data(NULL)
138 {}
139 
140 
141 template <class T>
142 void
144 {
145  /* Access a non-const pointer to make the array unique.*/
146  _data = makeUnique ? _array.data() : _array.cdata();
147  UT_ASSERT(_size == 0 || _data != NULL);
148 }
149 
150 
151 template <class T>
152 void
154 {
155  _array.swap(o);
156  _size = _array.size();
157  _UpdateDataPointer(false);
158 }
159 
160 
161 template <class T>
164 {
165  This* copy = new This(_array);
166  copy->_UpdateDataPointer(true);
167  return GT_DataArrayHandle(copy);
168 }
169 
170 
171 template <>
172 GT_String
174 { return _GetStringFromStdString(o); }
175 
176 
177 template <>
178 GT_String
180 { return _GetStringFromStdString(o.GetString()); }
181 
182 
183 template <>
184 GT_String
186 { return _GetStringFromStdString(o.GetString()); }
187 
188 
189 template <>
190 GT_String
192 { return _GetStringFromStdString(o.GetAssetPath()); }
193 
194 
195 
196 template <>
197 int64
199 {
200  int64 sz = sizeof(*this) + sizeof(ValueType)*_size;
201  for(GT_Size i = 0; i < _size; ++i)
202  sz += _data[i].size()*sizeof(char);
203  return sz;
204 }
205 
206 
207 template <>
208 int64
210 {
211  int64 sz = sizeof(*this) + sizeof(ValueType)*_size;
212  for(GT_Size i = 0; i < _size; ++i) {
213  const SdfAssetPath& p = _data[i];
214  sz += (p.GetAssetPath().size() +
215  p.GetResolvedPath().size())*sizeof(char);
216  }
217  return sz;
218 }
219 
220 /** TODO: Specialization of getMemoryUsage for SdPath? */
221 
222 
227 
229 
230 #endif /*_GUSD_GT_VTSTRINGARRAY_H_*/
GT_Storage
Definition: GT_Types.h:18
int int32
Definition: SYS_Types.h:39
const std::string & GetResolvedPath() const
Definition: assetPath.h:120
GusdGT_VtStringArray< SdfAssetPath > GusdGT_VtAssetPathArray
GT_Size getTupleSize() const override
Number of elements for each array element.
GT_Type
Definition: GT_Types.h:34
std::string const & GetString() const
Return the string that this token represents.
Definition: token.h:207
GusdGT_VtStringArray< std::string > GusdGT_VtStdStringArray
void _UpdateDataPointer(bool makeUnique)
GT_Storage getStorage() const override
Type of data stored in the array.
float fpreal32
Definition: SYS_Types.h:200
const ArrayType & operator*() const
GLsizeiptr size
Definition: glcorearb.h:663
unsigned char uint8
Definition: SYS_Types.h:36
GLenum array
Definition: glew.h:9108
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:170
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
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:124
long long int64
Definition: SYS_Types.h:116
GLfloat GLfloat p
Definition: glew.h:16656
SYS_FORCE_INLINE const char * c_str() const
GT_DataArrayHandle harden() const override
Create a "hardened" version of the array.
Definition: types.h:166
Definition: path.h:288
SDF_API const std::string & GetString() const
void getIndexedStrings(UT_StringArray &, UT_IntArray &) const override
int64 GT_Size
Definition: GT_Types.h:123
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:1375
GusdGT_VtStringArray< TfToken > GusdGT_VtTokenArray
void swap(ArrayType &o)
GT_String _GetString(const T &o) const
const std::string & GetAssetPath() const
Return the asset path.
Definition: assetPath.h:111
OIIO_API bool copy(string_view from, string_view to, std::string &err)
#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:171
~GusdGT_VtStringArray() override
GT_String _GetStringFromStdString(const std::string &o) const
GusdGT_VtStringArray< T > This