Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_OpaqueWrapper.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_OPAQUE_WRAPPER_HPP
00030 #define TEUCHOS_OPAQUE_WRAPPER_HPP
00031 
00032 
00033 #include "Teuchos_RCP.hpp"
00034 
00035 
00036 //#define TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
00037 
00038 #ifdef TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
00039 #  include "Teuchos_VerboseObject.hpp"
00040 #endif // TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
00041 
00042 
00043 namespace Teuchos {
00044 
00045 
00108 template <class Opaque>
00109 class OpaqueWrapper {
00110 public:
00112   OpaqueWrapper( Opaque opaque )
00113     : opaque_(opaque)
00114     {}
00116   operator Opaque () const
00117     { return opaque_; }
00119   Opaque operator()() const
00120     { return opaque_; }
00121 protected:
00122   Opaque  opaque_; // Bad in general but ...
00123 private:
00124   OpaqueWrapper(); // Not defined
00125   OpaqueWrapper(const OpaqueWrapper&); // Not defined
00126   OpaqueWrapper& operator=(const OpaqueWrapper&); // Not defined
00127 }; 
00128 
00145 template <class Opaque, class OpaqueFree>
00146 class OpaqueWrapperWithFree : public OpaqueWrapper<Opaque> {
00147 public:
00148   OpaqueWrapperWithFree( Opaque opaque, OpaqueFree opaqueFree )
00149     : OpaqueWrapper<Opaque>(opaque), opaqueFree_(opaqueFree)
00150     {}
00151   ~OpaqueWrapperWithFree()
00152     {
00153       if(opaqueFree_) {
00154 #ifdef TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
00155         Teuchos::RCP<Teuchos::FancyOStream>
00156           out = Teuchos::VerboseObjectBase::getDefaultOStream();
00157         Teuchos::OSTab tab(out);
00158         *out << "\nOpaqueWrapperWithFree::~OpaqueWrapperWithFree(): Freeing opaque object"
00159              << " of type " << TypeNameTraits<Opaque>::name() << "!\n";
00160 #endif // TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
00161         opaqueFree_(&this->opaque_);
00162       }
00163     }
00164 private:
00165   OpaqueFree opaqueFree_;
00166   OpaqueWrapperWithFree(); // Not defined
00167   OpaqueWrapperWithFree(const OpaqueWrapperWithFree&); // Not defined
00168   OpaqueWrapperWithFree& operator=(const OpaqueWrapperWithFree&); // Not defined
00169 }; 
00170 
00171 
00177 template <class Opaque>
00178 inline
00179 RCP<OpaqueWrapper<Opaque> >
00180 opaqueWrapper( Opaque opaque)
00181 {
00182   return rcp(new OpaqueWrapper<Opaque>(opaque));
00183 }
00184 
00185 
00191 template <class Opaque, class OpaqueFree>
00192 inline
00193 RCP<OpaqueWrapper<Opaque> >
00194 opaqueWrapper( Opaque opaque, OpaqueFree opaqueFree)
00195 {
00196   return rcp(new OpaqueWrapperWithFree<Opaque,OpaqueFree>(opaque,opaqueFree));
00197 }
00198 
00199 
00200 } // namespace Teuchos
00201 
00202 
00203 #endif  // TEUCHOS_OPAQUE_WRAPPER_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines