casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
DlHandle.h
Go to the documentation of this file.
00001 //# DlHandle.h: smart pointer for display library.
00002 //# Copyright (C) 2010
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id$
00027 
00028 #ifndef DLHANDLE_H_
00029 #define DLHANDLE_H_
00030 #include <cstddef>
00031 
00032 namespace casa { //# NAMESPACE CASA - BEGIN
00033 
00034     class DlTarget;
00035 
00036     class DlHandleBase {
00037 
00038         protected:
00039             DlHandleBase( ) { }
00040             DlHandleBase( const DlHandleBase & ) { }
00041             virtual void target_gone( ) const = 0;
00042             virtual bool null( ) const = 0;
00043 
00044             void throw_exception( const char * ) const;
00045 
00046             virtual ~DlHandleBase( ) { }
00047         private:
00048             friend class DlTarget;
00049         
00050             // Prevent heap allocation
00051             void * operator new   (size_t);
00052             void * operator new[] (size_t);
00053     };
00054 
00055 
00056     template <class T> class DlHandle : public DlHandleBase {
00057         public:
00058             DlHandle( ) : target_(0) { }
00059             DlHandle( T *tgt ) : target_(tgt) { if ( target_ ) target_->reg(this); }
00060             DlHandle( const DlHandle<T> &other ) : DlHandleBase(other), target_(other.target_) { if ( target_ ) target_->reg(this); }
00061             T *operator->( ) { if ( ! target_ ) throw_exception( "null pointer in DlHandle" ); return target_; }
00062             const T *operator->( ) const { if ( ! target_ ) throw_exception( "null pointer in DlHandle" ); return target_; }
00063             operator T*( ) { if ( ! target_ ) throw_exception( "null pointer in DlHandle" ); return target_; }
00064             T &operator*( ) { if ( ! target_ ) throw_exception( "null pointer in DlHandle" ); return *target_; }
00065             operator const T*( ) const { if ( ! target_ ) throw_exception( "null pointer in DlHandle" ); return target_; }
00066             const T &operator*( ) const { if ( ! target_ ) throw_exception( "null pointer in DlHandle" ); return *target_; }
00067             DlHandle<T> &operator=( const DlHandle<T> &other ) { if ( target_ ) target_->unreg(this); target_ = other.target_; if ( target_ ) target_->reg(this); return *this; }
00068             T *operator=( T *tgt ) { if ( target_ ) target_->unreg(this); target_ = tgt; if ( target_ ) target_->reg(this); return target_; }
00069 
00070             ~DlHandle( ) { if ( target_ ) target_->unreg(this); }
00071 
00072             bool null( ) const { return target_ == 0; }
00073             const T *ptr( ) const { return target_; }
00074 
00075         protected:
00076             void target_gone( ) const { ((DlHandle<T>*)this)->target_ = 0; }
00077             T *target_;
00078     };
00079 }
00080 #endif