00001 /* 00002 * This file is part of libcxxsupport. 00003 * 00004 * libcxxsupport is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * libcxxsupport is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with libcxxsupport; if not, write to the Free Software 00016 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00017 */ 00018 00019 /* 00020 * libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik 00021 * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt 00022 * (DLR). 00023 */ 00024 00025 /*! \file safe_ptr.h 00026 * Pointer wrapper for better exception safety and leakage prevention 00027 * 00028 * Copyright (C) 2005, 2008 Max-Planck-Society 00029 * \author Martin Reinecke 00030 */ 00031 00032 #ifndef PLANCK_SAFE_PTR_H 00033 #define PLANCK_SAFE_PTR_H 00034 00035 #include "error_handling.h" 00036 00037 template <typename T> class safe_ptr 00038 { 00039 private: 00040 T *p; 00041 bool set; 00042 00043 // forbid copying of safe_ptrs, at least until we know how to do it 00044 safe_ptr (const safe_ptr &) {} 00045 safe_ptr &operator= (const safe_ptr &) { return *this; } 00046 00047 public: 00048 safe_ptr() : p(0), set(false) {} 00049 safe_ptr (T *p2) : p(p2), set(true) {} 00050 ~safe_ptr() { delete p; } 00051 00052 void operator= (T *p2) 00053 { 00054 planck_assert (!set, "safe_ptr: already set"); 00055 set = true; 00056 p=p2; 00057 } 00058 00059 void reset() 00060 { 00061 delete p; 00062 p=0; 00063 set=false; 00064 } 00065 00066 operator T*() { return p; } 00067 operator const T*() const { return p; } 00068 T *operator->() { return p; } 00069 const T *operator->() const { return p; } 00070 }; 00071 00072 #endif