[Trilinos-Users] boost/std::shared_ptr interoperability
Simone Pezzuto
junki.gnu at gmail.com
Thu Jan 30 08:47:09 MST 2014
Some time ago, after digging on stackoverflow, I came up with this solution:
#include <Teuchos_RCP.hpp>
#include <Teuchos_RCPBoostSharedPtrConversions.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <cstdlib>
// It creates T1<T> from T2<T>, in such a way that the deleter
// of T1<T> it's just a lambda function that holds a reference of T2
template<template <typename T> class T1, template <typename T> class T2,
typename T>
T1<T> convert_to (const T2<T>& ptr)
{
return T1<T> (ptr.get(), [ptr](T*){});
}
int main(void)
{
// boost -> {std, Teuchos}
{
boost::shared_ptr<int> b_ptr(new int(20));
auto c_ptr = convert_to<std::shared_ptr>(b_ptr);
auto t_ptr = Teuchos::rcp(b_ptr);
}
// RCP -> {boost, std}
{
Teuchos::RCP<int> t_ptr(new int(20));
auto b_ptr = Teuchos::shared_pointer(t_ptr);
auto c_ptr = convert_to<std::shared_ptr>(t_ptr);
}
// std -> {boost, Teuchos}
{
std::shared_ptr<int> c_ptr(new int(20));
auto b_ptr = convert_to<boost::shared_ptr>(c_ptr);
// RCP doesn't support lambda functions, while boost::shared_ptr
does
// because the deleter is templated
// auto t_ptr = convert_to<Teuchos::RCP>(c_ptr); // Not working!
// Not optimal workaround
auto t_ptr = Teuchos::rcp(convert_to<boost::shared_ptr>(s_ptr));
}
return EXIT_SUCCESS;
}
Is fundamental to have consistent internal counters of each smart pointer.
The "conversion" simply overloads
the deleter with a do-nothing function, but such that it takes as argument
a reference of the other smart pointer,
so that its internal counter is increased.
Cheers,
Simone
2014-01-30 Bartlett, Roscoe A. <bartlettra at ornl.gov>:
> Nico and others,
>
> You will want to copy the code for supporting the interoperability of
> Teuchos::RCP and boost::shared_ptr for use with std::shared_ptr I think.
>
> I am currently on projects that don’t allow C++11 yet so you should expect
> little support for C++11 at this point. However, this might change in the
> next few months. At that point, I will update a bunch of this software to
> optionally take advantage of C++11 features and to provide interoperability
> with std::shared_ptr.
>
> -Ross
>
> P.S. I remember being a grad student being able to use bleeding edge
> compilers and approaches. Those were good days in many respects.
>
> > -----Original Message-----
> > From: trilinos-users-bounces at software.sandia.gov [mailto:trilinos-users-
> > bounces at software.sandia.gov] On Behalf Of Wojciech Smigaj
> > Sent: Thursday, January 30, 2014 9:08 AM
> > To: trilinos-users at software.sandia.gov
> > Subject: Re: [Trilinos-Users] boost/std::shared_ptr interoperability
> >
> > On 30/01/2014 13:12, Nico Schlömer wrote:
> > > Hi all,
> > >
> > > is there any better way for creating an RCP from a
> > boost/std::shared_ptr than
> > >
> > > Teuchos::rcp(myBoostPtr.get(), false))
> > >
> > > ?
> > >
> > > Cheers,
> > > Nico
> >
> > Hi Nico,
> >
> > yes, there are dedicated functions for doing this; see here:
> >
> >
> http://trilinos.sandia.gov/packages/docs/r11.0/packages/teuchos/doc/html/
> > group__Teuchos__RCPBoostSharedPtrConversions__grp.html
> >
> > Best regards,
> > Wojciech
> > _______________________________________________
> > Trilinos-Users mailing list
> > Trilinos-Users at software.sandia.gov
> > http://software.sandia.gov/mailman/listinfo/trilinos-users
>
> _______________________________________________
> Trilinos-Users mailing list
> Trilinos-Users at software.sandia.gov
> http://software.sandia.gov/mailman/listinfo/trilinos-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://software.sandia.gov/pipermail/trilinos-users/attachments/20140130/052afefe/attachment.html
More information about the Trilinos-Users
mailing list