HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_VtArray.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_UT_VTARRAY_H_
25 #define _GUSD_UT_VTARRAY_H_
26 
27 #include <SYS/SYS_Types.h>
28 #include <UT/UT_Assert.h>
29 
30 #include "pxr/pxr.h"
31 #include "pxr/base/vt/array.h"
32 
34 
35 /** Read-only wrapper to assist in read operations on a VtArray.
36 
37  This container is not meant to be an owner of the VtArray --
38  only a temporary structure used when iterating over the array.
39  Hence, it retains a reference to the array, rather than holding
40  a shared pointer. The array must remain in memory while the
41  container is in use.
42 
43  Since this container is read-only on the referenced array,
44  it is guaranteed that none of its method will trigger copying
45  of that data.*/
46 template <class T>
48 {
49 public:
50  typedef T ValueType;
52 
53  typedef typename ArrayType::const_iterator const_iterator;
54  typedef typename ArrayType::const_reverse_iterator const_reverse_iterator;
55 
57  : _array(array), _size(array.GetSize()), _data(array.cdata())
58  { UT_ASSERT(_size == 0 || _data != NULL); }
59 
60  const T& operator()(exint i) const
61  {
62  UT_ASSERT_P(i >= 0 && i < _size);
63  return _data[i];
64  }
65 
66  const ArrayType& operator*() const { return _array; }
67 
68  const ArrayType& array() const { return _array; }
69 
70  const T* data() const { return _data; }
71 
72  exint size() const { return _size; }
73 
74  const_iterator begin() const { return _array.begin(); }
75  const_iterator end() const { return _array.end(); }
76  const_reverse_iterator rbegin() const { return _array.rbegin(); }
77  const_reverse_iterator rend() const { return _array.rend(); }
78 
79 private:
80  const ArrayType& _array;
81  const T* _data; /*! Cached as an optimization.*/
82  exint _size;
83 };
84 
85 
86 /** Read-write wrapper on a VtArray.
87 
88  As with GusdUT_VtArrayRW, this does not own the VtArray, but rather
89  is a general tool for editing one, and so retains a reference to
90  the underyling array. The array must remain in memory while the
91  container is in use. */
92 template <class T>
94 {
95 public:
96  typedef T ValueType;
98 
99  typedef typename ArrayType::iterator iterator;
100  typedef typename ArrayType::reverse_iterator reverse_iterator;
101  typedef typename ArrayType::const_iterator const_iterator;
102  typedef typename ArrayType::const_reverse_iterator const_reverse_iterator;
103 
104  GusdUT_VtArrayRW(const ArrayType& array) : _array(array)
105  { update(); }
106 
107  const T& operator()(exint i) const
108  {
109  UT_ASSERT_P(i >= 0 && i < _size);
110  UT_ASSERT_P(_size == _array.GetSize());
111  return _data[i];
112  }
113 
115  {
116  UT_ASSERT_P(i >= 0 && i < _size);
117  UT_ASSERT_P(_size == _array.GetSize());
118  return _data[i];
119  }
120 
121  /** Access to the underlying array.
122  @warning If you modify the arrays externally, you should
123  call update() before attempting to access the array again
124  through this container.
125  @{ */
126  const ArrayType& operator*() const { return _array; }
127  ArrayType& operator*() { return _array; }
128 
129  const ArrayType& array() const { return _array; }
130  ArrayType& array() { return _array; }
131  /** @} */
132 
133  const T* data() const { return _data; }
134  T* data() { return _data; }
135 
136  exint size() const { return _size; }
137 
139  {
140  UT_ASSERT(size >= 0);
141  _array.resize(size);
142  update();
143  }
144 
146  {
147  UT_ASSERT(size >= 0);
148  _array.reserve(size);
149  update();
150  }
151 
153  {
154  _array.swap(array);
155  update();
156  }
157 
158  /** Update the state of the array to reflect changes
159  in the underlying array (eg., change in size).*/
160  void update()
161  {
162  _data = _array.data();
163  _size = _array.GetSize();
164  UT_ASSSERT(_size == 0 || _data != NULL);
165  }
166 
167  const_iterator begin() const { return _array.begin(); }
168  const_iterator end() const { return _array.end(); }
169  iterator begin() { return _array.begin(); }
170  iterator end() { return _array.end(); }
171 
172  const_reverse_iterator rbegin() const { return _array.rbegin(); }
173  const_reverse_iterator rend() const { return _array.rend(); }
174  reverse_iterator rbegin() { return _array.rbegin(); }
175  reverse_iterator rend() { return _array.rend(); }
176 
177 private:
178  ArrayType& _array;
179  T* _data; /*! Cache as an optimization
180  (avoid referencing checks on every value lookup)*/
181  exint _size;
182 };
183 
185 
186 #endif /*_GUSD_UT_VTARRAY_H_*/
ArrayType & array()
Definition: UT_VtArray.h:130
const T * data() const
Definition: UT_VtArray.h:133
GLsizeiptr size
Definition: glew.h:1681
iterator end()
Definition: UT_VtArray.h:170
const_reverse_iterator rbegin() const
Definition: UT_VtArray.h:172
VtArray< T > ArrayType
Definition: UT_VtArray.h:97
const ArrayType & operator*() const
Definition: UT_VtArray.h:126
exint size() const
Definition: UT_VtArray.h:72
ArrayType & operator*()
Definition: UT_VtArray.h:127
void resize(exint size)
Definition: UT_VtArray.h:138
void swap(ArrayType &array)
Definition: UT_VtArray.h:152
const T * data() const
Definition: UT_VtArray.h:70
void reserve(exint size)
Definition: UT_VtArray.h:145
const_reverse_iterator rend() const
Definition: UT_VtArray.h:173
GusdUT_VtArrayRW(const ArrayType &array)
Definition: UT_VtArray.h:104
ArrayType::const_iterator const_iterator
Definition: UT_VtArray.h:53
const ArrayType & array() const
Definition: UT_VtArray.h:68
ArrayType::reverse_iterator reverse_iterator
Definition: UT_VtArray.h:100
const_iterator begin() const
Definition: UT_VtArray.h:74
const ArrayType & array() const
Definition: UT_VtArray.h:129
reverse_iterator rend()
Definition: UT_VtArray.h:175
int64 exint
Definition: SYS_Types.h:120
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:134
const_iterator begin() const
Definition: UT_VtArray.h:167
ArrayType::const_reverse_iterator const_reverse_iterator
Definition: UT_VtArray.h:54
VtArray< T > ArrayType
Definition: UT_VtArray.h:51
Definition: types.h:166
ArrayType::iterator iterator
Definition: UT_VtArray.h:99
reverse_iterator rbegin()
Definition: UT_VtArray.h:174
const T & operator()(exint i) const
Definition: UT_VtArray.h:107
const_iterator end() const
Definition: UT_VtArray.h:168
T & operator()(exint i)
Definition: UT_VtArray.h:114
const_iterator end() const
Definition: UT_VtArray.h:75
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1245
const_reverse_iterator rend() const
Definition: UT_VtArray.h:77
const_reverse_iterator rbegin() const
Definition: UT_VtArray.h:76
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:83
ArrayType::const_iterator const_iterator
Definition: UT_VtArray.h:101
ArrayType::const_reverse_iterator const_reverse_iterator
Definition: UT_VtArray.h:102
const T & operator()(exint i) const
Definition: UT_VtArray.h:60
GLenum array
Definition: glew.h:9066
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:135
exint size() const
Definition: UT_VtArray.h:136
iterator begin()
Definition: UT_VtArray.h:169
const ArrayType & operator*() const
Definition: UT_VtArray.h:66
GusdUT_VtArrayRO(const ArrayType &array)
Definition: UT_VtArray.h:56