All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
anyUniquePtr.h
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 TF_ANY_UNIQUE_PTR_H
25 #define TF_ANY_UNIQUE_PTR_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/base/tf/api.h"
29 
30 #include <type_traits>
31 
32 PXR_NAMESPACE_OPEN_SCOPE
33 
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 
68  TfAnyUniquePtr& operator=(TfAnyUniquePtr &&other) {
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 
81  ~TfAnyUniquePtr() {
82  _delete(_ptr);
83  }
84 
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 
107 PXR_NAMESPACE_CLOSE_SCOPE
108 
109 #endif
void const * Get() const
Return a pointer to the owned object.
Definition: anyUniquePtr.h:86
A simple type-erased container that provides only destruction, moves and immutable, untyped access to the held value.
Definition: anyUniquePtr.h:43