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_RefCountPtr.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 
00046 template <class Opaque>
00047 class OpaqueWrapper {
00048 public:
00050   OpaqueWrapper( Opaque opaque )
00051     : opaque_(opaque)
00052     {}
00054   operator Opaque () const
00055     { return opaque_; }
00057   Opaque operator()() const
00058     { return opaque_; }
00059 protected:
00060   Opaque  opaque_; // Bad in general but ...
00061 private:
00062   OpaqueWrapper(); // Not defined
00063   OpaqueWrapper(const OpaqueWrapper&); // Not defined
00064   OpaqueWrapper& operator=(const OpaqueWrapper&); // Not defined
00065 }; 
00066 
00071 template <class Opaque, class OpaqueFree>
00072 class OpaqueWrapperWithFree : public OpaqueWrapper<Opaque> {
00073 public:
00074   OpaqueWrapperWithFree( Opaque opaque, OpaqueFree opaqueFree )
00075     : OpaqueWrapper<Opaque>(opaque), opaqueFree_(opaqueFree)
00076     {}
00077   ~OpaqueWrapperWithFree()
00078     {
00079       if(opaqueFree_) {
00080 #ifdef TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
00081         Teuchos::RefCountPtr<Teuchos::FancyOStream>
00082           out = Teuchos::VerboseObjectBase::getDefaultOStream();
00083         Teuchos::OSTab tab(out);
00084         *out << "\nOpaqueWrapperWithFree::~OpaqueWrapperWithFree(): Freeing opaque object"
00085              << " of type " << typeid(Opaque).name() << "!\n";
00086 #endif // TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
00087         opaqueFree_(&this->opaque_);
00088       }
00089     }
00090 private:
00091   OpaqueFree opaqueFree_;
00092   OpaqueWrapperWithFree(); // Not defined
00093   OpaqueWrapperWithFree(const OpaqueWrapperWithFree&); // Not defined
00094   OpaqueWrapperWithFree& operator=(const OpaqueWrapperWithFree&); // Not defined
00095 }; 
00096 
00102 template <class Opaque>
00103 inline
00104 RefCountPtr<OpaqueWrapper<Opaque> >
00105 opaqueWrapper( Opaque opaque)
00106 {
00107   return rcp(new OpaqueWrapper<Opaque>(opaque));
00108 }
00109 
00115 template <class Opaque, class OpaqueFree>
00116 inline
00117 RefCountPtr<OpaqueWrapper<Opaque> >
00118 opaqueWrapper( Opaque opaque, OpaqueFree opaqueFree)
00119 {
00120   return rcp(new OpaqueWrapperWithFree<Opaque,OpaqueFree>(opaque,opaqueFree));
00121 }
00122 
00123 } // namespace Teuchos
00124 
00125 #endif  // TEUCHOS_OPAQUE_WRAPPER_HPP

Generated on Thu Sep 18 12:30:30 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1