Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_Handle.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00025 //
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef TEUCHOS_HANDLE_HPP
00030 #define TEUCHOS_HANDLE_HPP
00031 
00032 #include "Teuchos_ConfigDefs.hpp"
00033 #include "Teuchos_RCP.hpp"
00034 #include "Teuchos_Describable.hpp"
00035 #include "Teuchos_Handleable.hpp"
00036 
00037 namespace Teuchos 
00038 {
00039 
00065   template <typename PointerType> 
00066   class ConstHandle : public virtual Describable
00067   {
00068   public:
00070     ConstHandle(const RCP<const PointerType>& ptr) : ptr_(ptr) {;}
00073     explicit ConstHandle(const ConstHandleable<PointerType>* ptr) : ptr_(ptr->getConstRcp()) {;}
00075     const RCP<const PointerType>& constPtr() const {return ptr_;}
00077     const PointerType * const rawPtr() {return this->constPtr().get();}
00078   protected:
00080     explicit ConstHandle() : ptr_() {;}
00085     void setRcp(const RCP<PointerType>& ptr) 
00086     {ptr_=rcp_const_cast<const PointerType>(ptr);}
00091     RCP<PointerType> nonConstPtr() const
00092     {return rcp_const_cast<PointerType>(ptr_);}
00093   private:
00095     RCP<const PointerType> ptr_;
00096   };
00097 
00121   template <typename PointerType> 
00122   class Handle : public virtual ConstHandle<PointerType>
00123   {
00124   public:
00126     Handle()
00127       : ConstHandle<PointerType>() {}
00129     Handle(const RCP<PointerType>& smartPtr)
00130       : ConstHandle<PointerType>() 
00131     {
00132       /* \brief We need to set the rcp in the base class */
00133       setRcp(smartPtr);
00134     }
00140     explicit Handle(Handleable<PointerType>* rawPtr)
00141       : ConstHandle<PointerType>()
00142     {
00143       /* \brief We need to set the rcp in the base class. */
00144       setRcp(rawPtr->getRcp());
00145     }
00148     RCP<PointerType> ptr() const {return this->nonConstPtr();}
00150     PointerType* rawPtr() const {return this->nonConstPtr().get();}
00151   };
00152 
00153 } // namespace Teuchos
00154 
00166 #define TEUCHOS_HANDLE_CTORS(handle, contents) \
00167 handle() : Teuchos::Handle<contents >() {;} \
00168 handle(Teuchos::Handleable<contents >* rawPtr) : Teuchos::Handle<contents >(rawPtr) {;} \
00169 handle(const Teuchos::RCP<contents >& smartPtr) : Teuchos::Handle<contents >(smartPtr){;}
00170 
00182 #define TEUCHOS_CONST_HANDLE_CTORS(handle, contents) \
00183 handle( Teuchos::ENull _null = Teuchos::null ) : Teuchos::ConstHandle<contents >() {;} \
00184 handle(const Teuchos::ConstHandleable<contents >* rawPtr) : Teuchos::ConstHandle<contents >(rawPtr) {;} \
00185 handle(const Teuchos::RCP<const contents >& smartPtr) : Teuchos::ConstHandle<contents >(smartPtr){;}
00186 
00187 #endif // TEUCHOS_CONSTHANDLE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines