📄 reference.h
字号:
//// Copyright (c) 2003 by Istv醤 V醨adi//// This file is part of dxr3Player, a DVD player written specifically // for the DXR3 (aka Hollywood+) decoder card.// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#ifndef DXR3PLAYER_REFERENCE_H#define DXR3PLAYER_REFERENCE_H//------------------------------------------------------------------------------#include <cassert>//------------------------------------------------------------------------------/** * Reference. It handles the reference counter in objects. It only * calls the functions maintaining the reference counter, but does not * delete the object, when all references have been removed. The * object must do so itself, if that is the desired way of working. * * Template parameters: * * <dl> * <dt>R * <dd>the referenced object. It must have the following functions: * <dl> * <dt>addReference * <dd>increase the reference counter * <dt>removeReference * <dd>decrease the reference counter * </dl> * </dl> */template <class R>class Reference{private: /** * The referenced object. */ R* object;public: /** * Construct a reference */ Reference(R* object = 0); /** * Copy a reference. */ Reference(const Reference<R>& other); /** * Copy a reference. */ template <typename R1> Reference(const Reference<R1>& other); /** * Destroy a reference. */ ~Reference(); /** * Check if the reference is valid or not. */ bool isValid() const; /** * Get the pointer. */ R* get() const; /** * Assign a reference. */ Reference<R>& operator=(R* o); /** * Assign a reference. */ Reference<R>& operator=(const Reference<R>& other); /** * Access the referenced object. */ R* operator->() const;private: /** * Add a reference to the object, if not 0. */ void addReference(); /** * Remove a reference from the object, if not 0. This does not * delete the object, if all references are removed, the objet * should do it (or do anything it has to when all references are * removed). */ void removeReference();};//------------------------------------------------------------------------------// Template definitions//------------------------------------------------------------------------------template <class R>inline void Reference<R>::addReference(){ if (object!=0) { object->addReference(); }}//------------------------------------------------------------------------------template <class R>inline void Reference<R>::removeReference(){ if (object!=0) { object->removeReference(); object = 0; }}//------------------------------------------------------------------------------template <class R>inline R* Reference<R>::get() const{ return object;}//------------------------------------------------------------------------------template <class R>inline Reference<R>::Reference(R* object) : object(object){ addReference();}//------------------------------------------------------------------------------template <class R>inline Reference<R>::Reference(const Reference<R>& other) : object(other.object){ addReference();}//------------------------------------------------------------------------------template <class R>template <typename R1>inline Reference<R>::Reference(const Reference<R1>& other) : object(other.get()){ addReference();}//------------------------------------------------------------------------------template <class R>inline Reference<R>::~Reference(){ removeReference();}//------------------------------------------------------------------------------template <class R>inline bool Reference<R>::isValid() const{ return object!=0;}//------------------------------------------------------------------------------template <class R>inline Reference<R>& Reference<R>::operator=(R* o){ removeReference(); object = o; addReference(); return *this;}//------------------------------------------------------------------------------template <class R>inline Reference<R>& Reference<R>::operator=(const Reference<R>& other){ removeReference(); object = other.object; addReference(); return *this;}//------------------------------------------------------------------------------template <class R>inline R* Reference<R>::operator->() const{ assert(object!=0); return object;}//------------------------------------------------------------------------------#endif // DXR3PLAYER_REFERENCE_H// Local variables:// mode: c++// End:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -