Skip to content

Commit fb907f2

Browse files
committed
xrCore: intrusive_ptr refactoring
Thanks to Im-Dex From commit: Im-dex/xray-162@ae4b026
1 parent d2311b8 commit fb907f2

9 files changed

+121
-226
lines changed

src/xrCore/intrusive_ptr.h

Lines changed: 109 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,142 @@
11
////////////////////////////////////////////////////////////////////////////
2-
// Module : intrusive_ptr.h
3-
// Created : 30.07.2004
4-
// Modified : 30.07.2004
5-
// Author : Dmitriy Iassenev
6-
// Description : Intrusive pointer template
2+
// Module : intrusive_ptr.h
3+
// Created : 30.07.2004
4+
// Modified : 8.11.2016 by Im-Dex
5+
// Author : Dmitriy Iassenev
6+
// Description : Intrusive pointer template
77
////////////////////////////////////////////////////////////////////////////
88

99
#pragma once
1010

11-
#include "Common/object_type_traits.h"
12-
13-
#pragma pack(push, 4)
14-
1511
struct intrusive_base
1612
{
17-
u32 m_ref_count;
13+
intrusive_base() XR_NOEXCEPT : m_ref_count(0) {}
1814

19-
IC intrusive_base() : m_ref_count(0) {}
2015
template <typename T>
21-
IC void _release(T* object)
16+
void release(T* object) XR_NOEXCEPT
2217
{
2318
try
2419
{
2520
xr_delete(object);
2621
}
27-
catch (...)
28-
{
29-
}
22+
catch (...) { }
3023
}
24+
25+
void acquire() XR_NOEXCEPT { ++m_ref_count; }
26+
27+
bool release() XR_NOEXCEPT { return --m_ref_count == 0; }
28+
29+
bool released() const XR_NOEXCEPT { return m_ref_count == 0; }
30+
31+
private:
32+
size_t m_ref_count;
3133
};
3234

33-
template <typename object_type, typename base_type = intrusive_base>
35+
template <typename ObjectType, typename BaseType = intrusive_base>
3436
class intrusive_ptr
3537
{
36-
private:
37-
typedef intrusive_ptr<object_type, base_type> self_type;
38-
typedef const object_type* (intrusive_ptr::*unspecified_bool_type)() const;
38+
using object_type = ObjectType;
39+
using base_type = BaseType;
40+
using self_type = intrusive_ptr<object_type, base_type>;
3941

40-
private:
41-
enum
42-
{
43-
result = object_type_traits::is_base_and_derived<base_type, object_type>::value ||
44-
object_type_traits::is_same<base_type, object_type>::value
45-
};
42+
static_assert(std::is_base_of_v<BaseType, ObjectType>,
43+
"ObjectType must be derived from the BaseType");
4644

47-
private:
4845
object_type* m_object;
4946

5047
protected:
51-
IC void dec();
48+
void dec() XR_NOEXCEPT
49+
{
50+
if (!m_object)
51+
return;
52+
53+
if (m_object->release())
54+
m_object->release(m_object);
55+
}
5256

5357
public:
54-
IC intrusive_ptr();
55-
IC intrusive_ptr(object_type* rhs);
56-
IC intrusive_ptr(self_type const& rhs);
57-
IC ~intrusive_ptr();
58-
IC self_type& operator=(object_type* rhs);
59-
IC self_type& operator=(self_type const& rhs);
60-
IC object_type& operator*() const;
61-
IC object_type* operator->() const;
62-
IC bool operator!() const;
63-
IC operator unspecified_bool_type() const { return (!m_object ? 0 : &intrusive_ptr::get); }
64-
IC u32 size();
65-
IC void swap(self_type& rhs);
66-
IC bool equal(const self_type& rhs) const;
67-
IC void set(object_type* rhs);
68-
IC void set(self_type const& rhs);
69-
IC const object_type* get() const;
70-
};
58+
intrusive_ptr() XR_NOEXCEPT : m_object(nullptr) {}
7159

72-
template <typename object_type, typename base_type>
73-
IC bool operator==(intrusive_ptr<object_type, base_type> const& a, intrusive_ptr<object_type, base_type> const& b);
60+
intrusive_ptr(object_type* rhs) XR_NOEXCEPT : m_object(rhs)
61+
{
62+
if (m_object != nullptr)
63+
m_object->acquire();
64+
}
7465

75-
template <typename object_type, typename base_type>
76-
IC bool operator!=(intrusive_ptr<object_type, base_type> const& a, intrusive_ptr<object_type, base_type> const& b);
66+
intrusive_ptr(const self_type& rhs) XR_NOEXCEPT : m_object(rhs.m_object)
67+
{
68+
if (m_object != nullptr)
69+
m_object->acquire();
70+
}
7771

