HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fixedSizeHolder.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_EXEC_VDF_FIXED_SIZE_HOLDER_H
8 #define PXR_EXEC_VDF_FIXED_SIZE_HOLDER_H
9 
10 #include "pxr/pxr.h"
11 
13 #include "pxr/base/tf/mallocTag.h"
14 
15 #include <memory>
16 #include <type_traits>
17 #include <utility>
18 
20 
21 // Storage class used when T is too big to fit in Vdf_FixedSizeHolder's Size.
22 // It is mutable and therefore it makes deep copies.
23 template <typename T>
25 {
26 public:
28  TfAutoMallocTag tag("Vdf", "Vdf_FixedSizeHolder::ctor",
30  _pointer.reset(new T);
31  }
32 
33  template <typename U>
35  TfAutoMallocTag tag("Vdf", "Vdf_FixedSizeHolder::ctor",
37  _pointer.reset(new T(std::forward<U>(value)));
38  }
39 
41  Vdf_FixedSizeHolderRemoteStorage const &other) {
42  TfAutoMallocTag tag("Vdf", "Vdf_FixedSizeHolder::copy ctor",
44  _pointer.reset(new T(other.Get()));
45  }
47  Vdf_FixedSizeHolderRemoteStorage const &other) {
48  if (this != &other) {
49  TfAutoMallocTag tag("Vdf", "Vdf_FixedSizeHolder::assignment",
51  _pointer.reset(new T(other.Get()));
52  }
53  return *this;
54  }
55 
60 
61  inline T const &Get() const {
62  return *_pointer;
63  }
64  inline T &GetMutable() {
65  return *_pointer;
66  }
67 private:
68  std::unique_ptr<T> _pointer;
69 };
70 
71 // Local storage class used when T is small enough to fit in
72 // Vdf_FixedSizeHolder's Size.
73 template <typename T>
75 {
76 public:
78 
79  template <typename U>
81  : _value(std::forward<U>(value))
82  {}
83  inline T const &Get() const {
84  return _value;
85  }
86  inline T &GetMutable() {
87  return _value;
88  }
89 private:
90  T _value;
91 };
92 
93 ///
94 /// \class Vdf_FixedSizeHolder
95 ///
96 /// Vdf_FixedSizeHolder holds an object of type \a T of any size, but the
97 /// sizeof(Vdf_FixedSizeHolder<T>) is always exactly \a Size. If \a T's size is
98 /// less than or equal to \a Size, it is stored directly in member data. If
99 /// instead it is greater than \a Size, it is stored on the heap.
100 ///
101 /// The remote storage policy allows mutation of the object held in the
102 /// Vdf_FixedSizeHolder, but does this by deep-copying the held object whenever
103 /// the holder is copied.
104 ///
105 template <class T, size_t Size>
107 {
108  // Ensure that Size is large enough to hold remote storage even if this
109  // particular T fits into local storage.
110  static_assert(sizeof(Vdf_FixedSizeHolderRemoteStorage<int>) <= Size,
111  "Size too small to allow remote storage");
112 
113  // Choose storage type based on the size of T. Local if small enough,
114  // heap if too big.
115  typedef typename std::conditional<
116  sizeof(T) <= Size,
119  >::type _StorageType;
120 
121 public:
122 
123  Vdf_FixedSizeHolder() : _storage() {}
124 
125  //! Construct a fixed size holder holding \a obj.
126  explicit Vdf_FixedSizeHolder(T const &obj) : _storage(obj)
127  {
128  }
129 
130  //! Construct a fixed size holder holding \a obj.
131  explicit Vdf_FixedSizeHolder(T &&obj) : _storage(std::move(obj))
132  {
133  }
134 
136  : _storage(other._storage)
137  {
138  }
139 
141  : _storage(std::move(other._storage))
142  {
143  }
144 
146  _storage.~_StorageType();
147  }
148 
150  _storage = other._storage;
151  return *this;
152  }
153 
155  _storage = std::move(other._storage);
156  return *this;
157  }
158 
159  inline T const &Get() const {
160  return _storage.Get();
161  }
162 
163  inline T &GetMutable() {
164  return _storage.GetMutable();
165  }
166 
167  inline void Set(T const &value) {
168  _storage.GetMutable() = value;
169  }
170 
171  inline void Set(T &&value) {
172  _storage.GetMutable() = std::move(value);
173  }
174 
175 private:
176  union {
177  _StorageType _storage;
178  char _padding[Size];
179  };
180 };
181 
183 
184 #endif // PXR_EXEC_VDF_FIXED_SIZE_HOLDER_H
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
Vdf_FixedSizeHolderRemoteStorage & operator=(Vdf_FixedSizeHolderRemoteStorage const &other)
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
#define __ARCH_PRETTY_FUNCTION__
Definition: functionLite.h:29
LeafData & operator=(const LeafData &)=delete
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Vdf_FixedSizeHolderRemoteStorage(Vdf_FixedSizeHolderRemoteStorage const &other)