forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefCounting.h
More file actions
112 lines (86 loc) · 2.89 KB
/
RefCounting.h
File metadata and controls
112 lines (86 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright Sebastian Jeckel 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef REACT_COMMON_REF_COUNTING_H_INCLUDED
#define REACT_COMMON_REF_COUNTING_H_INCLUDED
#pragma once
#include "react/detail/Defs.h"
#include <condition_variable>
#include <mutex>
/***************************************/ REACT_IMPL_BEGIN /**************************************/
// A non-exception-safe pointer wrapper with conditional intrusive ref counting.
template <typename T>
class IntrusiveRefCountingPtr
{
enum
{
tag_ref_counted = 0x1
};
public:
IntrusiveRefCountingPtr() :
ptrData_( reinterpret_cast<uintptr_t>(nullptr) )
{}
IntrusiveRefCountingPtr(T* ptr) :
ptrData_( reinterpret_cast<uintptr_t>(ptr) )
{
if (ptr != nullptr && ptr->IsRefCounted())
{
ptr->IncRefCount();
ptrData_ |= tag_ref_counted;
}
}
IntrusiveRefCountingPtr(const IntrusiveRefCountingPtr& other) :
ptrData_( other.ptrData_ )
{
if (isValidRefCountedPtr())
Get()->IncRefCount();
}
IntrusiveRefCountingPtr(IntrusiveRefCountingPtr&& other) :
ptrData_( other.ptrData_ )
{
other.ptrData_ = reinterpret_cast<uintptr_t>(nullptr);
}
~IntrusiveRefCountingPtr()
{
if (isValidRefCountedPtr())
Get()->DecRefCount();
}
IntrusiveRefCountingPtr& operator=(const IntrusiveRefCountingPtr& other)
{
if (this != &other)
{
if (other.isValidRefCountedPtr())
other.Get()->IncRefCount();
if (isValidRefCountedPtr())
Get()->DecRefCount();
ptrData_ = other.ptrData_;
}
return *this;
}
IntrusiveRefCountingPtr& operator=(IntrusiveRefCountingPtr&& other)
{
if (this != &other)
{
if (isValidRefCountedPtr())
Get()->DecRefCount();
ptrData_ = other.ptrData_;
other.ptrData_ = reinterpret_cast<uintptr_t>(nullptr);
}
return *this;
}
T* Get() const { return reinterpret_cast<T*>(ptrData_ & ~tag_ref_counted); }
T& operator*() const { return *Get(); }
T* operator->() const { return Get(); }
bool operator==(const IntrusiveRefCountingPtr& other) const { return Get() == other.Get(); }
bool operator!=(const IntrusiveRefCountingPtr& other) const { return !(*this == other); }
private:
bool isValidRefCountedPtr() const
{
return ptrData_ != reinterpret_cast<uintptr_t>(nullptr)
&& (ptrData_ & tag_ref_counted) != 0;
}
uintptr_t ptrData_;
};
/****************************************/ REACT_IMPL_END /***************************************/
#endif // REACT_COMMON_REF_COUNTING_H_INCLUDED