casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DlHandle.h
Go to the documentation of this file.
1 //# DlHandle.h: smart pointer for display library.
2 //# Copyright (C) 2010
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef DLHANDLE_H_
29 #define DLHANDLE_H_
30 #include <cstddef>
31 
32 namespace casa { //# NAMESPACE CASA - BEGIN
33 
34  class DlTarget;
35 
36  class DlHandleBase {
37 
38  protected:
39  DlHandleBase( ) { }
40  DlHandleBase( const DlHandleBase & ) { }
41  virtual void target_gone( ) const = 0;
42  virtual bool null( ) const = 0;
43 
44  void throw_exception( const char * ) const;
45 
46  virtual ~DlHandleBase( ) { }
47  private:
48  friend class DlTarget;
49 
50  // Prevent heap allocation
51  void * operator new (size_t);
52  void * operator new[] (size_t);
53  };
54 
55 
56  template <class T> class DlHandle : public DlHandleBase {
57  public:
58  DlHandle( ) : target_(0) { }
59  DlHandle( T *tgt ) : target_(tgt) {
60  if ( target_ ) target_->reg(this);
61  }
62  DlHandle( const DlHandle<T> &other ) : DlHandleBase(other), target_(other.target_) {
63  if ( target_ ) target_->reg(this);
64  }
65  T *operator->( ) {
66  if ( ! target_ ) throw_exception( "null pointer in DlHandle" );
67  return target_;
68  }
69  const T *operator->( ) const {
70  if ( ! target_ ) throw_exception( "null pointer in DlHandle" );
71  return target_;
72  }
73  operator T*( ) {
74  if ( ! target_ ) throw_exception( "null pointer in DlHandle" );
75  return target_;
76  }
77  T &operator*( ) {
78  if ( ! target_ ) throw_exception( "null pointer in DlHandle" );
79  return *target_;
80  }
81  operator const T*( ) const {
82  if ( ! target_ ) throw_exception( "null pointer in DlHandle" );
83  return target_;
84  }
85  const T &operator*( ) const {
86  if ( ! target_ ) throw_exception( "null pointer in DlHandle" );
87  return *target_;
88  }
89  DlHandle<T> &operator=( const DlHandle<T> &other ) {
90  if ( target_ ) target_->unreg(this);
91  target_ = other.target_;
92  if ( target_ ) target_->reg(this);
93  return *this;
94  }
95  T *operator=( T *tgt ) {
96  if ( target_ ) target_->unreg(this);
97  target_ = tgt;
98  if ( target_ ) target_->reg(this);
99  return target_;
100  }
101 
103  if ( target_ ) target_->unreg(this);
104  }
105 
106  bool null( ) const {
107  return target_ == 0;
108  }
109  const T *ptr( ) const {
110  return target_;
111  }
112 
113  protected:
114  void target_gone( ) const {
115  ((DlHandle<T>*)this)->target_ = 0;
116  }
118  };
119 }
120 #endif
virtual ~DlHandleBase()
Definition: DlHandle.h:46
T * operator->()
Definition: DlHandle.h:65
virtual bool null() const =0
DlHandle(const DlHandle< T > &other)
Definition: DlHandle.h:62
void target_gone() const
Definition: DlHandle.h:114
const T * operator->() const
Definition: DlHandle.h:69
DlHandleBase(const DlHandleBase &)
Definition: DlHandle.h:40
T & operator*()
Definition: DlHandle.h:77
bool null() const
Definition: DlHandle.h:106
virtual void target_gone() const =0
DlHandle< T > & operator=(const DlHandle< T > &other)
Definition: DlHandle.h:89
void throw_exception(const char *) const
T * operator=(T *tgt)
Definition: DlHandle.h:95
const T & operator*() const
Definition: DlHandle.h:85
DlHandle(T *tgt)
Definition: DlHandle.h:59
const T * ptr() const
Definition: DlHandle.h:109