T
Thomas J. Gritzan
Hi!
I wrote (yet another) linked_ptr implementation, based on discussions
about boost's linked_ptr (I can't find the source code) and other
discussions about smart pointers, mainly by Andrei Alexandrescu.
Recently there were mentioned linked_ptrs in c.l.c++ and *.moderated, so
I decided to put out the source code.
The source can be found here:
http://pastebin.com/m5f222fa9
** Introduction **
All the other linked_ptr implementations on the web don't support a
custom deleter, and don't support ptr<derived> to ptr<base> conversions.
This implementation is intended as a drop-in replacement for shared_ptr
for some uses, so it tries to mimic its interface. It supports implicit
derived to base conversions, explicit
{static,const,dynamic}_pointer_cast, a custom deleter and a form of
policy class.
Using the policy class, two extra member functions are injected into the
linked_ptr for array types: get(index) and operator[], while op* and
op-> are disabled. So you can have linked_ptr<type[]>. For array types,
the class supports aliasing, so you can do pointer arithmetic on the
smart pointer (++, --, +=, -=, +, -).
Feel free to comment on design, sense and functionality.
** Unsolved problem **
There's one issue I'd like to have a solution for. You can set a custom
deleter on class basis like this:
template <>
struct linked_ptr_deleter<SomeClass>
{
void operator()(SomeClass* ptr) const { ptr->release(); }
};
But how do I set a custom deleter for all classes which are derived from
SomeClass without specializing the template for every such class?
I wrote (yet another) linked_ptr implementation, based on discussions
about boost's linked_ptr (I can't find the source code) and other
discussions about smart pointers, mainly by Andrei Alexandrescu.
Recently there were mentioned linked_ptrs in c.l.c++ and *.moderated, so
I decided to put out the source code.
The source can be found here:
http://pastebin.com/m5f222fa9
** Introduction **
All the other linked_ptr implementations on the web don't support a
custom deleter, and don't support ptr<derived> to ptr<base> conversions.
This implementation is intended as a drop-in replacement for shared_ptr
for some uses, so it tries to mimic its interface. It supports implicit
derived to base conversions, explicit
{static,const,dynamic}_pointer_cast, a custom deleter and a form of
policy class.
Using the policy class, two extra member functions are injected into the
linked_ptr for array types: get(index) and operator[], while op* and
op-> are disabled. So you can have linked_ptr<type[]>. For array types,
the class supports aliasing, so you can do pointer arithmetic on the
smart pointer (++, --, +=, -=, +, -).
Feel free to comment on design, sense and functionality.
** Unsolved problem **
There's one issue I'd like to have a solution for. You can set a custom
deleter on class basis like this:
template <>
struct linked_ptr_deleter<SomeClass>
{
void operator()(SomeClass* ptr) const { ptr->release(); }
};
But how do I set a custom deleter for all classes which are derived from
SomeClass without specializing the template for every such class?