OpenStructure
ptr_observer.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2020 by the OpenStructure authors
5 // Copyright (C) 2003-2010 by the IPLT authors
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 3.0 of the License, or (at your option)
10 // any later version.
11 // This library is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 // details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this library; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 //------------------------------------------------------------------------------
20 
21 #ifndef OST_PTR_OBSERVER_HH
22 #define OST_PTR_OBSERVER_HH
23 
24 #include <ost/message.hh>
25 #include <ost/module_config.hh>
26 /*
27  ptr observer concept
28 
29  Author: Ansgar Philippsen
30 */
31 
32 class DLLEXPORT InvalidatedPointer: public ost::Error {
33 public:
35  ost::Error("access attempt on invalidated Pointer")
36  {}
37 };
38 
39 template <class T>
40 class TEMPLATE_EXPORT PtrObserver {
41 public:
42  PtrObserver(T* t):
43  observed_(t),
44  valid_(true)
45  {}
46 
48 
49  void Invalidate() {valid_=false;}
50 
51  bool IsValid() const {return valid_;}
52 
53  T& Ref() {
54  if(!valid_) throw InvalidatedPointer();
55  return *observed_;
56  }
57 
58  T* Ptr() {
59  if(!valid_) throw InvalidatedPointer();
60  return observed_;
61  }
62 
63  bool operator==(const PtrObserver<T>& ref) const {
64  return observed_ == ref.observed_;
65  }
66 
67 private:
68  PtrObserver(const PtrObserver<T>& o);
69  PtrObserver<T>& operator=(const PtrObserver<T>& u);
70 
71  T* observed_;
72  bool valid_;
73 };
74 
75 #endif
bool operator==(const PtrObserver< T > &ref) const
Definition: ptr_observer.hh:63
PtrObserver(T *t)
Definition: ptr_observer.hh:42
void Invalidate()
Definition: ptr_observer.hh:49
bool IsValid() const
Definition: ptr_observer.hh:51
Definition: base.dox:1