HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_UniquePtr.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_UniquePtr.h (UT Library, C++)
7  *
8  * COMMENTS:
9  *
10  * RELATION TO THE STL:
11  *
12  * Use UT_UniquePtr instead of std::unique_ptr for a consistent style.
13  * It has special traits defined that make it easily usable in UT_ArraySet
14  * and UT_ArrayMap.
15  *
16  */
17 
18 #ifndef __UT_UNIQUEPTR_H_INCLUDED__
19 #define __UT_UNIQUEPTR_H_INCLUDED__
20 
21 #include <stddef.h>
22 #include <memory> // IWYU pragma: export
23 #include <type_traits>
24 #include <utility>
25 
26 /// @file
27 
28 /// @brief A smart pointer for unique ownership of dynamically allocated objects
29 ///
30 /// UT_UniquePtr mimics a built-in pointer except that it guarantees deletion
31 /// of the object pointed to, either upon destruction or via an explicit
32 /// reset(). UT_UniquePtr is a simple solution for simple needs; use
33 /// UT_SharedPtr/UT_IntrusivePtr if your needs are more complex.
34 ///
35 template<
36  class T,
37  class Deleter = std::default_delete<T>
38 >
39 using UT_UniquePtr = std::unique_ptr<T, Deleter>;
40 
41 
42 /// Constructs an object of type T and wraps it in an UT_UniquePtr. The args
43 /// are passed to the constructor of T.
44 template<
45  class T,
46  class... REST,
47  std::enable_if_t<!std::is_array_v<T>, int> = 0
48 >
49 inline UT_UniquePtr<T>
50 UTmakeUnique(REST&&... args)
51 {
52  return UT_UniquePtr<T>(new T(std::forward<REST>(args)...));
53 }
54 
55 /// Construct a value initialized 1D array of type T with len elements
56 template<
57  class T,
58  std::enable_if_t<std::is_array_v<T> && std::extent_v<T> == 0, int> = 0
59 >
60 inline UT_UniquePtr<T>
61 UTmakeUnique(size_t len)
62 {
63  using ElemT = std::remove_extent_t<T>;
64  return UT_UniquePtr<T>(new ElemT[len]());
65 }
66 
67 /// Construction of arrays of known bound is disallowed. Just do it directly!
68 template<
69  class T,
70  class... REST,
71  std::enable_if_t<std::extent_v<T> != 0, int> = 0
72 >
73 void UTmakeUnique(REST&&...) = delete;
74 
75 /// Construct a default initialized T in a UT_UniquePtr
76 template <
77  class T,
78  std::enable_if_t<!std::is_array_v<T>, int> = 0
79 >
80 inline UT_UniquePtr<T>
82 {
83  return UT_UniquePtr<T>(new T);
84 }
85 
86 /// Construct a default initialized 1D array of type T with len elements
87 template <
88  class T,
89  std::enable_if_t<std::is_array_v<T> && std::extent_v<T> == 0, int> = 0
90 >
91 inline UT_UniquePtr<T>
93 {
94  using ElemT = std::remove_extent_t<T>;
95  return UT_UniquePtr<T>(new ElemT[len]);
96 }
97 
98 /// Construction of arrays of known bound is disallowed. Just do it directly!
99 template <
100  class T,
101  class... REST,
102  std::enable_if_t<std::extent_v<T> != 0, int> = 0
103 >
104 void UTmakeUniqueForOverwrite(REST&&...) = delete;
105 
106 /// Unary equality predicate for UT_Array removeIf()/findIf() methods
107 template <typename T>
109 {
110 public:
112  : myPtr(ptr)
113  {
114  }
115 
116  bool operator()(const UT_UniquePtr<T> &elem)
117  {
118  return myPtr == elem.get();
119  }
120 
121 private:
122  const T *myPtr;
123 };
124 
125 // For UT::ArraySet.
126 namespace UT
127 {
128 template <typename T>
129 struct DefaultClearer;
130 
131 template <typename T>
133 {
134  static void clear(UT_UniquePtr<T> &v) { v.reset(nullptr); }
135  static bool isClear(const UT_UniquePtr<T> &v) { return v.get() == nullptr; }
137  {
138  new ((void *)p) UT_UniquePtr<T>(nullptr);
139  }
140  static const bool clearNeedsDestruction = false;
141 };
142 } // namespace UT
143 
144 #endif // __UT_UNIQUEPTR_H_INCLUDED__
const GLdouble * v
Definition: glcorearb.h:837
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
static void clear(UT_UniquePtr< T > &v)
Definition: UT_UniquePtr.h:134
static void clearConstruct(UT_UniquePtr< T > *p)
Definition: UT_UniquePtr.h:136
UT_UniquePtrIsEqual(const T *ptr)
Definition: UT_UniquePtr.h:111
UT_UniquePtr< T > UTmakeUnique(REST &&...args)
Definition: UT_UniquePtr.h:50
auto ptr(T p) -> const void *
Definition: format.h:4331
**If you just want to fire and args
Definition: thread.h:618
Unary equality predicate for UT_Array removeIf()/findIf() methods.
Definition: UT_UniquePtr.h:108
static bool isClear(const UT_UniquePtr< T > &v)
Definition: UT_UniquePtr.h:135
bool operator()(const UT_UniquePtr< T > &elem)
Definition: UT_UniquePtr.h:116
UT_UniquePtr< T > UTmakeUniqueForOverwrite()
Construct a default initialized T in a UT_UniquePtr.
Definition: UT_UniquePtr.h:81