Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_RCPStdSharedPtrConversions.hpp
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_RCP_STD_SHAREDPTR_CONVERSIONS_HPP
11 #define TEUCHOS_RCP_STD_SHAREDPTR_CONVERSIONS_HPP
12 
13 #include "Teuchos_RCPStdSharedPtrConversionsDecl.hpp"
14 #include "Teuchos_RCP.hpp"
15 
16 
17 template<class T>
19 Teuchos::rcp( const std::shared_ptr<T> &sptr )
20 {
21  if (sptr.get()) {
22  // First, see if the RCP is in the shared_ptr deleter object
23  const StdSharedPtrRCPDeleter<T>
24  *rcpd = std::get_deleter<StdSharedPtrRCPDeleter<T> >(sptr);
25  if (rcpd) {
26  return rcpd->ptr();
27  }
28 #ifdef TEUCHOS_DEBUG
29  // Second, see if the an RCP node pointing to this type already exists
30  // from being wrapped already from a prior call to this function where the
31  // add_new_RCPNode(...) function could have been called already!.
32  RCPNode* existingRCPNode = RCPNodeTracer::getExistingRCPNode(sptr.get());
33  if (existingRCPNode) {
34  return RCP<T>(sptr.get(), RCPNodeHandle(existingRCPNode, RCP_STRONG, false));
35  }
36 #endif
37  // Lastly, we just create a new RCP and RCPNode ...
38  return rcpWithDealloc(sptr.get(), DeallocStdSharedPtr<T>(sptr), true);
39  }
40  return null;
41 }
42 
43 
44 template<class T>
45 std::shared_ptr<T>
46 Teuchos::get_shared_ptr( const RCP<T> &rcp )
47 {
48  if (nonnull(rcp)) {
49  Ptr<const DeallocStdSharedPtr<T> >
50  dbsp = get_optional_dealloc<DeallocStdSharedPtr<T> >(rcp);
51  if (nonnull(dbsp)) {
52  return dbsp->ptr();
53  }
54  return std::shared_ptr<T>(rcp.get(), StdSharedPtrRCPDeleter<T>(rcp));
55  }
56  return std::shared_ptr<T>();
57 }
58 
59 
60 #endif // TEUCHOS_RCP_STD_SHAREDPTR_CONVERSIONS_HPP
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Smart reference counting pointer class for automatic garbage collection.
RCP< T > rcpWithDealloc(T *p, Dealloc_T dealloc, bool owns_mem=true)
Initialize from a raw pointer with a deallocation policy.
Reference-counted pointer class and non-member templated function implementations.
bool nonnull(const ArrayRCP< T > &p)
Returns true if p.get()!=NULL.