shared_ptr.hpp
来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 412 行 · 第 1/2 页
HPP
412 行
////////////////////////////////////////////////////////////////////////////////// This file is the adaptation for Interprocess of boost/shared_ptr.hpp//// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.// (C) Copyright Peter Dimov 2001, 2002, 2003// (C) Copyright Ion Gaztanaga 2006-2008.// Distributed under the Boost Software License, Version 1.0.// (See accompanying file LICENSE_1_0.txt or copy at// http://www.boost.org/LICENSE_1_0.txt)//// See http://www.boost.org/libs/interprocess for documentation.////////////////////////////////////////////////////////////////////////////////#ifndef BOOST_INTERPROCESS_SHARED_PTR_HPP_INCLUDED#define BOOST_INTERPROCESS_SHARED_PTR_HPP_INCLUDED#include <boost/interprocess/detail/config_begin.hpp>#include <boost/interprocess/detail/workaround.hpp>#include <boost/interprocess/detail/utilities.hpp>#include <boost/interprocess/detail/cast_tags.hpp>#include <boost/assert.hpp>#include <boost/interprocess/smart_ptr/detail/shared_count.hpp>#include <boost/interprocess/detail/mpl.hpp>#include <boost/interprocess/detail/move.hpp>#include <boost/interprocess/detail/type_traits.hpp>#include <boost/interprocess/allocators/allocator.hpp>#include <boost/interprocess/smart_ptr/deleter.hpp>#include <boost/static_assert.hpp>#include <algorithm> // for std::swap#include <functional> // for std::less#include <typeinfo> // for std::bad_cast#include <iosfwd> // for std::basic_ostream//!\file//!Describes the smart pointer shared_ptrnamespace boost{namespace interprocess{template<class T, class VoidAllocator, class Deleter> class weak_ptr;template<class T, class VoidAllocator, class Deleter> class enable_shared_from_this;namespace detail{template<class T, class VoidAllocator, class Deleter>inline void sp_enable_shared_from_this (shared_count<T, VoidAllocator, Deleter> const & pn ,enable_shared_from_this<T, VoidAllocator, Deleter> *pe ,T *ptr) { (void)ptr; if(pe != 0){ pe->_internal_weak_this._internal_assign(pn); }}template<class T, class VoidAllocator, class Deleter>inline void sp_enable_shared_from_this(shared_count<T, VoidAllocator, Deleter> const &, ...){}} // namespace detail//!shared_ptr stores a pointer to a dynamically allocated object. //!The object pointed to is guaranteed to be deleted when the last shared_ptr pointing to //!it is destroyed or reset.//!//!shared_ptr is parameterized on //!T (the type of the object pointed to), VoidAllocator (the void allocator to be used//!to allocate the auxiliary data) and Deleter (the deleter whose //!operator() will be used to delete the object.//!//!The internal pointer will be of the same pointer type as typename //!VoidAllocator::pointer type (that is, if typename VoidAllocator::pointer is //!offset_ptr<void>, the internal pointer will be offset_ptr<T>).//!//!Because the implementation uses reference counting, cycles of shared_ptr//!instances will not be reclaimed. For example, if main() holds a//!shared_ptr to A, which directly or indirectly holds a shared_ptr back//!to A, A's use count will be 2. Destruction of the original shared_ptr//!will leave A dangling with a use count of 1.//!Use weak_ptr to "break cycles."template<class T, class VoidAllocator, class Deleter>class shared_ptr{ /// @cond private: typedef shared_ptr<T, VoidAllocator, Deleter> this_type; /// @endcond public: typedef T element_type; typedef T value_type; typedef typename detail::pointer_to_other <typename VoidAllocator::pointer, T>::type pointer; typedef typename detail::add_reference <value_type>::type reference; typedef typename detail::add_reference <const value_type>::type const_reference; typedef typename detail::pointer_to_other <typename VoidAllocator::pointer, const Deleter>::type const_deleter_pointer; typedef typename detail::pointer_to_other <typename VoidAllocator::pointer, const VoidAllocator>::type const_allocator_pointer; public: //!Constructs an empty shared_ptr. //!Use_count() == 0 && get()== 0. shared_ptr() : m_pn() // never throws {} //!Constructs a shared_ptr that owns the pointer p. Auxiliary data will be allocated //!with a copy of a and the object will be deleted with a copy of d. //!Requirements: Deleter and A's copy constructor must not throw. explicit shared_ptr(const pointer&p, const VoidAllocator &a = VoidAllocator(), const Deleter &d = Deleter()) : m_pn(p, a, d) { //Check that the pointer passed is of the same type that //the pointer the allocator defines or it's a raw pointer typedef typename detail::pointer_to_other<pointer, T>::type ParameterPointer; BOOST_STATIC_ASSERT((detail::is_same<pointer, ParameterPointer>::value) || (detail::is_pointer<pointer>::value)); detail::sp_enable_shared_from_this<T, VoidAllocator, Deleter>( m_pn, detail::get_pointer(p), detail::get_pointer(p) ); } //!Constructs a shared_ptr that shares ownership with r and stores p. //!Postconditions: get() == p && use_count() == r.use_count(). //!Throws: nothing. shared_ptr(const shared_ptr &other, const pointer &p) : m_pn(other.m_pn, p) {} //!If r is empty, constructs an empty shared_ptr. Otherwise, constructs //!a shared_ptr that shares ownership with r. Never throws. template<class Y> shared_ptr(shared_ptr<Y, VoidAllocator, Deleter> const & r) : m_pn(r.m_pn) // never throws {} //!Constructs a shared_ptr that shares ownership with r and stores //!a copy of the pointer stored in r. template<class Y> explicit shared_ptr(weak_ptr<Y, VoidAllocator, Deleter> const & r) : m_pn(r.m_pn) // may throw {} //!Move-Constructs a shared_ptr that takes ownership of other resource and //!other is put in default-constructed state. //!Throws: nothing. #ifndef BOOST_INTERPROCESS_RVALUE_REFERENCE explicit shared_ptr(detail::moved_object<shared_ptr> other) : m_pn() { this->swap(other.get()); } #else explicit shared_ptr(shared_ptr &&other) : m_pn() { this->swap(other); } #endif /// @cond template<class Y> shared_ptr(shared_ptr<Y, VoidAllocator, Deleter> const & r, detail::static_cast_tag) : m_pn( pointer(static_cast<T*>(detail::get_pointer(r.m_pn.get_pointer()))) , r.m_pn) {} template<class Y> shared_ptr(shared_ptr<Y, VoidAllocator, Deleter> const & r, detail::const_cast_tag) : m_pn( pointer(const_cast<T*>(detail::get_pointer(r.m_pn.get_pointer()))) , r.m_pn) {} template<class Y> shared_ptr(shared_ptr<Y, VoidAllocator, Deleter> const & r, detail::dynamic_cast_tag) : m_pn( pointer(dynamic_cast<T*>(detail::get_pointer(r.m_pn.get_pointer()))) , r.m_pn) { if(!m_pn.get_pointer()){ // need to allocate new counter -- the cast failed m_pn = detail::shared_count<T, VoidAllocator, Deleter>(); } } /// @endcond //!Equivalent to shared_ptr(r).swap(*this). //!Never throws template<class Y> shared_ptr & operator=(shared_ptr<Y, VoidAllocator, Deleter> const & r) { m_pn = r.m_pn; // shared_count::op= doesn't throw return *this; } //!Move-assignment. Equivalent to shared_ptr(other).swap(*this). //!Never throws #ifndef BOOST_INTERPROCESS_RVALUE_REFERENCE shared_ptr & operator=(detail::moved_object<shared_ptr> other) // never throws { this_type(other).swap(*this); return *this; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?