Making a class more generic [part 2]

S

Simon Elliott

I have a class (actually the same one described in part 1) which
implements a state machine. The state machine is inside a pimpl, and
looks a bit like this:

class foo::fooImpl
{
public:
foo(void);
void StartBloggs(const BloggsType& bloggsStuff)
{
bloggsStuff_ = bloggsStuff;
state_ = 1;
}
void StartJimmy(const JimmyType& jimmyStuff)
{
jimmyStuff_ = jimmyStuff;
state_ = 2;
}
void Exec(void)
{
switch (state_)
{
case 1:
execBloggs();
break;
case 2:
execJimmy();
break;
}
}
private:
int state_;
BloggsType bloggsStuff_;
JimmyType jimmyStuff_;
void execBloggs();
void execJimmy();
};

As before, I now want to make this code a bit more generic. The problem
I now have is that some callers may want to only use the Jimmy code, and
others only want to use the Bloggs code. But anyone who links my class
gets the overhead of having both Bloggs and Jimmy, whether needed or
not. (In my example above, I could probably dynamically allocate the
state data bloggsStuff_ and jimmyStuff_, but I'm more interested in not
linking the execBloggs() and execJimmy() methods.)

Is there any way of only linking the stuff the user of the class needs,
and still keeping the pimpl in some shape or form?
 
L

lilburne

Simon said:
Is there any way of only linking the stuff the user of the class needs,
and still keeping the pimpl in some shape or form?

Seperate the execBloggs(), and execJimmy() into seperate
classes. Have you class only see abstract bases. Clients
supply an implementation exec object contructed with an
appropriate object.

BloggsAbs.h
class BloggsExecAbs
{
public:
virtual void execBloggs() = 0;
};

bloggs_impl.h
class BloggsExecImpl : public BloggsAbs
{
public:
virtual void execBloggs();
};


foo.h
#include "BloggsAbs.h"
#include "JimmyAbs.h"

class foo
{
public:
void StartBloggs(BloggsExecAbs* bloggsExec) {
bloggsExec_ = bloggsStuff;
state_ = 1;
}
void StartJimmy(JimmyExecAbs* jimmyExec) {
jimmyExec_ = jimmyStuff;
state_ = 2;
}

void Exec(void) {
switch (state_) {
case 1:
bloggsExec_->execBloggs();
break;
case 2:
jimmyExec_->execJimmy();
break;
}
}
};
 
S

Simon Elliott

lilburne said:
Seperate the execBloggs(), and execJimmy() into seperate
classes. Have you class only see abstract bases. Clients
supply an implementation exec object contructed with an
appropriate object.

Thanks - an interesting idea. I could even have all the relevant local
state data encapsulated into the implementation object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top