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 >
48 inline typename std::enable_if<
51 >::type
52 UTmakeUnique(REST&&... args)
53 {
54  return UT_UniquePtr<T>(new T(std::forward<REST>(args)...));
55 }
56 
57 /// Constructs an 1D array of type T with len elements and wraps it in an
58 /// UT_UniquePtr.
59 template<class T>
60 inline typename std::enable_if<
63 >::type
64 UTmakeUnique(size_t len)
65 {
66  typedef typename std::remove_extent<T>::type ElemT;
67  return UT_UniquePtr<T>(new ElemT[len]());
68 }
69 
70 /// Construction of arrays of known bound is disallowed. Just do it directly!
71 template<
72  class T,
73  class... REST
74 >
76 UTmakeUnique(REST&&...) = delete;
77 
78 // For UT::ArraySet.
79 namespace UT
80 {
81 template <typename T>
82 struct DefaultClearer;
83 
84 template <typename T>
86 {
87  static void clear(UT_UniquePtr<T> &v) { v.reset(nullptr); }
88  static bool isClear(const UT_UniquePtr<T> &v) { return v.get() == nullptr; }
90  {
91  new ((void *)p) UT_UniquePtr<T>(nullptr);
92  }
93  static const bool clearNeedsDestruction = false;
94 };
95 } // namespace UT
96 
97 #endif // __UT_UNIQUEPTR_H_INCLUDED__
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
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:87
static void clearConstruct(UT_UniquePtr< T > *p)
Definition: UT_UniquePtr.h:89
std::enable_if< !std::is_array< T >::value, UT_UniquePtr< T >>::type UTmakeUnique(REST &&...args)
Definition: UT_UniquePtr.h:52
**If you just want to fire and args
Definition: thread.h:609
static bool isClear(const UT_UniquePtr< T > &v)
Definition: UT_UniquePtr.h:88
type
Definition: core.h:1059