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
8namespace 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) {
19 }
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 }
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:
65#ifdef FCT_DEBUG_MODE
66 std::thread::id m_id;
67#endif // FCT_DEBUG_MODE
68 };
69
70#endif //TLSPTR_H
71}
TlsPtr & operator=(TlsPtr &&other)
T * operator*() const
T * get() const
TlsPtr(TlsPtr &&other)
TlsPtr(const TlsPtr &other)
T * operator->() const
TlsPtr(T *ptr)
void safeRelease(RefCount *&obj)
void safeAddRef(RefCount *obj)