36a7564dbea8402a97497a9a344bf6108035527a
[senf.git] / Utils / intrusive_refcount.hh
1 // $Id$
2 //
3 // Copyright (C) 2006 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief intrusive_refcount public header */
25
26 #ifndef HH_intrusive_refcount_
27 #define HH_intrusive_refcount_ 1
28
29 // Custom includes
30 #include <boost/utility.hpp>
31
32 //#include "intrusive_refcount.mpp"
33 ///////////////////////////////hh.p////////////////////////////////////////
34
35 namespace senf {
36
37     /** \brief Reference count mixin for intrusive_ptr
38
39         This class provides a simple internally managed refcount and supplies the <a
40         href="http://www.boost.org/libs/smart_ptr/intrusive_ptr.html">boost::intrusive_ptr</a>
41         required interface. To make a class compatible with \c boost::intrusive_ptr, just derive
42         publicly from intrusive_refcount.
43
44         Two additional benifits of using intrusive_refcount are
45         \li The object can access it's own refcount
46         \li It is valid and safe to convert a plain object pointer to an intrusive_ptr at any time
47             (not only after new)
48      */
49     class intrusive_refcount
50         : public boost::noncopyable
51     {
52     public:
53         typedef unsigned refcount_t;    ///< reference count type
54
55         virtual ~intrusive_refcount();
56
57         refcount_t refcount();          ///< current refcount
58         bool is_shared();               ///< return \c true if refcount() > 1
59
60     protected:
61         intrusive_refcount();
62         
63     private:
64         void add_ref();
65         bool release();
66
67         refcount_t refcount_;
68
69         friend void senf::intrusive_ptr_add_ref(intrusive_refcount* p);
70         friend void senf::intrusive_ptr_release(intrusive_refcount* p);
71     };
72
73     void intrusive_ptr_add_ref(intrusive_refcount* p);
74     void intrusive_ptr_release(intrusive_refcount* p);
75 }
76
77 ///////////////////////////////hh.e////////////////////////////////////////
78 #include "intrusive_refcount.cci"
79 //#include "intrusive_refcount.ct"
80 //#include "intrusive_refcount.cti"
81 #endif
82
83 \f
84 // Local Variables:
85 // mode: c++
86 // c-file-style: "senf"
87 // fill-column: 100
88 // End: