[smart pointers] Proposal to add some small utilities

Hi to all, While developing Shmem I wrote a smart pointer to represent relative pointers and now I'm experimenting with a multi-segment shared memory pointer to create growing multi-segment shared memory containers and allocators. Both pointers are based on boost smart pointer's interface (shared_ptr, etc...). In Shmem and possibly other generic libraries the developer wants pointer-independent code, so that the pointer type is a template parameter and the same code is used for relative, reference counted, intrusive, raw, or multisegment pointers. One of such uses, in Shmem, is for example, a memory allocator (a first fit, best fit algorithm) or an intrusive container. While developing pointer-independent code, however, I've seen that some features are present in boost (for example, get_pointer() function) and other features are missing, and I would like to propose them: 1. Overloads of static_pointer_cast, dynamic_pointer_cast, const_pointer_cast overloads for raw pointers, so that we can use pointer_xxx_cast in a generic way. 2. Add reinterpret_pointer_cast to complete cast family. 3. A protocol to obtain a pointer of the same type from any pointer. For example, if you want to generically use a pointer (for example, raw or relative pointer) in a class and you want to declare another pointer to other type (raw or relative, depending on the source): //typedef typename boost::pointer_to_other //<GenericPointer, NewType>::type MyNewPointer; //A new pointer to float values typedef typename boost::pointer_to_other <SmartPtr, float>::type MyFloatPointer; We should define how this protocol is implemented in boost smart pointers (for example, using overloading or a STL-like rebind mechanism) so that every boost smart pointer developer knows how to make its pointer type protocol-aware. Shmem uses rebind-like mechanism now, but the mechanism is not important. This way, a class can receive a void pointer like a template parameter and can be pointer-independent: template <class VoidPtr> class memory_manager { struct memory_block { //... }; typedef typename boost::pointer_to_other <VoidPtr, memory_block>::type BlockPtr; BlockPtr mp_free_list; }; I think these utilities are simple to develop and quite useful. What do smart pointer experts think? Regards, Ion

Ion Gaztañaga wrote: [...]
While developing pointer-independent code, however, I've seen that some features are present in boost (for example, get_pointer() function) and other features are missing, and I would like to propose them:
1. Overloads of static_pointer_cast, dynamic_pointer_cast, const_pointer_cast overloads for raw pointers, so that we can use pointer_xxx_cast in a generic way.
Makes sense. :-) boost/utility/pointer_cast.hpp, probably.
2. Add reinterpret_pointer_cast to complete cast family.
Out of curiosity, why do you want it?
3. A protocol to obtain a pointer of the same type from any pointer. For example, if you want to generically use a pointer (for example, raw or relative pointer) in a class and you want to declare another pointer to other type (raw or relative, depending on the source):
//typedef typename boost::pointer_to_other //<GenericPointer, NewType>::type MyNewPointer;
//A new pointer to float values typedef typename boost::pointer_to_other <SmartPtr, float>::type MyFloatPointer;
We should define how this protocol is implemented in boost smart pointers (for example, using overloading or a STL-like rebind mechanism) so that every boost smart pointer developer knows how to make its pointer type protocol-aware.
template<class T, class U, template<class> class Sp> class pointer_to_other< Sp<T>, U > { typedef Sp<U> type; }; works with auto_ptr and all boost smart pointers. Other smart pointers may want to specialize pointer_to_other.

1. Overloads of static_pointer_cast, dynamic_pointer_cast, const_pointer_cast overloads for raw pointers, so that we can use pointer_xxx_cast in a generic way.
Makes sense. :-) boost/utility/pointer_cast.hpp, probably.
Nice!
2. Add reinterpret_pointer_cast to complete cast family.
Out of curiosity, why do you want it?
Well, currently I'm removing reinterpret_casts to convert void pointers with static_casts, but if there is some place where a reinterpret_cast has sense with raw pointers, I would add it to have complete family. But we can live with static_pointer_cast.
We should define how this protocol is implemented in boost smart pointers (for example, using overloading or a STL-like rebind mechanism) so that every boost smart pointer developer knows how to make its pointer type protocol-aware.
template<class T, class U, template<class> class Sp> class pointer_to_other< Sp<T>, U > { typedef Sp<U> type; };
works with auto_ptr and all boost smart pointers. Other smart pointers may want to specialize pointer_to_other.
No problem at all. What about other overloads for 2 and 3 template parameter smart pointers, so that almost all smart pointers would be covered. Of course, we need another version for raw pointers. What about boost/utility/pointer_to_other.hpp? Regards, Ion

Ion Gaztañaga wrote:
2. Add reinterpret_pointer_cast to complete cast family.
Out of curiosity, why do you want it?
Well, currently I'm removing reinterpret_casts to convert void pointers with static_casts, but if there is some place where a reinterpret_cast has sense with raw pointers, I would add it to have complete family. But we can live with static_pointer_cast.
We actually do have a motivating example for reinterpret_pointer_cast: function pointer casts, where you can't use void. Something like shared_ptr<void()> get_function( char const * module, char const * name ); I think that it was Vladimir Prus that came up with this use case... Yes, it was him: http://lists.boost.org/Archives/boost/2004/08/70705.php Thanks for reminding me. Another candidate for a TR1 extension proposal.
template<class T, class U, template<class> class Sp> class pointer_to_other< Sp<T>, U > { typedef Sp<U> type; };
works with auto_ptr and all boost smart pointers. Other smart pointers may want to specialize pointer_to_other.
No problem at all. What about other overloads for 2 and 3 template parameter smart pointers, so that almost all smart pointers would be covered. Of course, we need another version for raw pointers. What about boost/utility/pointer_to_other.hpp?
No objections here, but what about documentation and tests? :-)

No objections here, but what about documentation and tests? :-)
No problem to help on this. I will define a small documentation explaining the use of these utilities and some tests. I will e-mail you when I have something. Is quickbook a good tool for this? I've never used boost test framework, but these utilities are a good way to start. Ion
participants (2)
-
Ion Gaztañaga
-
Peter Dimov