FCT
载入中...
搜索中...
未找到
TlsPtr.h
浏览该文件的文档.
1
//
2
// Created by Administrator on 2025/2/25.
3
//
4
#include "
../ThirdParty.h
"
5
#include "
./RefCount.h
"
6
#ifndef TLSPTR_H
7
#define TLSPTR_H
8
namespace
FCT
{
9
template
<
typename
T>
10
class
TlsPtr
{
11
public
:
12
TlsPtr
(T *ptr) :
m_ptr
(ptr) {
13
#ifdef FCT_DEBUG_MODE
14
m_id = std::this_thread::get_id();
15
#endif
// FCT_DEBUG_MODE
16
}
17
TlsPtr
(
const
TlsPtr
&other) :
m_ptr
(other.
m_ptr
) {
18
safeAddRef
(
m_ptr
);
19
}
20
~TlsPtr
() {
21
safeRelease
(
m_ptr
);
22
}
23
TlsPtr
(
TlsPtr
&&other) :
m_ptr
(other.
m_ptr
) {
24
#ifdef FCT_DEBUG_MODE
25
m_id = std::move(other.m_id);
26
#endif
// FCT_DEBUG_MODE
27
other.m_ptr =
nullptr
;
28
}
29
30
T *
operator*
()
const
{
31
#ifdef FCT_DEBUG_MODE
32
if
(std::this_thread::get_id()!= m_id) {
33
//todo: error handling
34
}
35
#endif
// FCT_DEBUG_MODE
36
return
m_ptr
;
37
}
38
TlsPtr
&
operator=
(
TlsPtr
&& other) {
39
if
(
this
!= &other) {
40
if
(
m_ptr
)
m_ptr
->release();
41
m_ptr
= other.m_ptr;
42
#ifdef FCT_DEBUG_MODE
43
m_id = std::move(other.m_id);
44
#endif
// FCT_DEBUG_MODE
45
other.m_ptr =
nullptr
;
46
}
47
return
*
this
;
48
}
49
50
T *
get
()
const
{
51
return
*
this
;
52
}
53
54
T *
operator->
()
const
{
// 添加 const 修饰符
55
#ifdef FCT_DEBUG_MODE
56
if
(std::this_thread::get_id() != m_id) {
57
// TODO: error handling
58
}
59
#endif
// FCT_DEBUG_MODE
60
return
m_ptr
;
// 直接返回 m_ptr
61
}
62
63
private
:
64
T *
m_ptr
;
65
#ifdef FCT_DEBUG_MODE
66
std::thread::id m_id;
67
#endif
// FCT_DEBUG_MODE
68
};
69
70
#endif
//TLSPTR_H
71
}
RefCount.h
ThirdParty.h
FCT::TlsPtr::operator=
TlsPtr & operator=(TlsPtr &&other)
定义
TlsPtr.h:38
FCT::TlsPtr::operator*
T * operator*() const
定义
TlsPtr.h:30
FCT::TlsPtr::get
T * get() const
定义
TlsPtr.h:50
FCT::TlsPtr::~TlsPtr
~TlsPtr()
定义
TlsPtr.h:20
FCT::TlsPtr::TlsPtr
TlsPtr(TlsPtr &&other)
定义
TlsPtr.h:23
FCT::TlsPtr::TlsPtr
TlsPtr(const TlsPtr &other)
定义
TlsPtr.h:17
FCT::TlsPtr::operator->
T * operator->() const
定义
TlsPtr.h:54
FCT::TlsPtr::TlsPtr
TlsPtr(T *ptr)
定义
TlsPtr.h:12
FCT::TlsPtr::m_ptr
T * m_ptr
定义
TlsPtr.h:64
FCT
定义
Enums.h:8
FCT::safeRelease
void safeRelease(RefCount *&obj)
定义
RefCount.h:87
FCT::safeAddRef
void safeAddRef(RefCount *obj)
定义
RefCount.h:84
src
MutilThreadBase
TlsPtr.h
制作者
1.14.0