78-
template <typename object_type, typename base_type>
79-
IC bool operator<(intrusive_ptr<object_type, base_type> const& a, intrusive_ptr<object_type, base_type> const& b);
72+
intrusive_ptr(self_type&& rhs) XR_NOEXCEPT : m_object(rhs.m_object) { rhs.m_object = nullptr; }
8073

81-
template <typename object_type, typename base_type>
82-
IC bool operator>(intrusive_ptr<object_type, base_type> const& a, intrusive_ptr<object_type, base_type> const& b);
74+
~intrusive_ptr() XR_NOEXCEPT { dec(); }
8375

84-
template <typename object_type, typename base_type>
85-
IC void swap(intrusive_ptr<object_type, base_type>& lhs, intrusive_ptr<object_type, base_type>& rhs);
76+
self_type& operator=(object_type* rhs) XR_NOEXCEPT
77+
{
78+
dec();
79+
m_object = rhs;
80+
if (m_object != nullptr)
81+
m_object->acquire();
8682

87-
#include "intrusive_ptr_inline.h"
83+
return *this;
84+
}
85+
86+
self_type& operator=(const self_type& rhs) XR_NOEXCEPT
87+
{
88+
dec();
89+
m_object = rhs.m_object;
90+
if (m_object != nullptr)
91+
m_object->acquire();
92+
93+
return *this;
94+
}
95+
96+
self_type& operator=(self_type&& rhs) XR_NOEXCEPT
97+
{
98+
dec();
99+
m_object = rhs.m_object;
100+
if (m_object != nullptr)
101+
rhs.m_object = nullptr;
102+
return *this;
103+
}
104+
105+
object_type& operator*() const XR_NOEXCEPT
106+
{
107+
VERIFY(m_object);
108+
return *m_object;
109+
}
110+
111+
object_type* operator->() const XR_NOEXCEPT
112+
{
113+
VERIFY(m_object);
114+
return m_object;
115+
}
116+
117+
explicit operator bool() const XR_NOEXCEPT { return m_object != nullptr; }
118+
119+
bool operator==(const self_type& rhs) const XR_NOEXCEPT { return m_object == rhs.m_object; }
120+
121+
bool operator!=(const self_type& rhs) const XR_NOEXCEPT { return m_object != rhs.m_object; }
88122

89-
#pragma pack(pop)
123+
bool operator<(const self_type& rhs) const XR_NOEXCEPT { return m_object < rhs.m_object; }
124+
125+
bool operator>(const self_type& rhs) const XR_NOEXCEPT { return m_object > rhs.m_object; }
126+
127+
void swap(self_type& rhs) XR_NOEXCEPT
128+
{
129+
object_type* tmp = m_object;
130+
m_object = rhs.m_object;
131+
rhs.m_object = tmp;
132+
}
133+
134+
const object_type* get() const XR_NOEXCEPT { return m_object; }
135+
};
136+
137+
template <typename object_type, typename base_type>
138+
void swap(intrusive_ptr<object_type, base_type>& lhs,
139+
intrusive_ptr<object_type, base_type>& rhs) XR_NOEXCEPT
140+
{
141+
lhs.swap(rhs);
142+
}

src/xrCore/intrusive_ptr_inline.h

Lines changed: 0 additions & 157 deletions
This file was deleted.

src/xrCore/xrCore.vcxproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@
327327
<ClInclude Include="FS_internal.h" />
328328
<ClInclude Include="FTimer.h" />
329329
<ClInclude Include="intrusive_ptr.h" />
330-
<ClInclude Include="intrusive_ptr_inline.h" />
331330
<ClInclude Include="LocatorAPI.h" />
332331
<ClInclude Include="LocatorAPI_defs.h" />
333332
<ClInclude Include="log.h" />

src/xrCore/xrCore.vcxproj.filters

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,6 @@
563563
<ClInclude Include="intrusive_ptr.h">
564564
<Filter>intrusive_ptr</Filter>
565565
</ClInclude>
566-
<ClInclude Include="intrusive_ptr_inline.h">
567-
<Filter>intrusive_ptr</Filter>
568-
</ClInclude>
569566
<ClInclude Include="memory_monitor.h">
570567
<Filter>memory_monitor</Filter>
571568
</ClInclude>

src/xrGame/LevelGraphDebugRender.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ void LevelGraphDebugRender::DrawRestrictions()
701701
const float yOffset = 0.1;
702702
for (auto& pair : spaceRestrictionMgr.restrictions())
703703
{
704-
if (!pair.second->m_ref_count)
704+
if (pair.second->released())
705705
continue;
706706
if (!pair.second->initialized())
707707
continue;

0 commit comments

Comments
 (0)