/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Fabian 'x3n' Landau * Co-authors: * ... * */ // Inspired by boost::intrusive_ptr by Peter Dimov #ifndef _SmartPtr_H__ #define _SmartPtr_H__ #include "CorePrereqs.h" #include #include #include "OrxonoxClass.h" namespace orxonox { template class SmartPtr { public: inline SmartPtr() : pointer_(0), base_(0) { } inline SmartPtr(int) : pointer_(0), base_(0) { } inline SmartPtr(T* pointer, bool bAddRef = true) : pointer_(pointer), base_(pointer) { if (this->base_ && bAddRef) this->base_->incrementReferenceCount(); } inline SmartPtr(const SmartPtr& other) : pointer_(other.pointer_), base_(other.base_) { if (this->base_) this->base_->incrementReferenceCount(); } template inline SmartPtr(const SmartPtr& other) : pointer_(other.get()), base_(other.base_) { if (this->base_) this->base_->incrementReferenceCount(); } inline ~SmartPtr() { if (this->base_) this->base_->decrementReferenceCount(); } inline const SmartPtr& operator=(int) { SmartPtr(0).swap(*this); return *this; } inline const SmartPtr& operator=(T* pointer) { SmartPtr(pointer).swap(*this); return *this; } inline const SmartPtr& operator=(const SmartPtr& other) { SmartPtr(other).swap(*this); return *this; } template inline const SmartPtr& operator=(const SmartPtr& other) { SmartPtr(other).swap(*this); return *this; } inline T* get() const { return this->pointer_; } inline operator T*() const { return this->pointer_; } inline T* operator->() const { assert(this->pointer_ != 0); return this->pointer_; } inline T& operator*() const { assert(this->pointer_ != 0); return *this->pointer_; } inline bool operator!() const { return (this->pointer_ == 0); } inline void swap(SmartPtr& other) { { T* temp = this->pointer_; this->pointer_ = other.pointer_; other.pointer_ = temp; } { OrxonoxClass* temp = this->base_; this->base_ = other.base_; other.base_ = temp; } } inline void reset() { SmartPtr().swap(*this); } private: T* pointer_; OrxonoxClass* base_; }; template inline bool operator==(const SmartPtr& a, const SmartPtr& b) { return (a.get() == b.get()); } template inline bool operator!=(const SmartPtr& a, const SmartPtr& b) { return (a.get() != b.get()); } template inline bool operator<(const SmartPtr& a, const SmartPtr& b) { return std::less()(a.get(), b.get()); } template void swap(SmartPtr& a, SmartPtr& b) { a.swap(b); } template SmartPtr static_pointer_cast(const SmartPtr& p) { return static_cast(p.get()); } template SmartPtr const_pointer_cast(const SmartPtr& p) { return const_cast(p.get()); } template SmartPtr dynamic_pointer_cast(const SmartPtr& p) { return dynamic_cast(p.get()); } template std::ostream& operator<<(std::ostream& os, const SmartPtr& p) { os << p.get(); return os; } } #endif /* _SmartPtr_H__ */