/* Copyright (C) 1997,1998,1999,2000,2001 Franz Josef Och (RWTH Aachen - Lehrstuhl fuer Informatik VI) This file is part of GIZA++ ( extension of GIZA ). 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 HEADER_Pointer_DEFINED #define HEADER_Pointer_DEFINED #include #include template class SmartPointer { protected: T*p; public: SmartPointer(T*_p=0) : p(_p) {} inline T&operator*() const { return *p; } inline T*operator->() const { return p; } inline operator bool() const { return p!=0; } inline T*ptr() const { return p; } }; template inline ostream &operator<<(ostream&out,const SmartPointer&s) { if( s.ptr() )return out << *s; else return out <<"nullpointer"; } template class SmartPointerConst { protected: const T*p; public: SmartPointerConst(const T*_p=0) : p(_p) {} inline const T&operator*() const { return *p; } inline const T*operator->() const { return p; } inline operator bool() const { return p!=0; } inline const T*ptr() const { return p; } }; template inline ostream &operator<<(ostream&out,const SmartPointerConst&s) { if( s.ptr() )return out << *s; else return out <<"nullpointer"; } template class UP : public SmartPointer { public: UP(T*_p=0) : SmartPointer(_p) {} }; template inline bool operator==(const UP&s1,const UP&s2) { return s1.ptr()==s2.ptr(); } template inline bool operator<(const UP&s1,const UP&s2) { return s1.ptr() < s2.ptr(); } template inline int Hash(const UP &wp) { if(wp.ptr())return Hash(*wp); else return 0; } template class UPConst : public SmartPointerConst { public: UPConst(const T*_p=0) : SmartPointerConst(_p) {} }; template inline bool operator==(const UPConst&s1,const UPConst&s2) { return s1.ptr()==s2.ptr(); } template inline bool operator<(const UPConst&s1,const UPConst&s2) { return s1.ptr() inline int Hash(const UPConst &wp) { if(wp.ptr())return Hash(*wp); else return 0; } template class MP : public SmartPointer { public: MP(T*_p=0) : SmartPointer(_p) {} }; template inline bool operator==(const MP&s1,const MP&s2) { assert(s1); assert(s2); return *s1==*s2; } template inline bool operator<(const MP&s1,const MP&s2) { assert(s1); assert(s2); return *s1 < *s2; } template inline int Hash(const MP &wp) { if(wp.ptr())return Hash(*wp); else return 0; } template class MPConst : public SmartPointerConst { public: MPConst(const T*_p=0) : SmartPointerConst(_p) {} }; template inline bool operator==(const MPConst&s1,const MPConst&s2) { assert(s1); assert(s2); return *s1== *s2; } template inline bool operator<(const MPConst&s1,const MPConst&s2) { assert(s1); assert(s2); return *s1 < *s2; } template inline int Hash(const MPConst &wp) { if(wp.ptr())return Hash(*wp); else return 0; } template class DELP : public SmartPointer { private: DELP(const DELP&x); public: const DELP&operator=(DELP&x) { delete this->p; this->p=x.p; x.p=0; return *this; } ~DELP() { delete this->p; this->p=0; } DELP(T*_p=0) : SmartPointer(_p) {} void set(T*_p) { delete this->p; this->p=_p; } friend bool operator==(const DELP&s1,const DELP&s2) { return *(s1.p)== *(s2.p); } friend bool operator<(const DELP&s1,const DELP&s2) { return *(s1.p) < *(s2.p); } friend inline int Hash(const DELP &wp) { if(wp.p) return Hash(*wp.p); else return 0; } }; #endif