paludis  Version 2.6.0
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 
27 namespace 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) :
65  _ptr(other._ptr),
66  _mutex(other._mutex)
67  {
68  }
69 
70  ~ActiveObjectPtr()
71  {
72  }
73 
74  ActiveObjectPtr &
75  operator= (const ActiveObjectPtr & other)
76  {
77  if (this != &other)
78  {
79  _ptr = other._ptr;
80  _mutex = other._mutex;
81  }
82  return *this;
83  }
84 
85  Deref operator-> () const
86  {
87  return Deref(this);
88  }
89 
90  const T_ & value() const
91  {
92  return Deref(this).operator->();
93  }
94  };
95 }
96 
97 #endif
Definition: about_metadata-fwd.hh:23
STL namespace.