All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_VtArray.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_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 
33 PXR_NAMESPACE_OPEN_SCOPE
34 
46 template <class T>
48 {
49 public:
50  typedef T ValueType;
51  typedef VtArray<T> ArrayType;
52 
53  typedef typename ArrayType::const_iterator const_iterator;
54  typedef typename ArrayType::const_reverse_iterator const_reverse_iterator;
55 
56  GusdUT_VtArrayRO(const ArrayType& array)
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;
82  exint _size;
83 };
84 
85 
92 template <class T>
94 {
95 public:
96  typedef T ValueType;
97  typedef VtArray<T> ArrayType;
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 
114  T& operator()(exint i)
115  {
116  UT_ASSERT_P(i >= 0 && i < _size);
117  UT_ASSERT_P(_size == _array.GetSize());
118  return _data[i];
119  }
120 
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; }
133  const T* data() const { return _data; }
134  T* data() { return _data; }
135 
136  exint size() const { return _size; }
137 
138  void resize(exint size)
139  {
140  UT_ASSERT(size >= 0);
141  _array.resize(size);
142  update();
143  }
144 
145  void reserve(exint size)
146  {
147  UT_ASSERT(size >= 0);
148  _array.reserve(size);
149  update();
150  }
151 
152  void swap(ArrayType& array)
153  {
154  _array.swap(array);
155  update();
156  }
157 
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;
181  exint _size;
182 };
183 
184 PXR_NAMESPACE_CLOSE_SCOPE
185 
186 #endif /*_GUSD_UT_VTARRAY_H_*/
void reserve(size_t num)
Ensure enough memory is allocated to hold num elements.
Definition: array.h:449
PointerIterator< const ElementType > const_iterator
Const iterator type.
Definition: array.h:223
Read-write wrapper on a VtArray.
Definition: UT_VtArray.h:93
reverse_iterator rend()
Return a reverse iterator to the start of the array.
Definition: array.h:365
iterator begin()
Return a non-const iterator to the start of the array.
Definition: array.h:345
void resize(size_t newSize)
Resize this array.
Definition: array.h:481
void update()
Update the state of the array to reflect changes in the underlying array (eg., change in size)...
Definition: UT_VtArray.h:160
boost::reverse_iterator< iterator > reverse_iterator
Reverse iterator type.
Definition: array.h:225
iterator end()
Returns a non-const iterator to the end of the array.
Definition: array.h:348
const_pointer cdata() const
Return a const pointer to the data held by this array.
Definition: array.h:387
pointer data()
Return a non-const pointer to this array&#39;s data.
Definition: array.h:383
PointerIterator< ElementType > iterator
Iterator type.
Definition: array.h:221
reverse_iterator rbegin()
Return a non-const reverse iterator to the end of the array.
Definition: array.h:362
void swap(VtArray &other)
Swap the contents of this array with other.
Definition: array.h:585
boost::reverse_iterator< const_iterator > const_reverse_iterator
Reverse const iterator type.
Definition: array.h:227
Read-only wrapper to assist in read operations on a VtArray.
Definition: UT_VtArray.h:47
const ArrayType & operator*() const
Access to the underlying array.
Definition: UT_VtArray.h:126