In article
<38f73473-466e-4baf-8358-add3121b2ae0@[EMAIL PROTECTED]
>,
Fejimush <grahamreitz@[EMAIL PROTECTED]
> wrote:
> What are good strategies for selecting, either at run-time or compile
> time, various pimpl'ed implementations? Also, retaining the ability
> to switch implementations without recompiling.
>
without recompiling most likely runtime polymorhism. If you don't
want to add classes derived from the base at will the fact they are all
derived from a common base can also be hidden by not exposing them
to users. If you need to add pimples at will, I think its a design
problem. If you have two or three policies to be choosen at run time
provide a function boost::shared_ptr<Pimpl> create_pimple(/* some arg
to say which */); hidden from user and use it to get the proper derived
class ptr in the shared_ptr<Pimple> I don't see the problem,
// foo header foo.hpp
#include <boost/shared_ptr.hpp>
class Foo
{
class Pimpl;
boost::shared_ptr<Pimpl> pimpl;
public:
explicit Foo(int n=1);
void something();
// ...
};
// implementation
#include "foo.hpp"
struct Foo::Pimple
{
// base class with virt funcs.
};
namespace
{
struct Pimpl_1:Pimpl { /**/};
struct Pimpl_2:Pimpl {/**/};
// ,,,
}
Foo:Foo(int n)
{
switch(n)
{
case 1:pimpl = boost::shared_ptr<Pimpl_1>(new Pimp_1); break;
case 2:pimpl = boost::shared_ptr<Pimpl_2>(new Pimpl_2);break;
// ...
default:pimpl = boost::shared_ptr<Pimpl_1>(new Pimpl_1); break;
}
};
void Foo::something()
{
access data thru pimpl's virt. funcs.
}
// ...
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|