1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/stdapis/boost/detail/shared_count.hpp Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,376 @@
1.4 +#ifndef BOOST_DETAIL_SHARED_COUNT_HPP_INCLUDED
1.5 +#define BOOST_DETAIL_SHARED_COUNT_HPP_INCLUDED
1.6 +
1.7 +// MS compatible compilers support #pragma once
1.8 +
1.9 +#if defined(_MSC_VER) && (_MSC_VER >= 1020)
1.10 +# pragma once
1.11 +#endif
1.12 +
1.13 +//
1.14 +// detail/shared_count.hpp
1.15 +//
1.16 +// Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
1.17 +// Copyright 2004-2005 Peter Dimov
1.18 +//
1.19 +// Distributed under the Boost Software License, Version 1.0. (See
1.20 +// accompanying file LICENSE_1_0.txt or copy at
1.21 +// http://www.boost.org/LICENSE_1_0.txt)
1.22 +//
1.23 +
1.24 +#ifdef __BORLANDC__
1.25 +# pragma warn -8027 // Functions containing try are not expanded inline
1.26 +#endif
1.27 +
1.28 +#include <boost/config.hpp>
1.29 +#include <boost/checked_delete.hpp>
1.30 +#include <boost/throw_exception.hpp>
1.31 +#include <boost/detail/bad_weak_ptr.hpp>
1.32 +#include <boost/detail/sp_counted_base.hpp>
1.33 +#include <boost/detail/sp_counted_impl.hpp>
1.34 +
1.35 +#include <memory> // std::auto_ptr
1.36 +#include <functional> // std::less
1.37 +#include <new> // std::bad_alloc
1.38 +#include <typeinfo> // std::type_info in get_deleter
1.39 +
1.40 +namespace boost
1.41 +{
1.42 +
1.43 +namespace detail
1.44 +{
1.45 +
1.46 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.47 +
1.48 +int const shared_count_id = 0x2C35F101;
1.49 +int const weak_count_id = 0x298C38A4;
1.50 +
1.51 +#endif
1.52 +
1.53 +class weak_count;
1.54 +
1.55 +class shared_count
1.56 +{
1.57 +private:
1.58 +
1.59 + sp_counted_base * pi_;
1.60 +
1.61 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.62 + int id_;
1.63 +#endif
1.64 +
1.65 + friend class weak_count;
1.66 +
1.67 +public:
1.68 +
1.69 + shared_count(): pi_(0) // nothrow
1.70 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.71 + , id_(shared_count_id)
1.72 +#endif
1.73 + {
1.74 + }
1.75 +
1.76 + template<class Y> explicit shared_count( Y * p ): pi_( 0 )
1.77 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.78 + , id_(shared_count_id)
1.79 +#endif
1.80 + {
1.81 +#ifndef BOOST_NO_EXCEPTIONS
1.82 +
1.83 + try
1.84 + {
1.85 + pi_ = new sp_counted_impl_p<Y>( p );
1.86 + }
1.87 + catch(...)
1.88 + {
1.89 + boost::checked_delete( p );
1.90 + throw;
1.91 + }
1.92 +
1.93 +#else
1.94 +
1.95 + pi_ = new sp_counted_impl_p<Y>( p );
1.96 +
1.97 + if( pi_ == 0 )
1.98 + {
1.99 + boost::checked_delete( p );
1.100 + boost::throw_exception( std::bad_alloc() );
1.101 + }
1.102 +
1.103 +#endif
1.104 + }
1.105 +
1.106 + template<class P, class D> shared_count(P p, D d): pi_(0)
1.107 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.108 + , id_(shared_count_id)
1.109 +#endif
1.110 + {
1.111 +#ifndef BOOST_NO_EXCEPTIONS
1.112 +
1.113 + try
1.114 + {
1.115 + pi_ = new sp_counted_impl_pd<P, D>(p, d);
1.116 + }
1.117 + catch(...)
1.118 + {
1.119 + d(p); // delete p
1.120 + throw;
1.121 + }
1.122 +
1.123 +#else
1.124 +
1.125 + pi_ = new sp_counted_impl_pd<P, D>(p, d);
1.126 +
1.127 + if(pi_ == 0)
1.128 + {
1.129 + d(p); // delete p
1.130 + boost::throw_exception(std::bad_alloc());
1.131 + }
1.132 +
1.133 +#endif
1.134 + }
1.135 +
1.136 + template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
1.137 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.138 + , id_(shared_count_id)
1.139 +#endif
1.140 + {
1.141 + typedef sp_counted_impl_pda<P, D, A> impl_type;
1.142 + typedef typename A::template rebind< impl_type >::other A2;
1.143 +
1.144 + A2 a2( a );
1.145 +
1.146 +#ifndef BOOST_NO_EXCEPTIONS
1.147 +
1.148 + try
1.149 + {
1.150 + pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
1.151 + new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
1.152 + }
1.153 + catch(...)
1.154 + {
1.155 + d( p );
1.156 +
1.157 + if( pi_ != 0 )
1.158 + {
1.159 + a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
1.160 + }
1.161 +
1.162 + throw;
1.163 + }
1.164 +
1.165 +#else
1.166 +
1.167 + pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
1.168 +
1.169 + if( pi_ != 0 )
1.170 + {
1.171 + new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
1.172 + }
1.173 + else
1.174 + {
1.175 + d( p );
1.176 + boost::throw_exception( std::bad_alloc() );
1.177 + }
1.178 +
1.179 +#endif
1.180 + }
1.181 +
1.182 +#ifndef BOOST_NO_AUTO_PTR
1.183 +
1.184 + // auto_ptr<Y> is special cased to provide the strong guarantee
1.185 +
1.186 + template<class Y>
1.187 + explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
1.188 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.189 + , id_(shared_count_id)
1.190 +#endif
1.191 + {
1.192 +#ifdef BOOST_NO_EXCEPTIONS
1.193 +
1.194 + if( pi_ == 0 )
1.195 + {
1.196 + boost::throw_exception(std::bad_alloc());
1.197 + }
1.198 +
1.199 +#endif
1.200 +
1.201 + r.release();
1.202 + }
1.203 +
1.204 +#endif
1.205 +
1.206 + ~shared_count() // nothrow
1.207 + {
1.208 + if( pi_ != 0 ) pi_->release();
1.209 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.210 + id_ = 0;
1.211 +#endif
1.212 + }
1.213 +
1.214 + shared_count(shared_count const & r): pi_(r.pi_) // nothrow
1.215 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.216 + , id_(shared_count_id)
1.217 +#endif
1.218 + {
1.219 + if( pi_ != 0 ) pi_->add_ref_copy();
1.220 + }
1.221 +
1.222 + explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
1.223 +
1.224 + shared_count & operator= (shared_count const & r) // nothrow
1.225 + {
1.226 + sp_counted_base * tmp = r.pi_;
1.227 +
1.228 + if( tmp != pi_ )
1.229 + {
1.230 + if( tmp != 0 ) tmp->add_ref_copy();
1.231 + if( pi_ != 0 ) pi_->release();
1.232 + pi_ = tmp;
1.233 + }
1.234 +
1.235 + return *this;
1.236 + }
1.237 +
1.238 + void swap(shared_count & r) // nothrow
1.239 + {
1.240 + sp_counted_base * tmp = r.pi_;
1.241 + r.pi_ = pi_;
1.242 + pi_ = tmp;
1.243 + }
1.244 +
1.245 + long use_count() const // nothrow
1.246 + {
1.247 + return pi_ != 0? pi_->use_count(): 0;
1.248 + }
1.249 +
1.250 + bool unique() const // nothrow
1.251 + {
1.252 + return use_count() == 1;
1.253 + }
1.254 +
1.255 + friend inline bool operator==(shared_count const & a, shared_count const & b)
1.256 + {
1.257 + return a.pi_ == b.pi_;
1.258 + }
1.259 +
1.260 + friend inline bool operator<(shared_count const & a, shared_count const & b)
1.261 + {
1.262 + return std::less<sp_counted_base *>()( a.pi_, b.pi_ );
1.263 + }
1.264 +
1.265 + void * get_deleter(std::type_info const & ti) const
1.266 + {
1.267 + return pi_? pi_->get_deleter( ti ): 0;
1.268 + }
1.269 +};
1.270 +
1.271 +
1.272 +class weak_count
1.273 +{
1.274 +private:
1.275 +
1.276 + sp_counted_base * pi_;
1.277 +
1.278 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.279 + int id_;
1.280 +#endif
1.281 +
1.282 + friend class shared_count;
1.283 +
1.284 +public:
1.285 +
1.286 + weak_count(): pi_(0) // nothrow
1.287 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.288 + , id_(weak_count_id)
1.289 +#endif
1.290 + {
1.291 + }
1.292 +
1.293 + weak_count(shared_count const & r): pi_(r.pi_) // nothrow
1.294 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.295 + , id_(shared_count_id)
1.296 +#endif
1.297 + {
1.298 + if(pi_ != 0) pi_->weak_add_ref();
1.299 + }
1.300 +
1.301 + weak_count(weak_count const & r): pi_(r.pi_) // nothrow
1.302 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.303 + , id_(shared_count_id)
1.304 +#endif
1.305 + {
1.306 + if(pi_ != 0) pi_->weak_add_ref();
1.307 + }
1.308 +
1.309 + ~weak_count() // nothrow
1.310 + {
1.311 + if(pi_ != 0) pi_->weak_release();
1.312 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.313 + id_ = 0;
1.314 +#endif
1.315 + }
1.316 +
1.317 + weak_count & operator= (shared_count const & r) // nothrow
1.318 + {
1.319 + sp_counted_base * tmp = r.pi_;
1.320 + if(tmp != 0) tmp->weak_add_ref();
1.321 + if(pi_ != 0) pi_->weak_release();
1.322 + pi_ = tmp;
1.323 +
1.324 + return *this;
1.325 + }
1.326 +
1.327 + weak_count & operator= (weak_count const & r) // nothrow
1.328 + {
1.329 + sp_counted_base * tmp = r.pi_;
1.330 + if(tmp != 0) tmp->weak_add_ref();
1.331 + if(pi_ != 0) pi_->weak_release();
1.332 + pi_ = tmp;
1.333 +
1.334 + return *this;
1.335 + }
1.336 +
1.337 + void swap(weak_count & r) // nothrow
1.338 + {
1.339 + sp_counted_base * tmp = r.pi_;
1.340 + r.pi_ = pi_;
1.341 + pi_ = tmp;
1.342 + }
1.343 +
1.344 + long use_count() const // nothrow
1.345 + {
1.346 + return pi_ != 0? pi_->use_count(): 0;
1.347 + }
1.348 +
1.349 + friend inline bool operator==(weak_count const & a, weak_count const & b)
1.350 + {
1.351 + return a.pi_ == b.pi_;
1.352 + }
1.353 +
1.354 + friend inline bool operator<(weak_count const & a, weak_count const & b)
1.355 + {
1.356 + return std::less<sp_counted_base *>()(a.pi_, b.pi_);
1.357 + }
1.358 +};
1.359 +
1.360 +inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
1.361 +#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
1.362 + , id_(shared_count_id)
1.363 +#endif
1.364 +{
1.365 + if( pi_ == 0 || !pi_->add_ref_lock() )
1.366 + {
1.367 + boost::throw_exception( boost::bad_weak_ptr() );
1.368 + }
1.369 +}
1.370 +
1.371 +} // namespace detail
1.372 +
1.373 +} // namespace boost
1.374 +
1.375 +#ifdef __BORLANDC__
1.376 +# pragma warn .8027 // Functions containing try are not expanded inline
1.377 +#endif
1.378 +
1.379 +#endif // #ifndef BOOST_DETAIL_SHARED_COUNT_HPP_INCLUDED