00001 /* 00002 00003 ************************************************************************* 00004 00005 ArmageTron -- Just another Tron Lightcycle Game in 3D. 00006 Copyright (C) 2000 Manuel Moos (manuel@moosnet.de) 00007 00008 ************************************************************************** 00009 00010 This program is free software; you can redistribute it and/or 00011 modify it under the terms of the GNU General Public License 00012 as published by the Free Software Foundation; either version 2 00013 of the License, or (at your option) any later version. 00014 00015 This program is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU General Public License for more details. 00019 00020 You should have received a copy of the GNU General Public License 00021 along with this program; if not, write to the Free Software 00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00023 00024 *************************************************************************** 00025 00026 */ 00027 00028 #ifndef ArmageTron_OBSERVER_H 00029 #define ArmageTron_OBSERVER_H 00030 00031 #include "tSafePTR.h" 00032 00033 class nNetObject; 00034 00035 // netobject observer 00036 class nObserver: public tReferencable< nObserver > 00037 { 00038 friend class tReferencable< nObserver >; 00039 public: 00040 const nNetObject* GetNetObject() const; // returns the observed object 00041 00042 #ifdef DEBUG 00043 static void SetLastChecked( const nObserver* checked ); 00044 static bool WasChecked( const nObserver* checked ); 00045 #endif 00046 00047 private: 00048 const nNetObject* object_; // the observed object 00049 00050 void SetObject( const nNetObject *); // sets the observed object 00051 00052 nObserver(); 00053 ~nObserver(); 00054 00055 friend class nNetObject; 00056 }; 00057 00058 // observer pointer 00059 template< class T > 00060 class nObserverPtr 00061 { 00062 public: 00063 operator bool() const 00064 { 00065 #ifdef DEBUG 00066 nObserver::SetLastChecked( this->observer_ ); 00067 #endif 00068 00069 return ( bool( this->observer_ ) && this->observer_->GetNetObject() ); 00070 } 00071 00072 bool operator!() const 00073 { 00074 return !operator bool(); 00075 } 00076 00077 const T* GetPointer() const 00078 { 00079 if ( this->observer_ ) 00080 return dynamic_cast< const T* >( observer_->GetNetObject() ); 00081 else 00082 return NULL; 00083 } 00084 00085 operator const T* () const 00086 { 00087 return this->GetPointer(); 00088 } 00089 00090 const T* operator->() const 00091 { 00092 const T* t = *this; 00093 00094 #ifdef DEBUG 00095 // tASSERT( nObserver::WasChecked( this->observer_ ) ); 00096 #endif 00097 00098 tASSERT(t); 00099 00100 return t; 00101 } 00102 00103 const T* operator*() const 00104 { 00105 const T* t = *this; 00106 00107 #ifdef DEBUG 00108 // tASSERT( nObserver::WasChecked( this->observer_ ) ); 00109 #endif 00110 00111 tASSERT(t); 00112 00113 return t; 00114 } 00115 00116 void SetObject ( const T* t) 00117 { 00118 if ( t == NULL ) 00119 observer_ = NULL; 00120 else 00121 observer_ = &t->GetObserver(); 00122 } 00123 00124 nObserverPtr& operator = ( const T* t) 00125 { 00126 SetObject( t ); 00127 00128 return *this; 00129 } 00130 00131 nObserverPtr( const T* t) 00132 { 00133 SetObject( t ); 00134 } 00135 00136 nObserverPtr() 00137 { 00138 } 00139 private: 00140 tCONTROLLED_PTR( nObserver ) observer_; 00141 }; 00142 00143 #endif