CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
smart_ptr_counter.h
1
2#ifndef _aspose_system_smart_ptr_counter_h_
3#define _aspose_system_smart_ptr_counter_h_
4
5
6#include "refcount.h"
7
8
9namespace System {
10
11class Object;
12
13}
14
15
16namespace System { namespace Detail {
17
21class SmartPtrCounter
22{
23public:
25 enum ObjectOwnershipState {
27 BeingConstructed,
29 OwnedByPointers,
31 Deleted
32 };
33
36 SmartPtrCounter(Object *o)
37 : m_object(o)
38#ifdef ENABLE_EXTERNAL_REFCOUNT
39 // A little messy logic here.
40 // OwnedByPointers flag shows whether the object should be deleted when the reference count reaches zero.
41 // When the object is created by MakeObject(), it is false at the beginning and then turns true when all constructors succeed (no exception thrown).
42 // Both global (NextOwnedByPointers) and object-local (m_ownedByPointers) flags are explicitly modified by MakeObject() in this case.
43 // But, if the object is created by operator new, no such control is possible, so it is considered owned by pointers from the beginning.
44 // That's why we can only set NextOwnedByPointers value to false for a short time (until the next object gets created).
45 // The better approach is to forbid creating objects with operator new() rather than with MakeObject() call, but this requires revising the whole asposecpp library and will
46 // inevitably be broken again anyway.
47 // So, this messy magic seems the most reliable way to allow for what we want.
48 , m_ownership(NextOwnership())
49#endif
50 {}
51
54 Object* GetObject() const
55 {
56 return m_object;
57 }
60 SmartPtrCounter* WeakRefAdded()
61 {
62 ++m_pointers;
63 return this;
64 }
66 void WeakRefRemoved()
67 {
68 if (--m_pointers == 0 && m_object == nullptr)
69 delete this;
70 }
71
72#ifdef ENABLE_EXTERNAL_REFCOUNT
75 Object* SharedRefAdded()
76 {
77 ++m_pointers;
78 ++m_shared_pointers;
79 return m_object;
80 }
83 int SharedRefRemovedSafe()
84 {
85 const int result = --m_shared_pointers;
86 --m_pointers;
87 return result;
88 }
92 int DecreaseSharedRef(int count)
93 {
94 const int shared_pointers_left = m_shared_pointers -= count;
95 m_pointers -= count;
96 return shared_pointers_left;
97 }
100 int SharedCount() const
101 {
102 return m_shared_pointers;
103 }
106 ASPOSECPP_SHARED_API static ObjectOwnershipState& NextOwnership();
108 void CreatedSuccessfully()
109 {
110 m_ownership = OwnedByPointers;
111 }
112 ObjectOwnershipState GetOwnershipState() const
113 {
114 return m_ownership;
115 }
116#endif
117
119 [[noreturn]] ASPOSECPP_SHARED_API static void ThrowNullReferenceException();
120
122 [[noreturn]] ASPOSECPP_SHARED_API static void ThrowInvalidCastException();
123
126 ASPOSECPP_SHARED_API Object* Lock();
127
130 bool CanBeDeleted() const
131 {
132 return m_pointers == 0;
133 }
135 void Detach()
136 {
137 m_object = nullptr;
138#ifdef ENABLE_EXTERNAL_REFCOUNT
139 m_ownership = Deleted;
140#endif
141 }
142
143private:
145 Object *m_object;
147 RefCount m_pointers;
148#ifdef ENABLE_EXTERNAL_REFCOUNT
150 RefCount m_shared_pointers;
152 ObjectOwnershipState m_ownership;
153#endif
154
155 // Explicitly deleting some members to avoid warnings.
156 SmartPtrCounter(const SmartPtrCounter&) = delete;
157 SmartPtrCounter(SmartPtrCounter&&) = delete;
158 SmartPtrCounter& operator = (const SmartPtrCounter&) = delete;
159 SmartPtrCounter& operator = (SmartPtrCounter&&) = delete;
160};
161
162
163} } //namespace System::Detail
164
165
166#endif //_aspose_system_smart_ptr_counter_h_
Definition: db_command.h:9