All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
defaultArray.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_DEFAULTARRAY_H_
25 #define _GUSD_DEFAULTARRAY_H_
26 
27 #include "pxr/pxr.h"
28 
29 #include <UT/UT_Array.h>
30 
31 
32 PXR_NAMESPACE_OPEN_SCOPE
33 
34 
37 template <typename T>
39 {
40 public:
41  using value_type = T;
42  using array_type = UT_Array<T>;
43 
45  {
46  if(SYSisPOD<T>())
47  memset((void*)&_default, 0, sizeof(T));
48  }
49 
50  GusdDefaultArray(const T& defaultVal)
51  : _default(defaultVal) {}
52 
53  exint size() const
54  { return _array.size(); }
55 
56  void Clear()
57  { return _array.clear(); }
58 
59  bool IsConstant() const { return size() == 0; }
60 
61  bool IsVarying() const { return size() > 0; }
62 
63  T& GetDefault() { return _default; }
64  const T& GetDefault() const { return _default; }
65 
66  void SetDefault(const T& val) {_default = val; }
67 
69  void SetConstant(const T& val)
70  {
71  Clear();
72  SetDefault(val);
73  }
74 
75  T& operator()(exint i)
76  { return IsVarying() ? _array(i) : GetDefault(); }
77 
78  const T& operator()(exint i) const
79  { return IsVarying() ? _array(i) : GetDefault(); }
80 
81  array_type& GetArray() { return _array; }
82 
83  const array_type& GetArray() const { return _array; }
84 
85 private:
86  array_type _array;
87  T _default;
88 };
89 
90 
91 PXR_NAMESPACE_CLOSE_SCOPE
92 
93 
94 #endif /*_GUSD_DEFAULTARRAY_H_*/
void SetConstant(const T &val)
Turn this into a constant array, with value val.
Definition: defaultArray.h:69
Simple array wrapper, providing an array that may either hold a single constant value, or an array of values.
Definition: defaultArray.h:38