HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
defaultArray.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_DEFAULTARRAY_H_
25 #define _GUSD_DEFAULTARRAY_H_
26 
27 #include "pxr/pxr.h"
28 
29 #include <UT/UT_Array.h>
30 
31 
33 
34 
35 /// Simple array wrapper, providing an array that may either hold a
36 /// single constant value, or an array of values.
37 template <typename T>
39 {
40 public:
41  using value_type = 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 
68  /// Turn this into a constant array, with value @a val.
69  void SetConstant(const T& val)
70  {
71  Clear();
72  SetDefault(val);
73  }
74 
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 
92 
93 
94 #endif /*_GUSD_DEFAULTARRAY_H_*/
const T & operator()(exint i) const
Definition: defaultArray.h:78
const array_type & GetArray() const
Definition: defaultArray.h:83
bool IsVarying() const
Definition: defaultArray.h:61
void SetDefault(const T &val)
Definition: defaultArray.h:66
const T & GetDefault() const
Definition: defaultArray.h:64
int64 exint
Definition: SYS_Types.h:125
UT_Array< T > array_type
Definition: defaultArray.h:42
exint size() const
Definition: UT_Array.h:646
bool IsConstant() const
Definition: defaultArray.h:59
exint size() const
Definition: defaultArray.h:53
void SetConstant(const T &val)
Turn this into a constant array, with value val.
Definition: defaultArray.h:69
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
array_type & GetArray()
Definition: defaultArray.h:81
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
T & operator()(exint i)
Definition: defaultArray.h:75
void clear()
Resets list to an empty list.
Definition: UT_Array.h:729
GusdDefaultArray(const T &defaultVal)
Definition: defaultArray.h:50