1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_NT_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_NT_HPP_INCLUDED
3 
4 // MS compatible compilers support #pragma once
5 
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
7 # pragma once
8 #endif
9 
10 //
11 //  detail/sp_counted_base_nt.hpp
12 //
13 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
14 //  Copyright 2004-2005 Peter Dimov
15 //
16 // Distributed under the Boost Software License, Version 1.0. (See
17 // accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
19 //
20 
21 #include <boost/detail/sp_typeinfo.hpp>
22 
23 namespace boost
24 {
25 
26 namespace detail
27 {
28 
29 class sp_counted_base
30 {
31 private:
32 
33     sp_counted_base( sp_counted_base const & );
34     sp_counted_base & operator= ( sp_counted_base const & );
35 
36     long use_count_;        // #shared
37     long weak_count_;       // #weak + (#shared != 0)
38 
39 public:
40 
sp_counted_base()41     sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
42     {
43     }
44 
~sp_counted_base()45     virtual ~sp_counted_base() // nothrow
46     {
47     }
48 
49     // dispose() is called when use_count_ drops to zero, to release
50     // the resources managed by *this.
51 
52     virtual void dispose() = 0; // nothrow
53 
54     // destroy() is called when weak_count_ drops to zero.
55 
destroy()56     virtual void destroy() // nothrow
57     {
58         delete this;
59     }
60 
61     virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
62     virtual void * get_untyped_deleter() = 0;
63 
add_ref_copy()64     void add_ref_copy()
65     {
66         ++use_count_;
67     }
68 
add_ref_lock()69     bool add_ref_lock() // true on success
70     {
71         if( use_count_ == 0 ) return false;
72         ++use_count_;
73         return true;
74     }
75 
release()76     void release() // nothrow
77     {
78         if( --use_count_ == 0 )
79         {
80             dispose();
81             weak_release();
82         }
83     }
84 
weak_add_ref()85     void weak_add_ref() // nothrow
86     {
87         ++weak_count_;
88     }
89 
weak_release()90     void weak_release() // nothrow
91     {
92         if( --weak_count_ == 0 )
93         {
94             destroy();
95         }
96     }
97 
use_count() const98     long use_count() const // nothrow
99     {
100         return use_count_;
101     }
102 };
103 
104 } // namespace detail
105 
106 } // namespace boost
107 
108 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_NT_HPP_INCLUDED
109