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 <stdlib.h> 00029 #include <stdio.h> 00030 00031 00032 template<class T> 00033 class P { 00034 private: 00035 T* p; 00036 public: 00037 P() : p(0) { } 00038 P(T* rawptr) : p(rawptr) { } 00039 P(const P& ptr) : p((T*)ptr) { } 00040 00041 template<class T2> 00042 P(const P<T2>& ptr) : p((T*)ptr) { } 00043 00044 ~P() { } 00045 00046 operator T*() const { return p; } 00047 T& operator*() const { 00048 if (p == 0) { 00049 //throw std::runtime_error("Null dereference in P<?>::operator*() const "); 00050 fprintf(stderr, "Null dereference in P<?>::operator*() const \n "); 00051 exit(1); 00052 } 00053 return *p; 00054 } 00055 T* operator->() const { 00056 if (p == 0) { 00057 fprintf(stderr, "Null dereference in P<?>::operator->() const \n "); 00058 //throw std::runtime_error("Null dereference in P<?>::operator->() const "); 00059 //std::cerr << "Null dereference in P<?>::operator->() const " << std::endl; 00060 exit(1); 00061 } 00062 return p; 00063 } 00064 T* pointee() const { return p;} 00065 P& operator=(T* p_) { p = p_; return *this;} 00066 00067 P& operator=(const P<T>& p_) { return operator=((T *) p_); } 00068 template<class T2> P& cast(T2* p_) { return operator=(dynamic_cast<T*>(p_)); } 00069 template<class T2> P& cast(const P<T2>& p_) { return operator=(dynamic_cast<T*>(p_)); } 00070 }; 00071 00072 00073 #endif