active_object_ptr.hh

00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2008 Ciaran McCreesh
00005  *
00006  * This file is part of the Paludis package manager. Paludis is free software;
00007  * you can redistribute it and/or modify it under the terms of the GNU General
00008  * Public License version 2, as published by the Free Software Foundation.
00009  *
00010  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013  * details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_ACTIVE_OBJECT_PTR_HH
00021 #define PALUDIS_GUARD_PALUDIS_UTIL_ACTIVE_OBJECT_PTR_HH 1
00022 
00023 #include <paludis/util/active_object_ptr-fwd.hh>
00024 #include <paludis/util/mutex.hh>
00025 #include <paludis/util/make_shared_ptr.hh>
00026 #include <tr1/memory>
00027 
00028 namespace paludis
00029 {
00030     template <typename T_>
00031     class ActiveObjectPtr
00032     {
00033         private:
00034             T_ _ptr;
00035             std::tr1::shared_ptr<Mutex> _mutex;
00036 
00037             class Deref
00038             {
00039                 private:
00040                     const ActiveObjectPtr * _ptr;
00041                     std::tr1::shared_ptr<Lock> _lock;
00042 
00043                 public:
00044                     Deref(const ActiveObjectPtr * p) :
00045                         _ptr(p),
00046                         _lock(make_shared_ptr(new Lock(*p->_mutex)))
00047                     {
00048                     }
00049 
00050                     const T_ & operator-> () const
00051                     {
00052                         return _ptr->_ptr;
00053                     }
00054             };
00055 
00056             friend class Deref;
00057 
00058         public:
00059             ActiveObjectPtr(const T_ & t) :
00060                 _ptr(t),
00061                 _mutex(new Mutex)
00062             {
00063             }
00064 
00065             ActiveObjectPtr(const ActiveObjectPtr & other) :
00066                 _ptr(other._ptr),
00067                 _mutex(other._mutex)
00068             {
00069             }
00070 
00071             ~ActiveObjectPtr()
00072             {
00073             }
00074 
00075             ActiveObjectPtr &
00076             operator= (const ActiveObjectPtr & other)
00077             {
00078                 if (this != &other)
00079                 {
00080                     _ptr = other._ptr;
00081                     _mutex = other._mutex;
00082                 }
00083                 return *this;
00084             }
00085 
00086             Deref operator-> () const
00087             {
00088                 return Deref(this);
00089             }
00090     };
00091 }
00092 
00093 #endif

Generated on Mon Sep 21 10:36:08 2009 for paludis by  doxygen 1.5.4