x
Yes
No
Do you want to visit DriveHQ English website?
首页
产品服务
价格
免费试用
下载客户端
关于我们
云文件服务
|
云备份服务
|
FTP服务
|
企业邮箱服务
|
网站托管
|
客户端软件
云文件服务
云备份服务
FTP服务
企业级邮箱服务
网站托管
客户端软件
weak_ptr.hpp - Hosted on DriveHQ Cloud IT Platform
返回上层目录
上传
下载
共享
发布
新建文件夹
新建文件
复制
剪切
删除
粘贴
评论
升级服务
路径: \\game3dprogramming\materials\GameFactory\GameFactoryDemo\references\boost_1_35_0\boost\interprocess\smart_ptr\weak_ptr.hpp
旋转
特效
属性
历史版本
////////////////////////////////////////////////////////////////////////////// // // This file is the adaptation for Interprocess of boost/weak_ptr.hpp // // (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_WEAK_PTR_HPP_INCLUDED #define BOOST_INTERPROCESS_WEAK_PTR_HPP_INCLUDED #include
#include
#include
#include
#include
#include
//!\file //!Describes the smart pointer weak_ptr. namespace boost{ namespace interprocess{ //!The weak_ptr class template stores a "weak reference" to an object //!that's already managed by a shared_ptr. To access the object, a weak_ptr //!can be converted to a shared_ptr using the shared_ptr constructor or the //!member function lock. When the last shared_ptr to the object goes away //!and the object is deleted, the attempt to obtain a shared_ptr from the //!weak_ptr instances that refer to the deleted object will fail: the constructor //!will throw an exception of type bad_weak_ptr, and weak_ptr::lock will //!return an empty shared_ptr. //! //!Every weak_ptr meets the CopyConstructible and Assignable requirements //!of the C++ Standard Library, and so can be used in standard library containers. //!Comparison operators are supplied so that weak_ptr works with the standard //!library's associative containers. //! //!weak_ptr operations never throw exceptions. //! //!The class template is parameterized on T, the type of the object pointed to. template
class weak_ptr { /// @cond private: // Borland 5.5.1 specific workarounds typedef weak_ptr
this_type; typedef typename detail::pointer_to_other
::type pointer; typedef typename detail::add_reference
::type reference; typedef typename detail::add_reference
::type const_reference; /// @endcond public: typedef T element_type; typedef T value_type; //!Effects: Constructs an empty weak_ptr. //!Postconditions: use_count() == 0. weak_ptr() : m_pn() // never throws {} // generated copy constructor, assignment, destructor are fine // // The "obvious" converting constructor implementation: // // template
// weak_ptr(weak_ptr
const & r): m_px(r.m_px), m_pn(r.m_pn) // never throws // { // } // // has a serious problem. // // r.m_px may already have been invalidated. The m_px(r.m_px) // conversion may require access to *r.m_px (virtual inheritance). // // It is not possible to avoid spurious access violations since // in multithreaded programs r.m_px may be invalidated at any point. //!Effects: If r is empty, constructs an empty weak_ptr; otherwise, //!constructs a weak_ptr that shares ownership with r as if by storing a //!copy of the pointer stored in r. //! //!Postconditions: use_count() == r.use_count(). //! //!Throws: nothing. template
weak_ptr(weak_ptr
const & r) : m_pn(r.m_pn) // never throws { //Construct a temporary shared_ptr so that nobody //can destroy the value while constructing this const shared_ptr
&ref = r.lock(); m_pn.set_pointer(ref.get()); } //!Effects: If r is empty, constructs an empty weak_ptr; otherwise, //!constructs a weak_ptr that shares ownership with r as if by storing a //!copy of the pointer stored in r. //! //!Postconditions: use_count() == r.use_count(). //! //!Throws: nothing. template
weak_ptr(shared_ptr
const & r) : m_pn(r.m_pn) // never throws {} //!Effects: Equivalent to weak_ptr(r).swap(*this). //! //!Throws: nothing. //! //!Notes: The implementation is free to meet the effects (and the //!implied guarantees) via different means, without creating a temporary. template
weak_ptr & operator=(weak_ptr
const & r) // never throws { //Construct a temporary shared_ptr so that nobody //can destroy the value while constructing this const shared_ptr
&ref = r.lock(); m_pn = r.m_pn; m_pn.set_pointer(ref.get()); return *this; } //!Effects: Equivalent to weak_ptr(r).swap(*this). //! //!Throws: nothing. //! //!Notes: The implementation is free to meet the effects (and the //!implied guarantees) via different means, without creating a temporary. template
weak_ptr & operator=(shared_ptr
const & r) // never throws { m_pn = r.m_pn; return *this; } //!Returns: expired()? shared_ptr
(): shared_ptr
(*this). //! //!Throws: nothing. shared_ptr
lock() const // never throws { // optimization: avoid throw overhead if(expired()){ return shared_ptr
(); } BOOST_TRY{ return shared_ptr
(*this); } BOOST_CATCH(bad_weak_ptr const &){ // Q: how can we get here? // A: another thread may have invalidated r after the use_count test above. return shared_ptr
(); } BOOST_CATCH_END } //!Returns: 0 if *this is empty; otherwise, the number of shared_ptr objects //!that share ownership with *this. //! //!Throws: nothing. //! //!Notes: use_count() is not necessarily efficient. Use only for debugging and //!testing purposes, not for production code. long use_count() const // never throws { return m_pn.use_count(); } //!Returns: Returns: use_count() == 0. //! //!Throws: nothing. //! //!Notes: expired() may be faster than use_count(). bool expired() const // never throws { return m_pn.use_count() == 0; } //!Effects: Equivalent to: //!weak_ptr().swap(*this). void reset() // never throws in 1.30+ { this_type().swap(*this); } //!Effects: Exchanges the contents of the two //!smart pointers. //! //!Throws: nothing. void swap(this_type & other) // never throws { detail::do_swap(m_pn, other.m_pn); } /// @cond template
bool _internal_less(weak_ptr
const & rhs) const { return m_pn < rhs.m_pn; } template
void _internal_assign(const detail::shared_count
& pn2) { m_pn = pn2; } private: template
friend class shared_ptr; template
friend class weak_ptr; detail::weak_count
m_pn; // reference counter /// @endcond }; // weak_ptr template
inline bool operator<(weak_ptr
const & a, weak_ptr
const & b) { return a._internal_less(b); } template
inline void swap(weak_ptr
& a, weak_ptr
& b) { a.swap(b); } //!Returns the type of a weak pointer //!of type T with the allocator boost::interprocess::allocator allocator //!and boost::interprocess::deleter deleter //!that can be constructed in the given managed segment type. template
struct managed_weak_ptr { typedef weak_ptr < T , typename ManagedMemory::template allocator
::type , typename ManagedMemory::template deleter
::type > type; }; //!Returns an instance of the a weak pointer constructed //!with the default allocator and deleter from a pointer //!of type T that has been allocated in the passed managed segment template
inline typename managed_weak_ptr
::type make_managed_weak_ptr(T *constructed_object, ManagedMemory &managed_memory) { return typename managed_weak_ptr
::type ( constructed_object , managed_memory.template get_allocator
() , managed_memory.template get_deleter
() ); } } // namespace interprocess } // namespace boost #include
#endif // #ifndef BOOST_INTERPROCESS_WEAK_PTR_HPP_INCLUDED
weak_ptr.hpp
网页地址
文件地址
上一页
7/7 下一页
下载
( 8 KB )
Comments
Total ratings:
0
Average rating:
无评论
of 10
Would you like to comment?
Join now
, or
Logon
if you are already a member.