HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
anyUniquePtr.h
Go to the documentation of this file.
1 //
2 // Copyright 2019 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 PXR_BASE_TF_ANY_UNIQUE_PTR_H
25 #define PXR_BASE_TF_ANY_UNIQUE_PTR_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/base/tf/api.h"
29 
30 #include <type_traits>
31 
33 
34 /// A simple type-erased container that provides only destruction, moves and
35 /// immutable, untyped access to the held value.
36 ///
37 /// There are a couple of facilities that provide fallback or default values
38 /// in error cases. TfAnyUniquePtr exists to hold these oft-instantiated but
39 /// rarely accessed values. As such, its design prioritizes compile-time
40 /// overhead over runtime performance and avoids clever metaprogramming.
41 /// Please resist the urge to add functionality to this class (e.g. small
42 /// object optimization, hboost::python interoperability.)
44 {
45 public:
46  template <typename T>
47  static TfAnyUniquePtr New() {
48  static_assert(!std::is_array<T>::value, "Array types not supported");
49  return TfAnyUniquePtr(new T());
50  }
51 
52  template <typename T>
53  static TfAnyUniquePtr New(T const &v) {
54  static_assert(!std::is_array<T>::value, "Array types not supported");
55  return TfAnyUniquePtr(new T(v));
56  }
57 
59  : _ptr(other._ptr)
60  , _delete(other._delete)
61  {
62  other._ptr = nullptr;
63  // We don't set other._delete to nullptr here on purpose. Invoking
64  // delete on a null pointer is not an error so if we can ensure that
65  // _delete is never null we can call it unconditionally.
66  }
67 
69  if (this != &other) {
70  _delete(_ptr);
71  _ptr = other._ptr;
72  _delete = other._delete;
73  other._ptr = nullptr;
74  }
75  return *this;
76  }
77 
78  TfAnyUniquePtr(TfAnyUniquePtr const&) = delete;
79  TfAnyUniquePtr& operator=(TfAnyUniquePtr const&) = delete;
80 
82  _delete(_ptr);
83  }
84 
85  /// Return a pointer to the owned object.
86  void const *Get() const {
87  return _ptr;
88  }
89 
90 private:
91  template <typename T>
92  explicit TfAnyUniquePtr(T const *ptr)
93  : _ptr(ptr)
94  , _delete(&_Delete<T>)
95  {}
96 
97  template <typename T>
98  static void _Delete(void const *ptr) {
99  delete static_cast<T const *>(ptr);
100  }
101 
102 private:
103  void const *_ptr;
104  void (*_delete)(void const *);
105 };
106 
108 
109 #endif
void
Definition: png.h:1083
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
TfAnyUniquePtr & operator=(TfAnyUniquePtr &&other)
Definition: anyUniquePtr.h:68
TfAnyUniquePtr(TfAnyUniquePtr &&other)
Definition: anyUniquePtr.h:58
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
static TfAnyUniquePtr New(T const &v)
Definition: anyUniquePtr.h:53
auto ptr(T p) -> const void *
Definition: format.h:2448
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
static TfAnyUniquePtr New()
Definition: anyUniquePtr.h:47
void const * Get() const
Return a pointer to the owned object.
Definition: anyUniquePtr.h:86