forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharedPtr.h
105 lines (95 loc) · 3.38 KB
/
SharedPtr.h
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
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2014 Wang Bin <[email protected]>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#ifndef SHARED_PTR
#define SHARED_PTR
#include <algorithm>
#include <QtCore/QAtomicInt>
/*
* a simple thread safe shared ptr. QSharedPointer does not provide a way to get how many the ref count is.
*/
namespace impl {
template <typename T>
class SharedPtrImpl {
public:
explicit SharedPtrImpl(T *ptr = 0) : m_ptr(ptr), m_counter(1) { }
~SharedPtrImpl() { delete m_ptr;}
T * operator->() const { return m_ptr;}
T & operator*() const { return *m_ptr;}
T * get() const { return m_ptr;}
void ref() { m_counter.ref(); }
// return false if becomes 0
bool deref() { return m_counter.deref();}
int count() const {
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) || QT_VERSION >= QT_VERSION_CHECK(5, 3, 0)
return m_counter;
#else
return m_counter.load();
#endif
}
bool isNull() const { return !m_ptr;}
private:
T *m_ptr;
QAtomicInt m_counter;
};
} // namespace impl
template <typename T>
class SharedPtr {
public:
explicit SharedPtr(T *ptr = 0) : m_impl(new impl::SharedPtrImpl<T>(ptr)) { }
template <typename U>
explicit SharedPtr(U *ptr = 0) : m_impl(new impl::SharedPtrImpl<T>(ptr)) { }
~SharedPtr() {
if (!m_impl->deref())
delete m_impl;
}
SharedPtr(const SharedPtr &other) {
m_impl = other.m_impl;
m_impl->ref();
}
template <typename U>
SharedPtr(const SharedPtr<U> &other) {
m_impl = other.m_impl;
m_impl->ref();
}
SharedPtr & operator=(const SharedPtr &other) {
SharedPtr tmp(other);
swap(tmp);
return *this;
}
template <typename U>
SharedPtr<T> & operator=(const SharedPtr<U> &other) {
SharedPtr<T> tmp(other);
swap(tmp);
return *this;
}
T * operator->() const { return m_impl->operator->();}
T & operator*() const { return **m_impl;}
bool operator !() const { return isNull();}
template <typename U>
bool operator<(const SharedPtr<U> &other) { return m_impl->get() < other.get();}
bool isNull() const { return m_impl->isNull();}
T * get() const { return m_impl->get();}
int count() const { return m_impl->count();}
void swap(SharedPtr<T> &other) { std::swap(m_impl, other.m_impl);}
private:
impl::SharedPtrImpl<T> *m_impl;
};
template <typename T>
void swap(SharedPtr<T> &left, SharedPtr<T> &right) {
left.swap(right);
}
#endif //SHARED_PTR