Custom smart pointer with const Types

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom smart pointer with const Types

Holger Brandsmeier
Dear list,

how should I export functions to python which return smart pointers to
const-pointers, e.g. shared_ptr<const T>?

For my own classes I always tried to avoid this problem by always
providing a methods which returns shared_ptr<T>.

Now I need to export the following method in a class provided by some
other software package (Trilinos). Its implementation I do not want to
change, the function is declared as
 Teuchos::RCP< const Teuchos::Comm< int > >    getComm () const
(if you need details:
http://trilinos.sandia.gov/packages/docs/r10.6/packages/tpetra/doc/html/classTpetra_1_1MpiPlatform.html
)

I believe I already exported the custom smart pointer `Teuchos::RCP`
to python, I also exported the class `Teuchos::Comm< int >` to python,
but I get the error
 No to_python (by-value) converter found for C++ type:
Teuchos::RCP<Teuchos::Comm<int> const>
which is perfectly true, as I did not export the class `const
Teuchos::Comm<int>`.

I briefly tried to export also the const version of this class (all
methods that I need are provided are available for the const version),
but I failed exporting the class with varying error message, depending
on how I tried to export it. I realized that I don't know how or even
if I should export a const version of a type?

Is there another workaround to this problem? Is there something I'm
missing in the implementation of my custom smart pointer?

I could wrap the function getComm() above to cast away the const'ness,
but do I need to?

I also found some old messages on the list titled "[Boost.Python]
shared_ptr<const T>" with some workaround proposed by providing
get_pointer for const T, but I believe that I have a different problem
that I can not solve by modifying get_pointer.

-Holger
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: Custom smart pointer with const Types

Jim Bosch-2
On 10/05/2011 03:08 AM, Holger Brandsmeier wrote:

> Dear list,
>
> how should I export functions to python which return smart pointers to
> const-pointers, e.g. shared_ptr<const T>?
>
> For my own classes I always tried to avoid this problem by always
> providing a methods which returns shared_ptr<T>.
>
> Now I need to export the following method in a class provided by some
> other software package (Trilinos). Its implementation I do not want to
> change, the function is declared as
>   Teuchos::RCP<  const Teuchos::Comm<  int>  >     getComm () const
> (if you need details:
> http://trilinos.sandia.gov/packages/docs/r10.6/packages/tpetra/doc/html/classTpetra_1_1MpiPlatform.html
> )
>
> I believe I already exported the custom smart pointer `Teuchos::RCP`
> to python, I also exported the class `Teuchos::Comm<  int>` to python,
> but I get the error
>   No to_python (by-value) converter found for C++ type:
> Teuchos::RCP<Teuchos::Comm<int>  const>
> which is perfectly true, as I did not export the class `const
> Teuchos::Comm<int>`.
>
> I briefly tried to export also the const version of this class (all
> methods that I need are provided are available for the const version),
> but I failed exporting the class with varying error message, depending
> on how I tried to export it. I realized that I don't know how or even
> if I should export a const version of a type?
>
> Is there another workaround to this problem? Is there something I'm
> missing in the implementation of my custom smart pointer?
>
> I could wrap the function getComm() above to cast away the const'ness,
> but do I need to?
>
> I also found some old messages on the list titled "[Boost.Python]
> shared_ptr<const T>" with some workaround proposed by providing
> get_pointer for const T, but I believe that I have a different problem
> that I can not solve by modifying get_pointer.
>

I'm afraid you do have a different problem, though some of those ideas
help.  Essentially, Boost.Python's support for custom smart pointers
isn't as complete as its support for shared_ptr, and its support for
const smart pointers is basically nonexistent.  You're pretty much in
not-currently-supported territory.

I think wrapping getComm() to cast away the constness is going to be the
easiest way to make this work, but if you have many such functions it
may be worth digging deeper into the Boost.Python internals to try and
trick it into working by specializing some templates.

Jim
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: Custom smart pointer with const Types

Holger Brandsmeier
Jim,

how do you handle smart_ptr<const T> in boost python? Do you simply
cast away the constness?


For my custom smart pointer I provide a class extending
  to_python_converter<Teuchos::RCP<T>, rcp_to_python<T>, true>
now I decided to also implement
  to_python_converter<Teuchos::RCP<const T>, rcp_to_python_const<T>, true>
where I simply cast away the constness and use the above implementation.

