Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:50:35

0001 #ifndef BOOST_SMART_PTR_DETAIL_LOCAL_COUNTED_BASE_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_DETAIL_LOCAL_COUNTED_BASE_HPP_INCLUDED
0003 
0004 // MS compatible compilers support #pragma once
0005 
0006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
0007 # pragma once
0008 #endif
0009 
0010 //  detail/local_counted_base.hpp
0011 //
0012 //  Copyright 2017 Peter Dimov
0013 //
0014 //  Distributed under the Boost Software License, Version 1.0. (See
0015 //  accompanying file LICENSE_1_0.txt or copy at
0016 //  http://www.boost.org/LICENSE_1_0.txt)
0017 //
0018 //  See http://www.boost.org/libs/smart_ptr/ for documentation.
0019 
0020 #include <boost/smart_ptr/detail/shared_count.hpp>
0021 #include <boost/config.hpp>
0022 #include <utility>
0023 
0024 namespace boost
0025 {
0026 
0027 namespace detail
0028 {
0029 
0030 class BOOST_SYMBOL_VISIBLE local_counted_base
0031 {
0032 private:
0033 
0034     local_counted_base & operator= ( local_counted_base const & );
0035 
0036 private:
0037 
0038     // not 'int' or 'unsigned' to avoid aliasing and enable optimizations
0039     enum count_type { min_ = 0, initial_ = 1, max_ = 2147483647 };
0040 
0041     count_type local_use_count_;
0042 
0043 public:
0044 
0045     constexpr local_counted_base() noexcept: local_use_count_( initial_ )
0046     {
0047     }
0048 
0049     constexpr local_counted_base( local_counted_base const & ) noexcept: local_use_count_( initial_ )
0050     {
0051     }
0052 
0053     virtual ~local_counted_base() /*noexcept*/
0054     {
0055     }
0056 
0057     virtual void local_cb_destroy() noexcept = 0;
0058 
0059     virtual boost::detail::shared_count local_cb_get_shared_count() const noexcept = 0;
0060 
0061     void add_ref() noexcept
0062     {
0063 #if !defined(__NVCC__)
0064 #if defined( __has_builtin )
0065 # if __has_builtin( __builtin_assume )
0066 
0067         __builtin_assume( local_use_count_ >= 1 );
0068 
0069 # endif
0070 #endif
0071 #endif
0072 
0073         local_use_count_ = static_cast<count_type>( local_use_count_ + 1 );
0074     }
0075 
0076     void release() noexcept
0077     {
0078         local_use_count_ = static_cast<count_type>( local_use_count_ - 1 );
0079 
0080         if( local_use_count_ == 0 )
0081         {
0082             local_cb_destroy();
0083         }
0084     }
0085 
0086     long local_use_count() const noexcept
0087     {
0088         return local_use_count_;
0089     }
0090 };
0091 
0092 class BOOST_SYMBOL_VISIBLE local_counted_impl: public local_counted_base
0093 {
0094 private:
0095 
0096     local_counted_impl( local_counted_impl const & );
0097 
0098 private:
0099 
0100     shared_count pn_;
0101 
0102 public:
0103 
0104     explicit local_counted_impl( shared_count const& pn ) noexcept: pn_( pn )
0105     {
0106     }
0107 
0108     explicit local_counted_impl( shared_count && pn ) noexcept: pn_( std::move(pn) )
0109     {
0110     }
0111 
0112     void local_cb_destroy() noexcept override
0113     {
0114         delete this;
0115     }
0116 
0117     boost::detail::shared_count local_cb_get_shared_count() const noexcept override
0118     {
0119         return pn_;
0120     }
0121 };
0122 
0123 class BOOST_SYMBOL_VISIBLE local_counted_impl_em: public local_counted_base
0124 {
0125 public:
0126 
0127     shared_count pn_;
0128 
0129     void local_cb_destroy() noexcept override
0130     {
0131         shared_count().swap( pn_ );
0132     }
0133 
0134     boost::detail::shared_count local_cb_get_shared_count() const noexcept override
0135     {
0136         return pn_;
0137     }
0138 };
0139 
0140 } // namespace detail
0141 
0142 } // namespace boost
0143 
0144 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_LOCAL_COUNTED_BASE_HPP_INCLUDED