paludis Version 3.0.2
The one true package mangler
active_object_ptr.hh
1/* vim: set sw=4 sts=4 et foldmethod=syntax : */
2
3/*
4 * Copyright (c) 2008, 2009, 2010, 2012 Ciaran McCreesh
5 *
6 * This file is part of the Paludis package manager. Paludis is free software;
7 * you can redistribute it and/or modify it under the terms of the GNU General
8 * Public License version 2, as published by the Free Software Foundation.
9 *
10 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#ifndef PALUDIS_GUARD_PALUDIS_UTIL_ACTIVE_OBJECT_PTR_HH
21#define PALUDIS_GUARD_PALUDIS_UTIL_ACTIVE_OBJECT_PTR_HH 1
22
23#include <paludis/util/active_object_ptr-fwd.hh>
24#include <memory>
25#include <mutex>
26
27namespace paludis
28{
29 template <typename T_>
30 class ActiveObjectPtr
31 {
32 private:
33 T_ _ptr;
34 std::shared_ptr<std::mutex> _mutex;
35
36 class Deref
37 {
38 private:
39 const ActiveObjectPtr * _ptr;
40 std::unique_lock<std::mutex> _lock;
41
42 public:
43 Deref(const ActiveObjectPtr * p) :
44 _ptr(p),
45 _lock(*p->_mutex)
46 {
47 }
48
49 const T_ & operator-> () const
50 {
51 return _ptr->_ptr;
52 }
53 };
54
55 friend class Deref;
56
57 public:
58 ActiveObjectPtr(const T_ & t) :
59 _ptr(t),
60 _mutex(std::make_shared<std::mutex>())
61 {
62 }
63
64 ActiveObjectPtr(const ActiveObjectPtr & other) = default;
65
66 ~ActiveObjectPtr() = default;
67
68 ActiveObjectPtr &
69 operator= (const ActiveObjectPtr & other)
70 {
71 if (this != &other)
72 {
73 _ptr = other._ptr;
74 _mutex = other._mutex;
75 }
76 return *this;
77 }
78
79 Deref operator-> () const
80 {
81 return Deref(this);
82 }
83
84 const T_ & value() const
85 {
86 return Deref(this).operator->();
87 }
88 };
89}
90
91#endif
Definition about_metadata-fwd.hh:24