This seems to be working so far. Did you provide a smarter
implementation for shared_ptr<const T>?

-Holger


On Wed, Oct 5, 2011 at 15:44, Jim Bosch <[hidden email]> wrote:

> On 10/05/2011 03:08 AM, Holger Brandsmeier wrote:
>>
>> Dear list,
>>
>> how should I export functions to python which return smart pointers to
>> const-pointers, e.g. shared_ptr<const T>?
>>
>> For my own classes I always tried to avoid this problem by always
>> providing a methods which returns shared_ptr<T>.
>>
>> Now I need to export the following method in a class provided by some
>> other software package (Trilinos). Its implementation I do not want to
>> change, the function is declared as
>>  Teuchos::RCP<  const Teuchos::Comm<  int>  >     getComm () const
>> (if you need details:
>>
>> http://trilinos.sandia.gov/packages/docs/r10.6/packages/tpetra/doc/html/classTpetra_1_1MpiPlatform.html
>> )
>>
>> I believe I already exported the custom smart pointer `Teuchos::RCP`
>> to python, I also exported the class `Teuchos::Comm<  int>` to python,
>> but I get the error
>>  No to_python (by-value) converter found for C++ type:
>> Teuchos::RCP<Teuchos::Comm<int>  const>
>> which is perfectly true, as I did not export the class `const
>> Teuchos::Comm<int>`.
>>
>> I briefly tried to export also the const version of this class (all
>> methods that I need are provided are available for the const version),
>> but I failed exporting the class with varying error message, depending
>> on how I tried to export it. I realized that I don't know how or even
>> if I should export a const version of a type?
>>
>> Is there another workaround to this problem? Is there something I'm
>> missing in the implementation of my custom smart pointer?
>>
>> I could wrap the function getComm() above to cast away the const'ness,
>> but do I need to?
>>
>> I also found some old messages on the list titled "[Boost.Python]
>> shared_ptr<const T>" with some workaround proposed by providing
>> get_pointer for const T, but I believe that I have a different problem
>> that I can not solve by modifying get_pointer.
>>
>
> I'm afraid you do have a different problem, though some of those ideas help.
>  Essentially, Boost.Python's support for custom smart pointers isn't as
> complete as its support for shared_ptr, and its support for const smart
> pointers is basically nonexistent.  You're pretty much in
> not-currently-supported territory.
>
> I think wrapping getComm() to cast away the constness is going to be the
> easiest way to make this work, but if you have many such functions it may be
> worth digging deeper into the Boost.Python internals to try and trick it
> into working by specializing some templates.
>
> Jim
> _______________________________________________
> Cplusplus-sig mailing list
> [hidden email]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: Custom smart pointer with const Types

Jim Bosch-2
On 10/05/2011 11:31 AM, Holger Brandsmeier wrote:

> Jim,
>
> how do you handle smart_ptr<const T>  in boost python? Do you simply
> cast away the constness?
>
>
> For my custom smart pointer I provide a class extending
>    to_python_converter<Teuchos::RCP<T>, rcp_to_python<T>, true>
> now I decided to also implement
>    to_python_converter<Teuchos::RCP<const T>, rcp_to_python_const<T>, true>
> where I simply cast away the constness and use the above implementation.
>
> This seems to be working so far. Did you provide a smarter
> implementation for shared_ptr<const T>?
>

Personally, I pretty much always use shared_ptr, and I've written a
rather large extension to support constness on the Python side, and
dealing with shared_ptr<const T> is a side effect of that.

I'm welcome to pass it on if you're interested, but I don't think it
really addresses your problem.

Jim



