Teuchos_OpaqueWrapper.hpp

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 #include "Teuchos_RCP.hpp"
00033 
00034 //#define TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
00035 
00036 #ifdef TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
00037 #  include "Teuchos_VerboseObject.hpp"
00038 #endif // TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
00039 
00040 namespace Teuchos {
00041 
00104 template <class Opaque>
00105 class OpaqueWrapper {
00106 public:
00108   OpaqueWrapper( Opaque opaque )
00109     : opaque_(opaque)
00110     {}
00112   operator Opaque () const
00113     { return opaque_; }
00115   Opaque operator()() const
00116     { return opaque_; }
00117 protected:
00118   Opaque  opaque_; // Bad in general but ...
00119 private:
00120   OpaqueWrapper(); // Not defined
00121   OpaqueWrapper(const OpaqueWrapper&); // Not defined
00122   OpaqueWrapper& operator=(const OpaqueWrapper&); // Not defined
00123 }; 
00124 
00141 template <class Opaque, class OpaqueFree>
00142 class OpaqueWrapperWithFree : public OpaqueWrapper<Opaque> {
00143 public:
00144   OpaqueWrapperWithFree( Opaque opaque, OpaqueFree opaqueFree )
00145     : OpaqueWrapper<Opaque>(opaque), opaqueFree_(opaqueFree)
00146     {}
00147   ~OpaqueWrapperWithFree()
00148     {
00149       if(opaqueFree_) {
00150 #ifdef TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
00151         Teuchos::RCP<Teuchos::FancyOStream>
00152           out = Teuchos::VerboseObjectBase::getDefaultOStream();
00153         Teuchos::OSTab tab(out);
00154         *out << "\nOpaqueWrapperWithFree::~OpaqueWrapperWithFree(): Freeing opaque object"
00155              << " of type " << TypeNameTraits<Opaque>::name() << "!\n";
00156 #endif // TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
00157         opaqueFree_(&this->opaque_);
00158       }
00159     }
00160 private:
00161   OpaqueFree opaqueFree_;
00162   OpaqueWrapperWithFree(); // Not defined
00163   OpaqueWrapperWithFree(const OpaqueWrapperWithFree&); // Not defined
00164   OpaqueWrapperWithFree& operator=(const OpaqueWrapperWithFree&); // Not defined
00165 }; 
00166 
00172 template <class Opaque>
00173 inline
00174 RCP<OpaqueWrapper<Opaque> >
00175 opaqueWrapper( Opaque opaque)
00176 {
00177   return rcp(new OpaqueWrapper<Opaque>(opaque));
00178 }
00179 
00185 template <class Opaque, class OpaqueFree>
00186 inline
00187 RCP<OpaqueWrapper<Opaque> >
00188 opaqueWrapper( Opaque opaque, OpaqueFree opaqueFree)
00189 {
00190   return rcp(new OpaqueWrapperWithFree<Opaque,OpaqueFree>(opaque,opaqueFree));
00191 }
00192 
00193 } // namespace Teuchos
00194 
00195 #endif  // TEUCHOS_OPAQUE_WRAPPER_HPP

Generated on Tue Oct 20 12:45:26 2009 for Teuchos - Trilinos Tools Package by doxygen 1.4.7