Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_OpaqueWrapper.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_OPAQUE_WRAPPER_HPP
11 #define TEUCHOS_OPAQUE_WRAPPER_HPP
12 
13 
14 #include "Teuchos_RCP.hpp"
15 
16 
17 //#define TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
18 
19 #ifdef TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
20 # include "Teuchos_VerboseObject.hpp"
21 #endif // TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
22 
23 
24 namespace Teuchos {
25 
26 
207 template <class Opaque>
209 public:
214  OpaqueWrapper( Opaque opaque )
215  : opaque_(opaque)
216  {}
231  operator Opaque () const
232  { return opaque_; }
238  Opaque operator()() const
239  { return opaque_; }
240 protected:
247  Opaque opaque_;
248 private:
249  OpaqueWrapper(); // Not defined
250  OpaqueWrapper(const OpaqueWrapper&); // Not defined
251  OpaqueWrapper& operator=(const OpaqueWrapper&); // Not defined
252 };
253 
279 template <class Opaque, class OpaqueFree>
280 class OpaqueWrapperWithFree : public OpaqueWrapper<Opaque> {
281 public:
283  OpaqueWrapperWithFree( Opaque opaque, OpaqueFree opaqueFree )
284  : OpaqueWrapper<Opaque>(opaque), opaqueFree_(opaqueFree)
285  {}
288  {
289  // FIXME (mfh 10 Sep 2012) This only works if the free function is
290  // a raw function pointer, not if it is a general "function object"
291  // (i.e., something callable via operator()).
292  if(opaqueFree_) {
293 #ifdef TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
296  Teuchos::OSTab tab(out);
297  *out << "\nOpaqueWrapperWithFree::~OpaqueWrapperWithFree(): Freeing opaque object"
298  << " of type " << TypeNameTraits<Opaque>::name() << "!\n";
299 #endif // TEUCHOS_OPAQUE_WRAPPER_ANNOUNCE_FREE
300  opaqueFree_(&this->opaque_);
301  }
302  }
303 private:
305  OpaqueFree opaqueFree_;
306  OpaqueWrapperWithFree(); // Not defined
307  OpaqueWrapperWithFree(const OpaqueWrapperWithFree&); // Not defined
308  OpaqueWrapperWithFree& operator=(const OpaqueWrapperWithFree&); // Not defined
309 };
310 
311 
319 template <class Opaque>
320 inline
322 opaqueWrapper( Opaque opaque)
323 {
324  return rcp(new OpaqueWrapper<Opaque>(opaque));
325 }
326 
327 
335 template <class Opaque, class OpaqueFree>
336 inline
338 opaqueWrapper( Opaque opaque, OpaqueFree opaqueFree)
339 {
340  return rcp(new OpaqueWrapperWithFree<Opaque,OpaqueFree>(opaque,opaqueFree));
341 }
342 
343 
344 } // namespace Teuchos
345 
346 
347 #endif // TEUCHOS_OPAQUE_WRAPPER_HPP
RCP< OpaqueWrapper< Opaque > > opaqueWrapper(Opaque opaque, OpaqueFree opaqueFree)
Create a new OpaqueWrapper object with a free function.
Opaque operator()() const
Explicit type conversion from wrapper to raw handle.
OpaqueWrapper(Opaque opaque)
Constructor that accepts and wraps a raw handle.
~OpaqueWrapperWithFree()
Destructor: invokes the free function.
Tabbing class for helping to create formated, indented output for a basic_FancyOStream object...
RCP< OpaqueWrapper< Opaque > > opaqueWrapper(Opaque opaque)
Create a new OpaqueWrapper object without a free function.
Base class for wrapped opaque objects.
OpaqueFree opaqueFree_
Function (or function object) for freeing the handle.
Opaque opaque_
The actual handle.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Subclass for wrapped opaque objects with a free function.
OpaqueWrapperWithFree & operator=(const OpaqueWrapperWithFree &)
OpaqueWrapperWithFree(Opaque opaque, OpaqueFree opaqueFree)
Constructor: takes the opaque handle, and its free function.
OpaqueWrapper & operator=(const OpaqueWrapper &)
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
Smart reference counting pointer class for automatic garbage collection.
Reference-counted pointer class and non-member templated function implementations.