All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GT_VtStringArray.h
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 
36 PXR_NAMESPACE_OPEN_SCOPE
37 
45 template <class T>
46 class GusdGT_VtStringArray : public GT_DataArray
47 {
48 public:
50  typedef T ValueType;
51  typedef VtArray<T> ArrayType;
52 
53 
55  GusdGT_VtStringArray(const ArrayType& array);
56 
57  virtual ~GusdGT_VtStringArray() {}
58 
59  virtual const char* className() const { return "GusdGT_VtStringArray"; }
60 
61  const T& operator()(GT_Offset o) const
62  {
63  UT_ASSERT_P(o >= 0 && o <= _size);
64  return _data[o];
65  }
66 
67  const ArrayType& operator*() const { return _array; }
68 
69  /* Non-virtual string accessor.*/
70  GT_String getString(GT_Offset o) const
71  { return _GetString((*this)(o)); }
72 
74  void swap(ArrayType& o);
75 
76  virtual GT_DataArrayHandle harden() const;
77 
78  virtual GT_String getS(GT_Offset o, int idx=0) const
79  { return getString(o); }
80 
82  virtual GT_Size getStringIndexCount() const { return -1; }
83  virtual GT_Offset getStringIndex(GT_Offset, int) const { return -1; }
84  virtual void getIndexedStrings(UT_StringArray&,
85  UT_IntArray&) const {}
86 
87  virtual GT_Storage getStorage() const { return GT_STORE_STRING; }
88  virtual GT_Size getTupleSize() const { return 1; }
89  virtual GT_Size entries() const { return _size; }
90  virtual GT_Type getTypeInfo() const { return GT_TYPE_NONE; }
91  virtual int64 getMemoryUsage() const
92  { return sizeof(*this) + sizeof(T)*_size; }
93 
94 protected:
98  void _UpdateDataPointer(bool makeUnique);
99 
102  GT_String _GetString(const T& o) const;
103 
104  GT_String _GetStringFromStdString(const std::string& o) const
105  { return o.empty() ? NULL : o.c_str(); }
106 
107 private:
109  virtual uint8 getU8(GT_Offset, int idx) const { return 0; }
110  virtual int32 getI32(GT_Offset, int idx) const { return 0; }
111  virtual fpreal32 getF32(GT_Offset, int idx) const { return 0; }
112 
113 private:
114  ArrayType _array;
115  GT_Size _size;
116  const T* _data;
119 };
120 
121 
122 template <class T>
123 GusdGT_VtStringArray<T>::GusdGT_VtStringArray(const ArrayType& array)
124  : _array(array), _size(array.size())
125 {
126  _UpdateDataPointer(false);
127 }
128 
129 
130 template <class T>
132  : _size(0), _data(NULL)
133 {}
134 
135 
136 template <class T>
137 void
139 {
140  /* Access a non-const pointer to make the array unique.*/
141  _data = makeUnique ? _array.data() : _array.cdata();
142  UT_ASSERT(_size == 0 || _data != NULL);
143 }
144 
145 
146 template <class T>
147 void
149 {
150  _array.swap(o);
151  _size = _array.size();
152  _UpdateDataPointer(false);
153 }
154 
155 
156 template <class T>
157 GT_DataArrayHandle
159 {
160  This* copy = new This(_array);
161  copy->_UpdateDataPointer(true);
162  return GT_DataArrayHandle(copy);
163 }
164 
165 
166 template <>
167 GT_String
168 GusdGT_VtStringArray<std::string>::_GetString(const std::string& o) const
169 { return _GetStringFromStdString(o); }
170 
171 
172 template <>
173 GT_String
175 { return _GetStringFromStdString(o.GetString()); }
176 
177 
178 template <>
179 GT_String
181 { return _GetStringFromStdString(o.GetString()); }
182 
183 
184 template <>
185 GT_String
187 { return _GetStringFromStdString(o.GetAssetPath()); }
188 
189 
190 
191 template <>
192 int64
194 {
195  int64 sz = sizeof(*this) + sizeof(ValueType)*_size;
196  for(GT_Size i = 0; i < _size; ++i)
197  sz += _data[i].size()*sizeof(char);
198  return sz;
199 }
200 
201 
202 template <>
203 int64
205 {
206  int64 sz = sizeof(*this) + sizeof(ValueType)*_size;
207  for(GT_Size i = 0; i < _size; ++i) {
208  const SdfAssetPath& p = _data[i];
209  sz += (p.GetAssetPath().size() +
210  p.GetResolvedPath().size())*sizeof(char);
211  }
212  return sz;
213 }
214 
222 
223 PXR_NAMESPACE_CLOSE_SCOPE
224 
225 #endif /*_GUSD_GT_VTSTRINGARRAY_H_*/
std::string const & GetString() const
Return the string that this token represents.
Definition: token.h:209
const std::string & GetAssetPath() const
Return the asset path.
Definition: assetPath.h:98
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
const std::string & GetResolvedPath() const
Return the resolved asset path, if any.
Definition: assetPath.h:107
void swap(ArrayType &o)
Swap our array contents with another array.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
SDF_API const std::string & GetString() const
Returns the string representation of this path as a std::string.
Contains an asset path and an optional resolved path.
Definition: assetPath.h:43
void _UpdateDataPointer(bool makeUnique)
Update our _data member to point at the array data.
GT_String _GetString(const T &o) const
Return a GT_String from one of our elems.
virtual GT_Size getStringIndexCount() const
Indexed strings are not currently supported in Vt.
GT_DataArray implementation wrapping VtArray for string-like types.