|
Boost : |
Subject: Re: [boost] pimpl library proposal
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2010-12-03 13:36:08
Michael Bailey wrote:
> my_object.hpp
>
> class my_object
> : public boost::pimpl<class my_object_impl>
> {
> public:
> my_object() {}
> void do_something();
> int do_other_thing( double ) const;
> void lots_of_args( int, int, int, int );
> };
>
>
> my_object.cpp
>
> class my_object_impl
> {
> public:
> void do_something() {...}
> int do_other_thing( double a1 ) const {...}
> void lots_of_args( int, int, int, int ) {...}
> private:
> ...
> };
>
> BOOST_PIMPL_CTOR( my_object );
> BOOST_PIMPL_METHOD( 0, my_object, do_something );
> BOOST_PIMPL_METHOD( 1, my_object, do_other_thing, const );
> BOOST_PIMPL_METHOD( 4, my_object, lots_of_args );
I would write:
my_object.hpp
class my_object_impl;
class my_object
{
my_object_impl* pimpl;
public:
my_object() {}
~my_object();
void do_something();
int do_other_thing( double ) const;
void lots_of_args( int, int, int, int );
};
my_object.cpp
class my_object_impl
{
public:
void do_something() {...}
int do_other_thing( double a1 ) const {...}
void lots_of_args( int, int, int, int ) {...}
private:
...
};
my_object::my_object(): pimpl(new my_object_impl) {}
my_object::~my_object() { delete pimpl; }
void my_object::do_something() { pimpl->do_something(); }
int my_object::do_other_thing(double d) const { return
pimpl->do_other_thing(d); }
void my_object::lots_of_args(int a, int b, int c, int d) {
pimpl->lots_of_args(a,b,c,d); }
which really isn't much different in terms of the amount of typing,
except for the forwarding. Can you show what your BOOST_PIMPL_METHOD
expands to? I can't immediately guess how you manage to not tell it
all the argument and return types.
Re using the shared_ptr, this mainly gains the safety of otherwise
forgetting to implement the dtor, and has speed and space
disadvantages. I'm not convinced that is really useful, and so I would
decouple it from the concise forwarding feature.
Generally, I think that implementing pimpls falls more into the
category of stuff that's best done with some sort of tool, e.g. a
"pimpl wizzard" in your IDE, rather than a library.
Regards, Phil.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk