blob: 3442f1f79f51d643f215176c1a67f0f481c54d94 [file] [log] [blame]
Jeff Thompsonf7d49942013-08-01 16:47:40 -07001#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
Jeff Thompson2277ce52013-08-01 17:34:11 -070021#include <ndnboost/detail/sp_typeinfo.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070022
23namespace ndnboost
24{
25
26namespace detail
27{
28
29class sp_counted_base
30{
31private:
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
39public:
40
41 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
42 {
43 }
44
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
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
64 void add_ref_copy()
65 {
66 ++use_count_;
67 }
68
69 bool add_ref_lock() // true on success
70 {
71 if( use_count_ == 0 ) return false;
72 ++use_count_;
73 return true;
74 }
75
76 void release() // nothrow
77 {
78 if( --use_count_ == 0 )
79 {
80 dispose();
81 weak_release();
82 }
83 }
84
85 void weak_add_ref() // nothrow
86 {
87 ++weak_count_;
88 }
89
90 void weak_release() // nothrow
91 {
92 if( --weak_count_ == 0 )
93 {
94 destroy();
95 }
96 }
97
98 long use_count() const // nothrow
99 {
100 return use_count_;
101 }
102};
103
104} // namespace detail
105
106} // namespace ndnboost
107
108#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_NT_HPP_INCLUDED