liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//////////////////////////////////////////////////////////////////////////////
// (c) Copyright Andreas Huber Doenni 2002-2005, Eric Niebler 2006
// Distributed under the Boost Software License, Version 1.0. (See accompany-
// ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//////////////////////////////////////////////////////////////////////////////
 
#ifndef BOOST_XPRESSIVE_DETAIL_UTILITY_COUNTED_BASE_HPP_EAN_04_16_2006
#define BOOST_XPRESSIVE_DETAIL_UTILITY_COUNTED_BASE_HPP_EAN_04_16_2006
 
#include <boost/assert.hpp>
#include <boost/checked_delete.hpp>
#include <boost/detail/atomic_count.hpp>
 
namespace boost { namespace xpressive { namespace detail
{
    template<typename Derived>
    struct counted_base_access;
 
    //////////////////////////////////////////////////////////////////////////////
    // counted_base
    template<typename Derived>
    struct counted_base
    {
        long use_count() const
        {
            return this->count_;
        }
 
    protected:
        counted_base()
          : count_(0)
        {
        }
 
        counted_base(counted_base<Derived> const &)
          : count_(0)
        {
        }
 
        counted_base &operator =(counted_base<Derived> const &)
        {
            return *this;
        }
 
    private:
        friend struct counted_base_access<Derived>;
        mutable boost::detail::atomic_count count_;
    };
 
    //////////////////////////////////////////////////////////////////////////////
    // counted_base_access
    template<typename Derived>
    struct counted_base_access
    {
        static void add_ref(counted_base<Derived> const *that)
        {
            ++that->count_;
        }
 
        static void release(counted_base<Derived> const *that)
        {
            BOOST_ASSERT(0 < that->count_);
            if(0 == --that->count_)
            {
                boost::checked_delete(static_cast<Derived const *>(that));
            }
        }
    };
 
    template<typename Derived>
    inline void intrusive_ptr_add_ref(counted_base<Derived> const *that)
    {
        counted_base_access<Derived>::add_ref(that);
    }
 
    template<typename Derived>
    inline void intrusive_ptr_release(counted_base<Derived> const *that)
    {
        counted_base_access<Derived>::release(that);
    }
 
}}} // namespace boost::xpressive::detail
 
#endif