/* * Copyright (c) 2010-2012 OTClient * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #ifndef STDEXT_SHARED_OBJECT_H #define STDEXT_SHARED_OBJECT_H #include #include namespace stdext { class shared_object { public: shared_object() : m_refs(0) { } virtual ~shared_object() { } void add_ref() { ++m_refs; assert(m_refs != 0xffffffff); } void dec_ref() { if(--m_refs == 0) boost::checked_delete(this); } bool is_unique_ref() { return m_refs == 1; } unsigned long ref_count() { return m_refs; } template boost::intrusive_ptr self_cast() { return boost::intrusive_ptr(static_cast(this)); } template boost::intrusive_ptr dynamic_self_cast() { return boost::intrusive_ptr(dynamic_cast(this)); } private: unsigned int m_refs; }; template boost::intrusive_ptr make_shared_object(Args... args) { return boost::intrusive_ptr(new T(args...)); } } namespace std { template struct hash> : public __hash_base> { size_t operator()(const boost::intrusive_ptr& p) const noexcept { return std::hash()(p.get()); } }; } template struct remove_const_ref { typedef typename std::remove_const::type>::type type; }; template void intrusive_ptr_add_ref(T* p) { (static_cast(p))->add_ref(); } template void intrusive_ptr_release(T* p) { (static_cast(p))->dec_ref(); } #endif