CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
weak_ptr.h
1
2#ifndef _aspose_system_weak_ptr_h_
3#define _aspose_system_weak_ptr_h_
4
5#include "smart_ptr.h"
6#include "detail.h"
7
8namespace System
9{
10
16template <class T>
17class ASPOSECPP_SHARED_CLASS WeakPtr : public SmartPtr<T>
18{
19public:
25 using typename SmartPtr_::Pointee_;
26
27private:
28 using SmartPtr_::m_data;
29
30public:
32 WeakPtr(std::nullptr_t = nullptr)
33 : SmartPtr_(nullptr, SmartPtrMode::Weak)
34 {}
39 {}
42 WeakPtr(const SmartPtr_ &ptr)
43 : SmartPtr_(ptr, SmartPtrMode::Weak)
44 {}
48 template<class Q, typename = typename std::enable_if<std::is_convertible<Q*, Pointee_*>::value>::type>
50 : SmartPtr_(x, SmartPtrMode::Weak)
51 {}
54 WeakPtr(const WeakPtr_ &ptr)
55 : SmartPtr_(ptr, SmartPtrMode::Weak)
56 {}
60 template<class Q, typename = typename std::enable_if<std::is_convertible<Q*, Pointee_*>::value>::type>
62 : SmartPtr_(x, SmartPtrMode::Weak)
63 {}
67 : SmartPtr_(x, SmartPtrMode::Weak)
68 {}
72 template <typename Q>
73 WeakPtr& operator = (Q &&value)
74 {
75 SmartPtr_::operator = (value);
76 return *this;
77 }
80 bool operator == (std::nullptr_t) const
81 {
82 return SmartPtr_::operator == (nullptr);
83 }
86 bool expired() const
87 {
88 CODEPORTING_DEBUG_ASSERT(m_data.GetMode() == SmartPtrMode::Weak);
89 return m_data.IsNull();
90 }
94 {
95 CODEPORTING_DEBUG_ASSERT(m_data.GetMode() == SmartPtrMode::Weak);
96 return m_data.IsNull() ? nullptr : m_data.WeakGetCounter()->GetObject();
97 }
98};
101template <class T>
102struct IsWeakPtr : System::detail::is_a<T, System::WeakPtr> {};
103
107template <typename T>
108struct BasePointerType<WeakPtr<T>>
109{
111 using type = ::System::SmartPtr<T>;
112};
114
116template <typename T>
117void PrintTo(const WeakPtr<T>& object_ptr, std::ostream* stream)
118{
119 if (object_ptr == nullptr)
120 *stream << "nullptr";
121 else
122 PrintTo(*object_ptr, stream);
123}
124
129template <typename T>
130std::ostream& operator<<(std::ostream& stream, const WeakPtr<T>& object_ptr)
131{
132 if (object_ptr != nullptr)
133 stream << object_ptr->ToString();
134 return stream;
135}
136
141template <typename T>
142std::wostream& operator<<(std::wostream& stream, const WeakPtr<T>& object_ptr)
143{
144 if (object_ptr != nullptr)
145 stream << object_ptr->ToString();
146 return stream;
147}
148
150template< class X > struct RemoveShared { typedef X type; };
151
153template< class X > struct RemoveShared<System::SharedPtr<X>> { typedef X type; };
154template< class X > struct RemoveShared<System::WeakPtr<X>> { typedef X type; };
156
158template <class T>
159struct WeakPtrFromTypeParameter : std::conditional<IsSmartPtr<T>::value, WeakPtr<typename RemoveShared<T>::type>, T>{};
160
161} // namespace System
162
163namespace std
164{
167 template <class T>
168 struct hash<System::WeakPtr<T> >
169 {
171 using argument_type = System::WeakPtr<T>;
173 using result_type = std::size_t;
177 std::size_t operator()(System::WeakPtr<T> const& val) const
178 {
179 return val.GetHashCode();
180 }
181 };
182}
183
184#endif // _aspose_system_weak_ptr_h_
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:62
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
int GetHashCode() const
Calls GetHashCode() on pointed object.
Definition: smart_ptr.h:877
T Pointee_
Pointed type.
Definition: smart_ptr.h:183
Subclass of System::SmartPtr which sets itself to weak mode at construction. Please note that this cl...
Definition: weak_ptr.h:18
WeakPtr(SmartPtr_ &&x)
Move-constructs weak pointer.
Definition: weak_ptr.h:66
WeakPtr(Pointee_ *object)
Creates weak pointer to given object.
Definition: weak_ptr.h:37
WeakPtr(const SmartPtr< Q > &x)
Creates weak pointer referencing same pointer x points to.
Definition: weak_ptr.h:49
WeakPtr(std::nullptr_t=nullptr)
Creates null pointer.
Definition: weak_ptr.h:32
WeakPtr(const WeakPtr_ &ptr)
Copy-constructs weak pointer.
Definition: weak_ptr.h:54
SmartPtr< T > SmartPtr_
Alias for corresponding SmartPtr class.
Definition: weak_ptr.h:21
WeakPtr(const WeakPtr< Q > &x)
Copy-constructs weak pointer.
Definition: weak_ptr.h:61
WeakPtr(const SmartPtr_ &ptr)
Creates weak pointer referencing same pointer ptr points to.
Definition: weak_ptr.h:42
Object * get_weak() const
Gets referenced object. Asserts that pointer is in weak mode.
Definition: weak_ptr.h:93
bool expired() const
Checks if referenced object was already deleted.
Definition: weak_ptr.h:86
WeakPtr< T > WeakPtr_
Alias for self type.
Definition: weak_ptr.h:23
Definition: db_command.h:9
void PrintTo(DateTime value, std::ostream *stream)
Prints value to ostream. Mostly used for debug.
SmartPtrMode
SmartPtr pointer type: weak or shared. Defines whether pointer is being counted when it is being deci...
Definition: smart_ptr.h:68
@ Weak
Weak mode: pointer does not participate in reference counting.
std::ostream & operator<<(std::ostream &stream, DateTime date_time)
Insert data into the stream using UTF-8 encoding.
Definition: date_time.h:729
bool operator==(ArraySegment< T > a, ArraySegment< T > b)
Definition: array_segment.h:151
Traits class to check if specific class is a specialization of System::WeakPtr. Doesn't check if inst...
Definition: weak_ptr.h:102
Trait structs to remove SharedPtr/WeakPtr from argument type.
Definition: weak_ptr.h:150
X type
Definition: weak_ptr.h:150
Trait struct to convert argument type to a weak-pointer, if it is a pointer type.
Definition: weak_ptr.h:159
Tests if specific type is a specialization of specific template. If it is, inherits std::true_type,...
Definition: detail.h:80