Repository navigation
Expand file tree
/
Copy pathenable_weak_ptr.hpp
More file actions
84 lines (65 loc) · 3.36 KB
/
Copy pathenable_weak_ptr.hpp
File metadata and controls
84 lines (65 loc) · 3.36 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
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_ *
* /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\ *
* \/____/ \/_/\/___/ \/____/ \ \ \/ \ \ \/ \/__/\/_/ *
* \ \_\ \ \_\ *
* \/_/ \/_/ *
* *
* Copyright (C) 2011-2013 *
* Dominik Charousset <dominik.charousset@haw-hamburg.de> *
* *
* This file is part of libcppa. *
* libcppa 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. *
* *
* libcppa 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 libcppa. If not, see <http://www.gnu.org/licenses/>. *
\******************************************************************************/
#ifndef CPPA_ENABLE_WEAK_PTR_HPP
#define CPPA_ENABLE_WEAK_PTR_HPP
#include <mutex>
#include <utility>
#include <type_traits>
#include "cppa/ref_counted.hpp"
#include "cppa/intrusive_ptr.hpp"
#include "cppa/weak_ptr_anchor.hpp"
#include "cppa/util/shared_spinlock.hpp"
#include "cppa/util/shared_lock_guard.hpp"
namespace cppa {
/**
* @brief Enables derived classes to be used in {@link weak_intrusive_ptr}.
*/
template<class Base, class Subtype>
class enable_weak_ptr : public Base {
template<typename T>
friend class weak_intrusive_ptr;
static_assert(std::is_base_of<ref_counted,Base>::value,
"Base needs to be derived from ref_counted");
protected:
typedef enable_weak_ptr combined_type;
template<typename... Ts>
enable_weak_ptr(Ts&&... args)
: Base(std::forward<Ts>(args)...)
, m_anchor(new weak_ptr_anchor(this)) { }
void request_deletion() {
if (m_anchor->try_expire()) delete this;
}
private:
inline intrusive_ptr<weak_ptr_anchor> get_weak_ptr_anchor() const {
return m_anchor;
}
intrusive_ptr<weak_ptr_anchor> m_anchor;
};
} // namespace cppa
#endif // CPPA_ENABLE_WEAK_PTR_HPP