IceShard
1
A personal game engine project, with development focused on 2D/2.5D games.
Toggle main menu visibility
Loading...
Searching...
No Matches
utils
public
ice
detail
refcounted.hxx
Go to the documentation of this file.
1
3
4
#pragma once
5
#include <
ice/base.hxx
>
6
#include <
ice/mem_allocator.hxx
>
7
8
namespace
ice
9
{
10
11
struct
RefCountStats
12
{
13
ice::u32
strong_refs
;
14
ice::u32
weak_refs
;
15
};
16
17
namespace
detail
18
{
19
20
class
RCPassKey;
21
22
}
// namespace detail
23
24
namespace
concepts
25
{
26
27
template
<
typename
Object>
28
concept
IsRefCounted
=
requires
(std::remove_const_t<Object>&o,
ice::detail::RCPassKey
const
& passkey) {
29
{ o.rc_add(passkey) } -> std::convertible_to<std::remove_const_t<Object>*>;
30
{ o.rc_sub(passkey) } -> std::convertible_to<void>;
31
{ o.rc_stats(passkey) } -> std::convertible_to<ice::RefCountStats>;
32
{ o.rc_unclaimed(passkey) } -> std::convertible_to<bool>;
33
{ o.rc_claim(passkey) } -> std::convertible_to<std::remove_const_t<Object>*>;
34
};
35
36
}
// namespace concepts
37
38
template
<
typename
Object>
requires
ice::concepts::IsRefCounted<Object>
39
class
Ptr
;
40
41
namespace
detail
42
{
43
44
class
RefCounted
45
{
46
public
:
47
RefCounted
(
ice::Allocator
&
alloc
)
noexcept
;
48
virtual
~RefCounted
() noexcept;
49
50
auto
allocator
() const noexcept ->
ice
::
Allocator
& {
return
*_allocator; }
51
52
auto
rc_add
(
this
auto
& self,
RCPassKey
const
& pass_key)
noexcept
{
return
self.rc_add_internal(), ice::addressof(self); }
53
void
rc_sub
(
RCPassKey
const
& pass_key)
noexcept
;
54
55
auto
rc_stats
(
RCPassKey
const
& pass_key)
const
noexcept
->
ice::RefCountStats
;
56
bool
rc_unclaimed
(
RCPassKey
const
& pass_key)
const
noexcept
;
57
58
auto
rc_claim
(
this
auto
& self,
RCPassKey
const
& pass_key)
noexcept
{
return
self.rc_claim_internal(), ice::addressof(self); }
59
auto
rc_extract
(
this
auto
& self,
RCPassKey
const
& pass_key)
noexcept
{
return
self.rc_extract_internal(), ice::addressof(self); }
60
void
rc_delete_extracted
() noexcept;
61
62
private:
63
void
rc_claim_internal() noexcept;
64
void
rc_extract_internal() noexcept;
65
void
rc_add_internal() noexcept;
66
67
private:
68
ice
::
Allocator
* const _allocator =
nullptr
;
69
70
// We start with a weak reference. This means that even if the object was created with an 'new' call,
71
// it will be considered 'dead'.
72
ice
::
i16
_weak = 1;
73
ice
::
i8
_strong = 0;
74
75
static constexpr
ice
::
i8
ClaimMagic_Claimed = 0;
76
static constexpr
ice
::
i8
ClaimMagic_Extracted = 42;
77
static constexpr
ice
::
i8
ClaimMagic_Unclaimed = 123;
78
ice
::
i8
_claim = ClaimMagic_Unclaimed;
79
};
80
81
class RCPassKey
82
{
83
RCPassKey()
noexcept
=
default
;
84
85
template
<
typename
Object>
requires
ice::concepts::IsRefCounted<Object>
86
friend
class
Ptr
;
87
};
88
89
}
// namespace detail
90
91
using
RefCounted
=
ice::detail::RefCounted
;
92
93
}
// namespace ice
base.hxx
ice::Ptr
Definition
ptr.hxx:12
ice::detail::RCPassKey
Definition
refcounted.hxx:82
ice::detail::RCPassKey::Ptr
friend class Ptr
Definition
refcounted.hxx:86
ice::detail::RefCounted
Definition
refcounted.hxx:45
ice::detail::RefCounted::allocator
auto allocator() const noexcept -> ice::Allocator &
Definition
refcounted.hxx:50
ice::detail::RefCounted::rc_extract
auto rc_extract(this auto &self, RCPassKey const &pass_key) noexcept
Definition
refcounted.hxx:59
ice::detail::RefCounted::rc_sub
void rc_sub(RCPassKey const &pass_key) noexcept
ice::detail::RefCounted::~RefCounted
virtual ~RefCounted() noexcept
ice::detail::RefCounted::RefCounted
RefCounted(ice::Allocator &alloc) noexcept
ice::detail::RefCounted::rc_unclaimed
bool rc_unclaimed(RCPassKey const &pass_key) const noexcept
ice::detail::RefCounted::rc_add
auto rc_add(this auto &self, RCPassKey const &pass_key) noexcept
Definition
refcounted.hxx:52
ice::detail::RefCounted::rc_stats
auto rc_stats(RCPassKey const &pass_key) const noexcept -> ice::RefCountStats
ice::detail::RefCounted::rc_claim
auto rc_claim(this auto &self, RCPassKey const &pass_key) noexcept
Definition
refcounted.hxx:58
ice::detail::RefCounted::rc_delete_extracted
void rc_delete_extracted() noexcept
ice::concepts::IsRefCounted
Definition
refcounted.hxx:28
mem_allocator.hxx
ice::detail
Definition
hashmap_details.hxx:13
ice
SPDX-License-Identifier: MIT.
Definition
array.hxx:12
ice::RefCounted
ice::detail::RefCounted RefCounted
Definition
refcounted.hxx:91
ice::i8
std::int8_t i8
Definition
types.hxx:19
ice::alloc
auto alloc(ice::usize size) noexcept -> ice::AllocResult
ice::u32
std::uint32_t u32
Definition
types.hxx:26
ice::Allocator
ice::AllocatorBase< ice::build::is_debug||ice::build::is_develop > Allocator
Definition
mem_types.hxx:25
ice::i16
std::int16_t i16
Definition
types.hxx:20
ice::RefCountStats
Definition
refcounted.hxx:12
ice::RefCountStats::strong_refs
ice::u32 strong_refs
Definition
refcounted.hxx:13
ice::RefCountStats::weak_refs
ice::u32 weak_refs
Definition
refcounted.hxx:14
Generated by
1.18.0