00001 //------------------------------------------------------------------------------ 00002 // This file is part of the OpenStructure project <www.openstructure.org> 00003 // 00004 // Copyright (C) 2008-2011 by the OpenStructure authors 00005 // Copyright (C) 2003-2010 by the IPLT authors 00006 // 00007 // This library is free software; you can redistribute it and/or modify it under 00008 // the terms of the GNU Lesser General Public License as published by the Free 00009 // Software Foundation; either version 3.0 of the License, or (at your option) 00010 // any later version. 00011 // This library is distributed in the hope that it will be useful, but WITHOUT 00012 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00013 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00014 // details. 00015 // 00016 // You should have received a copy of the GNU Lesser General Public License 00017 // along with this library; if not, write to the Free Software Foundation, Inc., 00018 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 //------------------------------------------------------------------------------ 00020 00021 #ifndef OST_PTR_OBSERVER_HH 00022 #define OST_PTR_OBSERVER_HH 00023 00024 #include <ost/message.hh> 00025 #include <ost/module_config.hh> 00026 /* 00027 ptr observer concept 00028 00029 Author: Ansgar Philippsen 00030 */ 00031 00032 class DLLEXPORT InvalidatedPointer: public ost::Error { 00033 public: 00034 InvalidatedPointer(): 00035 ost::Error("access attempt on invalidated Pointer") 00036 {} 00037 }; 00038 00039 template <class T> 00040 class TEMPLATE_EXPORT PtrObserver { 00041 public: 00042 PtrObserver(T* t): 00043 observed_(t), 00044 valid_(true) 00045 {} 00046 00047 ~PtrObserver() {} 00048 00049 void Invalidate() {valid_=false;} 00050 00051 bool IsValid() const {return valid_;} 00052 00053 T& Ref() { 00054 if(!valid_) throw InvalidatedPointer(); 00055 return *observed_; 00056 } 00057 00058 T* Ptr() { 00059 if(!valid_) throw InvalidatedPointer(); 00060 return observed_; 00061 } 00062 00063 bool operator==(const PtrObserver<T>& ref) const { 00064 return observed_ == ref.observed_; 00065 } 00066 00067 private: 00068 PtrObserver(const PtrObserver<T>& o); 00069 PtrObserver<T>& operator=(const PtrObserver<T>& u); 00070 00071 T* observed_; 00072 bool valid_; 00073 }; 00074 00075 #endif