sp_counted_impl.hpp

Go to the documentation of this file.
00001 #ifndef BOOST_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
00002 #define BOOST_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
00003 
00004 // MS compatible compilers support #pragma once
00005 
00006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
00007 # pragma once
00008 #endif
00009 
00010 //
00011 //  detail/sp_counted_impl.hpp
00012 //
00013 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
00014 //  Copyright 2004-2005 Peter Dimov
00015 //
00016 // Distributed under the Boost Software License, Version 1.0. (See
00017 // accompanying file LICENSE_1_0.txt or copy at
00018 // http://www.boost.org/LICENSE_1_0.txt)
00019 //
00020 
00021 #include <sysc/packages/boost/config.hpp>
00022 
00023 #if defined(BOOST_SP_USE_STD_ALLOCATOR) && defined(BOOST_SP_USE_QUICK_ALLOCATOR)
00024 # error BOOST_SP_USE_STD_ALLOCATOR and BOOST_SP_USE_QUICK_ALLOCATOR are incompatible.
00025 #endif
00026 
00027 #include <sysc/packages/boost/checked_delete.hpp>
00028 #include <sysc/packages/boost/detail/sp_counted_base.hpp>
00029 
00030 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
00031 #include <sysc/packages/boost/detail/quick_allocator.hpp>
00032 #endif
00033 
00034 #include <memory>           // std::allocator
00035 #include <typeinfo>         // std::type_info in get_deleter
00036 #include <cstddef>          // std::size_t
00037 
00038 namespace boost
00039 {
00040 
00041 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
00042 
00043 void sp_scalar_constructor_hook( void * px, std::size_t size, void * pn );
00044 void sp_scalar_destructor_hook( void * px, std::size_t size, void * pn );
00045 
00046 #endif
00047 
00048 namespace detail
00049 {
00050 
00051 template<class X> class sp_counted_impl_p: public sp_counted_base
00052 {
00053 private:
00054 
00055     X * px_;
00056 
00057     sp_counted_impl_p( sp_counted_impl_p const & );
00058     sp_counted_impl_p & operator= ( sp_counted_impl_p const & );
00059 
00060     typedef sp_counted_impl_p<X> this_type;
00061 
00062 public:
00063 
00064     explicit sp_counted_impl_p( X * px ): px_( px )
00065     {
00066 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
00067         boost::sp_scalar_constructor_hook( px, sizeof(X), this );
00068 #endif
00069     }
00070 
00071     virtual void dispose() // nothrow
00072     {
00073 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
00074         boost::sp_scalar_destructor_hook( px_, sizeof(X), this );
00075 #endif
00076         boost::checked_delete( px_ );
00077     }
00078 
00079     virtual void * get_deleter( std::type_info const & )
00080     {
00081         return 0;
00082     }
00083 
00084 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
00085 
00086     void * operator new( std::size_t )
00087     {
00088         return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
00089     }
00090 
00091     void operator delete( void * p )
00092     {
00093         std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
00094     }
00095 
00096 #endif
00097 
00098 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
00099 
00100     void * operator new( std::size_t )
00101     {
00102         return quick_allocator<this_type>::alloc();
00103     }
00104 
00105     void operator delete( void * p )
00106     {
00107         quick_allocator<this_type>::dealloc( p );
00108     }
00109 
00110 #endif
00111 };
00112 
00113 //
00114 // Borland's Codeguard trips up over the -Vx- option here:
00115 //
00116 #ifdef __CODEGUARD__
00117 # pragma option push -Vx-
00118 #endif
00119 
00120 template<class P, class D> class sp_counted_impl_pd: public sp_counted_base
00121 {
00122 private:
00123 
00124     P ptr; // copy constructor must not throw
00125     D del; // copy constructor must not throw
00126 
00127     sp_counted_impl_pd( sp_counted_impl_pd const & );
00128     sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
00129 
00130     typedef sp_counted_impl_pd<P, D> this_type;
00131 
00132 public:
00133 
00134     // pre: d(p) must not throw
00135 
00136     sp_counted_impl_pd( P p, D d ): ptr(p), del(d)
00137     {
00138     }
00139 
00140     virtual void dispose() // nothrow
00141     {
00142         del( ptr );
00143     }
00144 
00145     virtual void * get_deleter( std::type_info const & ti )
00146     {
00147         return ti == typeid(D)? &del: 0;
00148     }
00149 
00150 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
00151 
00152     void * operator new( std::size_t )
00153     {
00154         return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
00155     }
00156 
00157     void operator delete( void * p )
00158     {
00159         std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
00160     }
00161 
00162 #endif
00163 
00164 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
00165 
00166     void * operator new( std::size_t )
00167     {
00168         return quick_allocator<this_type>::alloc();
00169     }
00170 
00171     void operator delete( void * p )
00172     {
00173         quick_allocator<this_type>::dealloc( p );
00174     }
00175 
00176 #endif
00177 };
00178 
00179 #ifdef __CODEGUARD__
00180 # pragma option pop
00181 #endif
00182 
00183 } // namespace detail
00184 
00185 } // namespace boost
00186 
00187 #endif  // #ifndef BOOST_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED

Generated on Wed Jan 21 15:32:10 2009 for SystemC by  doxygen 1.5.5