>
> On Wed, Oct 5, 2011 at 15:44, Jim Bosch<[hidden email]>  wrote:
>> On 10/05/2011 03:08 AM, Holger Brandsmeier wrote:
>>>
>>> Dear list,
>>>
>>> how should I export functions to python which return smart pointers to
>>> const-pointers, e.g. shared_ptr<const T>?
>>>
>>> For my own classes I always tried to avoid this problem by always
>>> providing a methods which returns shared_ptr<T>.
>>>
>>> Now I need to export the following method in a class provided by some
>>> other software package (Trilinos). Its implementation I do not want to
>>> change, the function is declared as
>>>   Teuchos::RCP<    const Teuchos::Comm<    int>    >       getComm () const
>>> (if you need details:
>>>
>>> http://trilinos.sandia.gov/packages/docs/r10.6/packages/tpetra/doc/html/classTpetra_1_1MpiPlatform.html
>>> )
>>>
>>> I believe I already exported the custom smart pointer `Teuchos::RCP`
>>> to python, I also exported the class `Teuchos::Comm<    int>` to python,
>>> but I get the error
>>>   No to_python (by-value) converter found for C++ type:
>>> Teuchos::RCP<Teuchos::Comm<int>    const>
>>> which is perfectly true, as I did not export the class `const
>>> Teuchos::Comm<int>`.
>>>
>>> I briefly tried to export also the const version of this class (all
>>> methods that I need are provided are available for the const version),
>>> but I failed exporting the class with varying error message, depending
>>> on how I tried to export it. I realized that I don't know how or even
>>> if I should export a const version of a type?
>>>
>>> Is there another workaround to this problem? Is there something I'm
>>> missing in the implementation of my custom smart pointer?
>>>
>>> I could wrap the function getComm() above to cast away the const'ness,
>>> but do I need to?
>>>
>>> I also found some old messages on the list titled "[Boost.Python]
>>> shared_ptr<const T>" with some workaround proposed by providing
>>> get_pointer for const T, but I believe that I have a different problem
>>> that I can not solve by modifying get_pointer.
>>>
>>
>> I'm afraid you do have a different problem, though some of those ideas help.
>>   Essentially, Boost.Python's support for custom smart pointers isn't as
>> complete as its support for shared_ptr, and its support for const smart
>> pointers is basically nonexistent.  You're pretty much in
>> not-currently-supported territory.
>>
>> I think wrapping getComm() to cast away the constness is going to be the
>> easiest way to make this work, but if you have many such functions it may be
>> worth digging deeper into the Boost.Python internals to try and trick it
>> into working by specializing some templates.
>>
>> Jim
>> _______________________________________________
>> Cplusplus-sig mailing list
>> [hidden email]
>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>>
> _______________________________________________
> Cplusplus-sig mailing list
> [hidden email]
> http://mail.python.org/mailman/listinfo/cplusplus-sig

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: Custom smart pointer with const Types

Wichert Akkerman
On 2011-10-5 19:00, Jim Bosch wrote:

> On 10/05/2011 11:31 AM, Holger Brandsmeier wrote:
>> Jim,
>>
>> how do you handle smart_ptr<const T> in boost python? Do you simply
>> cast away the constness?
>>
>>
>> For my custom smart pointer I provide a class extending
>> to_python_converter<Teuchos::RCP<T>, rcp_to_python<T>, true>
>> now I decided to also implement
>> to_python_converter<Teuchos::RCP<const T>, rcp_to_python_const<T>, true>
>> where I simply cast away the constness and use the above implementation.
>>
>> This seems to be working so far. Did you provide a smarter
>> implementation for shared_ptr<const T>?
>>
>
> Personally, I pretty much always use shared_ptr, and I've written a
> rather large extension to support constness on the Python side, and
> dealing with shared_ptr<const T> is a side effect of that.
>
> I'm welcome to pass it on if you're interested, but I don't think it
> really addresses your problem.

I would be definitely be interested in seeing how you're handling const
objects.

Wichert.

--
Wichert Akkerman <[hidden email]>   It is simple to make things.
http://www.wiggy.net/                  It is hard to make things simple.
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: Custom smart pointer with const Types

Jim Bosch-2
On 10/06/2011 05:30 PM, Wichert Akkerman wrote:

> On 2011-10-5 19:00, Jim Bosch wrote:
>>
>> Personally, I pretty much always use shared_ptr, and I've written a
>> rather large extension to support constness on the Python side, and
>> dealing with shared_ptr<const T> is a side effect of that.
>>
>> I'm welcome to pass it on if you're interested, but I don't think it
>> really addresses your problem.
>
> I would be definitely be interested in seeing how you're handling const
> objects.
>

You can find a reasonable up-to-date version at

https://svn.boost.org/svn/boost/sandbox/python_extensions

There are a lot of other little extensions there too; you'll be most
interested in the const_aware subdirectories.  It's highly likely the
build system is broken, but the code itself should still work.  The best
documentation is probably the const_aware example in the tests.

Adding this to Boost.Python proper is a big part of my plans for a
Boost.Python v3, but that's still a ways off, and unfortunately the code
I'm pointing you at may have suffered from a little bitrot as I haven't
looked at it in a while.

Jim
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig