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>
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 // For UT::ArraySet.
107 namespace UT
108 {
109 template <typename T>
110 struct DefaultClearer;
111 
112 template <typename T>
114 {
115  static void clear(UT_UniquePtr<T> &v) { v.reset(nullptr); }
116  static bool isClear(const UT_UniquePtr<T> &v) { return v.get() == nullptr; }
118  {
119  new ((void *)p) UT_UniquePtr<T>(nullptr);
120  }
121  static const bool clearNeedsDestruction = false;
122 };
123 } // namespace UT
124 
125 #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:115
static void clearConstruct(UT_UniquePtr< T > *p)
Definition: UT_UniquePtr.h:117
UT_UniquePtr< T > UTmakeUnique(REST &&...args)
Definition: UT_UniquePtr.h:50
**If you just want to fire and args
Definition: thread.h:609
static bool isClear(const UT_UniquePtr< T > &v)
Definition: UT_UniquePtr.h:116
UT_UniquePtr< T > UTmakeUniqueForOverwrite()
Construct a default initialized T in a UT_UniquePtr.
Definition: UT_UniquePtr.h:81