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