IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
refcounted.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/base.hxx>
7
8namespace ice
9{
10
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
45 {
46 public:
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
92
93} // namespace ice
Definition ptr.hxx:12
Definition refcounted.hxx:82
friend class Ptr
Definition refcounted.hxx:86
Definition refcounted.hxx:45
auto allocator() const noexcept -> ice::Allocator &
Definition refcounted.hxx:50
auto rc_extract(this auto &self, RCPassKey const &pass_key) noexcept
Definition refcounted.hxx:59
void rc_sub(RCPassKey const &pass_key) noexcept
virtual ~RefCounted() noexcept
RefCounted(ice::Allocator &alloc) noexcept
bool rc_unclaimed(RCPassKey const &pass_key) const noexcept
auto rc_add(this auto &self, RCPassKey const &pass_key) noexcept
Definition refcounted.hxx:52
auto rc_stats(RCPassKey const &pass_key) const noexcept -> ice::RefCountStats
auto rc_claim(this auto &self, RCPassKey const &pass_key) noexcept
Definition refcounted.hxx:58
void rc_delete_extracted() noexcept
Definition refcounted.hxx:28
Definition hashmap_details.hxx:13
SPDX-License-Identifier: MIT.
Definition array.hxx:12
ice::detail::RefCounted RefCounted
Definition refcounted.hxx:91
std::int8_t i8
Definition types.hxx:19
auto alloc(ice::usize size) noexcept -> ice::AllocResult
std::uint32_t u32
Definition types.hxx:26
ice::AllocatorBase< ice::build::is_debug||ice::build::is_develop > Allocator
Definition mem_types.hxx:25
std::int16_t i16
Definition types.hxx:20
Definition refcounted.hxx:12
ice::u32 strong_refs
Definition refcounted.hxx:13
ice::u32 weak_refs
Definition refcounted.hxx:14