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 /// @brief A smart pointer for unique ownership of dynamically allocated objects
23 ///
24 /// UT_UniquePtr mimics a built-in pointer except that it guarantees deletion
25 /// of the object pointed to, either upon destruction or via an explicit
26 /// reset(). UT_UniquePtr is a simple solution for simple needs; use
27 /// UT_SharedPtr/UT_IntrusivePtr if your needs are more complex.
28 ///
29 template<
30  class T,
31  class Deleter = std::default_delete<T>
32 >
33 using UT_UniquePtr = std::unique_ptr<T, Deleter>;
34 
35 
36 /// Constructs an object of type T and wraps it in an UT_UniquePtr. The args
37 /// are passed to the constructor of T.
38 template<
39  class T,
40  class... REST
41 >
42 inline typename std::enable_if<
45 >::type
46 UTmakeUnique(REST&&... args)
47 {
48  return UT_UniquePtr<T>(new T(std::forward<REST>(args)...));
49 }
50 
51 /// Constructs an 1D array of type T with len elements and wraps it in an
52 /// UT_UniquePtr.
53 template<class T>
54 inline typename std::enable_if<
57 >::type
59 {
60  typedef typename std::remove_extent<T>::type ElemT;
61  return UT_UniquePtr<T>(new ElemT[len]());
62 }
63 
64 /// Construction of arrays of known bound is disallowed. Just do it directly!
65 template<
66  class T,
67  class... REST
68 >
70 UTmakeUnique(REST&&...) = delete;
71 
72 // For UT::ArraySet.
73 namespace UT
74 {
75 template <typename T>
76 struct DefaultClearer;
77 
78 template <typename T>
80 {
81  static void clear(UT_UniquePtr<T> &v) { v.reset(nullptr); }
82  static bool isClear(const UT_UniquePtr<T> &v) { return v.get() == nullptr; }
84  {
85  new ((void *)p) UT_UniquePtr<T>(nullptr);
86  }
87  static const bool clearNeedsDestruction = false;
88 };
89 } // namespace UT
90 
91 #endif // __UT_UNIQUEPTR_H_INCLUDED__
const Args & args
Definition: printf.h:628
const GLdouble * v
Definition: glew.h:1391
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:33
static void clear(UT_UniquePtr< T > &v)
Definition: UT_UniquePtr.h:81
static void clearConstruct(UT_UniquePtr< T > *p)
Definition: UT_UniquePtr.h:83
std::enable_if< !std::is_array< T >::value, UT_UniquePtr< T >>::type UTmakeUnique(REST &&...args)
Definition: UT_UniquePtr.h:46
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
GLfloat GLfloat p
Definition: glew.h:16321
static bool isClear(const UT_UniquePtr< T > &v)
Definition: UT_UniquePtr.h:82
GLsizei const GLfloat * value
Definition: glew.h:1849
type
Definition: core.h:528
GLenum GLsizei len
Definition: glew.h:7752