HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_SharedPtr.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_SharedPtr.h (UT Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UT_SHAREDPTR_H_INCLUDED__
13 #define __UT_SHAREDPTR_H_INCLUDED__
14 
15 #include <memory>
16 #include <type_traits>
17 #include <utility>
18 #include <stddef.h>
19 
20 /// @brief Wrapper around std::shared_ptr
21 ///
22 /// An shared pointer takes ownership of the object referenced. The reference
23 /// counting is done outside the object interface, so the size of a
24 /// UT_SharedPtr is larger than a vanilla pointer.
25 ///
26 /// As this is a wrapper around the boost shared pointer template, the type (T)
27 /// must have a <b>Copy Constructor</b> and a valid <b>Assignment Operator</b>
28 /// @see UT_IntrusivePtr
29 template <typename T>
30 using UT_SharedPtr = std::shared_ptr<T>;
31 
32 template <typename T>
33 using UTenable_shared_from_this = std::enable_shared_from_this<T>;
34 
35 #define UTallocateShared std::allocate_shared
36 
37 #define UTstatic_pointer_cast std::static_pointer_cast
38 #define UTdynamic_pointer_cast std::dynamic_pointer_cast
39 #define UTconst_pointer_cast std::const_pointer_cast
40 #define UTreinterpret_pointer_cast std::reinterpret_pointer_cast
41 
42 template <typename T>
43 using UT_WeakPtr = std::weak_ptr<T>;
44 
45 /// Return a UT_SharedPtr<T> that points to an array of T[len] buffer.
46 /// Only needed until we have proper support in C++20.
47 /// TODO: Bring back r389476 when macOS has __cpp_lib_shared_ptr_arrays
48 /// implemented.
49 template <typename T>
50 inline UT_SharedPtr<T>
52 {
53  return UT_SharedPtr<T>(new T[len], [](T* p) { delete [] p; });
54 }
55 
56 // is_(un)bounded_array here only until C++20 arrives
57 namespace UT {
58 namespace detail {
59  template<class T>
60  struct is_unbounded_array : std::false_type {};
61  template<class T>
62  struct is_unbounded_array<T[]> : std::true_type {};
63 
64  template<class T>
65  struct is_bounded_array : std::false_type {};
66  template<class T, std::size_t N>
67  struct is_bounded_array<T[N]> : std::true_type {};
68 }}
69 
70 /// Create UT_SharedPtr<T> to T constructed with args
71 template <typename T, typename... Args>
72 inline std::enable_if_t<
75 >
76 UTmakeShared(Args&&... args)
77 {
78  return std::make_shared<T>(std::forward<Args>(args)...);
79 }
80 
81 /// Create UT_SharedPtr<T[]> that points to an array of T[len] buffer
82 /// @warn This is currently disabled until C++20
83 template <typename T>
84 inline std::enable_if_t<
87 >
89 {
90  // This cannot be enabled until all supported compilers have
91  // __cpp_lib_shared_ptr_arrays defined
92 #if 0
93  return std::make_shared<T>(len);
94 #else
95  static_assert(sizeof(std::remove_extent_t<T>) == 0,
96  "Not allowed until C++20, use UTmakeSharedArrayPtr<T>() instead");
97 #endif
98 }
99 
100 /// Create UT_SharedPtr<T[N]> that points to a T[N] fixed array
101 /// @warn This is currently disabled until C++20
102 template <typename T>
103 inline std::enable_if_t<
106 >
108 {
109  // This cannot be enabled until all supported compilers have
110  // __cpp_lib_shared_ptr_arrays defined
111 #if 0
112  return std::make_shared<T>();
113 #else
114  static_assert(sizeof(std::remove_extent_t<T>) == 0,
115  "Not allowed until C++20, use UTmakeSharedArrayPtr<T>() instead");
116 #endif
117 }
118 
119 
120 // For UT::ArraySet.
121 namespace UT
122 {
123 template <typename T>
124 struct DefaultClearer;
125 
126 template <typename T>
128 {
129  static void clear(UT_SharedPtr<T> &v) { v = UT_SharedPtr<T>(); }
130  static bool isClear(const UT_SharedPtr<T> &v) { return v.get() == nullptr; }
132  {
133  new ((void *)p) UT_SharedPtr<T>(nullptr);
134  }
135  static const bool clearNeedsDestruction = false;
136 };
137 } // namespace UT
138 
139 #endif // __UT_SHAREDPTR_H_INCLUDED__
UT_SharedPtr< T > UTmakeSharedArrayPtr(size_t len)
Definition: UT_SharedPtr.h:51
static void clearConstruct(UT_SharedPtr< T > *p)
Definition: UT_SharedPtr.h:131
std::enable_shared_from_this< T > UTenable_shared_from_this
Definition: UT_SharedPtr.h:33
static bool isClear(const UT_SharedPtr< T > &v)
Definition: UT_SharedPtr.h:130
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:30
GLenum GLsizei len
Definition: glew.h:7782
const GLdouble * v
Definition: glcorearb.h:836
GLfloat GLfloat p
Definition: glew.h:16656
std::enable_if_t< !std::is_array< T >::value, UT_SharedPtr< T >> UTmakeShared(Args &&...args)
Create UT_SharedPtr<T> to T constructed with args.
Definition: UT_SharedPtr.h:76
static void clear(UT_SharedPtr< T > &v)
Definition: UT_SharedPtr.h:129
GA_API const UT_StringHolder N
GLsizei const GLfloat * value
Definition: glcorearb.h:823
**If you just want to fire and args
Definition: thread.h:615
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:259
std::weak_ptr< T > UT_WeakPtr
Definition: UT_SharedPtr.h:43