Boost logo

Boost :

From: Ion Gaztañaga (ion_g_m_at_[hidden])
Date: 2005-01-05 05:59:07


Hello to all,

 While developing the Boost Shmem library I needed to be independent of the
pointer types (raw or smart), so I started looking for those functions in
Boost.

 As I've seen an overloaded "get_pointer" function returns a raw pointer
from a generic pointer, and I've made the same with the relative offset_ptr
from Boost Shmem.

 But to treat all types of pointer generically I needed to obtain from a
generic pointer a pointer to other type of the same class.

 I've solved this defining in my smart pointer an internal type following
the STL allocator rebind mechanism:

/*!Obtains offset_ptr <OtherType> from offset_ptr*/
template<typename OtherType>
struct pointer_to_type
{
   typedef my_smart_ptr<OtherType> type;
};

and the structs:

template <typename Ptr, typename NewValueType>
struct pointer_to_other
{
   typedef typename Ptr::template
   pointer_to_type<NewValueType>::type type;
};

template <typename T, typename NewValueType>
struct pointer_to_other<T*, NewValueType>
{
   typedef NewValueType *type;
};

so that the user can generically write:

typedef pointer_to_other<NewType>(my_smart_ptr)::type new_pointer_t;

and obtain a pointer of the same class (smart pointer or raw pointer) to
other type.

My question is, do you find this mechanism useful to include it in boost in
a general way, and if so, is there another way of doing this without
modifying existing classes (for example, if I want use these mechanism with
shared_ptr)?

Regards,

/Ion


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk