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  */
11 
12 #ifndef __UT_UNIQUEPTR_H_INCLUDED__
13 #define __UT_UNIQUEPTR_H_INCLUDED__
14 
15 #include <stddef.h>
16 #include <memory>
17 #include <type_traits>
18 #include <utility>
19 
20 /// @file
21 
22 #if defined(__clang__)
23 
24 /// @brief A smart pointer for unique ownership of dynamically allocated objects
25 ///
26 /// UT_UniquePtr mimics a built-in pointer except that it guarantees deletion
27 /// of the object pointed to, either upon destruction or via an explicit
28 /// reset(). UT_UniquePtr is a simple solution for simple needs; use
29 /// UT_SharedPtr/UT_IntrusivePtr if your needs are more complex.
30 ///
31 #define UT_UniquePtr std::unique_ptr
32 
33 #else // !defined(__clang__)
34 
35 /// @brief A smart pointer for unique ownership of dynamically allocated objects
36 ///
37 /// UT_UniquePtr mimics a built-in pointer except that it guarantees deletion
38 /// of the object pointed to, either upon destruction or via an explicit
39 /// reset(). UT_UniquePtr is a simple solution for simple needs; use
40 /// UT_SharedPtr/UT_IntrusivePtr if your needs are more complex.
41 ///
42 /// @{
43 template<
44  class T,
45  class Deleter = std::default_delete<T>
46 >
48 
49 template<
50  class T,
51  class Deleter // = std::default_delete<T>
52 >
53 class UT_UniquePtr : public std::unique_ptr<T, Deleter>
54 {
55 public:
56  using std::unique_ptr<T, Deleter>::unique_ptr;
57 };
58 
59 /// Array specialization of UT_UniquePtr
60 template<
61  class T,
62  class Deleter
63 >
64 class UT_UniquePtr<T[], Deleter> : public std::unique_ptr<T[], Deleter>
65 {
66 public:
67  using std::unique_ptr<T[], Deleter>::unique_ptr;
68 };
69 /// @}
70 
71 #endif // !defined(__clang__)
72 
73 /// Constructs an object of type T and wraps it in an UT_UniquePtr. The args
74 /// are passed to the constructor of T.
75 template<
76  class T,
77  class... REST
78 >
79 inline typename std::enable_if<
82 >::type
83 UTmakeUnique(REST&&... args)
84 {
85  return UT_UniquePtr<T>(new T(std::forward<REST>(args)...));
86 }
87 
88 /// Constructs an 1D array of type T with len elements and wraps it in an
89 /// UT_UniquePtr.
90 template<class T>
91 inline typename std::enable_if<
94 >::type
95 UTmakeUnique(size_t len)
96 {
97  typedef typename std::remove_extent<T>::type ElemT;
98  return UT_UniquePtr<T>(new ElemT[len]());
99 }
100 
101 /// Construction of arrays of known bound is disallowed. Just do it directly!
102 template<
103  class T,
104  class... REST
105 >
107 UTmakeUnique(REST&&...) = delete;
108 
109 // For UT::ArraySet.
110 namespace UT
111 {
112 template <typename T>
113 struct DefaultClearer;
114 
115 template <typename T>
117 {
118  static void clear(UT_UniquePtr<T> &v) { v.reset(nullptr); }
119  static bool isClear(const UT_UniquePtr<T> &v) { return v.get() == nullptr; }
121  {
122  new ((void *)p) UT_UniquePtr<T>(nullptr);
123  }
124  static const bool clearNeedsDestruction = false;
125 };
126 } // namespace UT
127 
128 #endif // __UT_UNIQUEPTR_H_INCLUDED__
const GLdouble * v
Definition: glcorearb.h:836
static void clear(UT_UniquePtr< T > &v)
Definition: UT_UniquePtr.h:118
static void clearConstruct(UT_UniquePtr< T > *p)
Definition: UT_UniquePtr.h:120
std::enable_if< !std::is_array< T >::value, UT_UniquePtr< T >>::type UTmakeUnique(REST &&...args)
Definition: UT_UniquePtr.h:83
GLsizei const GLfloat * value
Definition: glcorearb.h:823
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:47
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
static bool isClear(const UT_UniquePtr< T > &v)
Definition: UT_UniquePtr.h:119