00001 // Package : omniIFR 00002 // Creche.h Created : 2004/02/22 00003 // Author : Alex Tingle 00004 // 00005 // Copyright (C) 2004 Alex Tingle. 00006 // 00007 // This file is part of the omniIFR application. 00008 // 00009 // omniIFR is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU Lesser General Public 00011 // License as published by the Free Software Foundation; either 00012 // version 2.1 of the License, or (at your option) any later version. 00013 // 00014 // omniIFR is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 // Lesser General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU Lesser General Public 00020 // License along with this library; if not, write to the Free Software 00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 // 00023 00024 #ifndef OMNIIFR__CRECHE_H 00025 #define OMNIIFR__CRECHE_H 00026 00027 namespace Omniifr { 00028 00034 template<class T> 00035 class Creche 00036 { 00037 public: 00038 Creche(T* ptr):_ptr(ptr){assert(_ptr);} 00039 ~Creche() 00040 { 00041 if(_ptr) 00042 { 00043 _ptr->uncheckedDestroy(); 00044 _ptr->_remove_ref(); // Kill the constructor's reference to this. 00045 _ptr=NULL; 00046 } 00047 } 00048 T* get() const {return _ptr;} 00049 T* operator->() const {return _ptr;} 00054 T* release(const char* oidStr =NULL) 00055 { 00056 assert(_ptr); 00057 T* result=_ptr; 00058 if(oidStr) 00059 _ptr->activateObjectWithId(oidStr); 00060 else 00061 _ptr->activateObject(); 00062 _ptr=NULL; 00063 return result; 00064 } 00065 private: 00066 Creche(const Creche&); 00067 T* _ptr; 00068 }; 00069 00070 } // end namespace Omniifr 00071 00072 #endif // OMNIIFR__CRECHE_H