00001 /************************************************************************ 00002 ************************************************************************ 00003 FAUST compiler 00004 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale 00005 --------------------------------------------------------------------- 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 ************************************************************************ 00020 ************************************************************************/ 00021 00022 00023 00024 #ifndef _SMARTPOINTER_H 00025 #define _SMARTPOINTER_H 00026 00027 //#include <iostream> 00028 #include <cstdlib> 00029 #include <cstdio> 00030 00031 00032 00033 template<class T> 00034 class P { 00035 private: 00036 T* p; 00037 public: 00038 P() : p(0) { } 00039 P(T* rawptr) : p(rawptr) { } 00040 P(const P& ptr) : p((T*)ptr) { } 00041 00042 template<class T2> 00043 P(const P<T2>& ptr) : p((T*)ptr) { } 00044 00045 ~P() { } 00046 00047 operator T*() const { return p; } 00048 T& operator*() const { 00049 if (p == 0) { 00050 //throw std::runtime_error("Null dereference in P<?>::operator*() const "); 00051 fprintf(stderr, "Null dereference in P<?>::operator*() const \n "); 00052 exit(1); 00053 } 00054 return *p; 00055 } 00056 T* operator->() const { 00057 if (p == 0) { 00058 fprintf(stderr, "Null dereference in P<?>::operator->() const \n "); 00059 //throw std::runtime_error("Null dereference in P<?>::operator->() const "); 00060 //std::cerr << "Null dereference in P<?>::operator->() const " << std::endl; 00061 exit(1); 00062 } 00063 return p; 00064 } 00065 T* pointee() const { return p;} 00066 P& operator=(T* p_) { p = p_; return *this;} 00067 00068 P& operator=(const P<T>& p_) { return operator=((T *) p_); } 00069 template<class T2> P& cast(T2* p_) { return operator=(dynamic_cast<T*>(p_)); } 00070 template<class T2> P& cast(const P<T2>& p_) { return operator=(dynamic_cast<T*>(p_)); } 00071 }; 00072 00073 00074 #endif