I'm trying to write a wrapper for a certain c api, specifically around a pair of functions that take this form :
int add_list_a(ablist *l, int id);
int add_list_b(ablist *l, long id);
What I'd like to do is hide the difference between those two and have something like this :
class List
{
void addAB(AB *ab);
};
class AB {};
class A: public AB {int id;};
class B: public AB {long id;};
I'd rather not directly put pointers directly in the public interface as that would make the interface depend on boost::shared_ptr. (Can't use modern c++)
I then realized it was difficult to define classes that didn't need to be wrapped in a smart pointer and did not expose some internals for this to work well.
I can do something like this :
class List
{
private:
ablist *l;
public:
void addAB(AB ab) {
ab.addToList(l);
}
};
class AB {
private:
boost::shared_ptr<InternalAB> ab;
public:
void addToList(list *l) {
ab->addToList(l);
}
};
class InternalAB { virtual void addToList(list *l) = 0; }
with these types internally :
class InternalA: public InternalAB {
public:
int id;
void addToList(list *l)
{
add_list_a(l, id);
}
};
class InternalB: public InternalAB {
public:
long id;
void addToList(list *l)
{
add_list_b(l, id);
}
};
but it's pretty convoluted and still exposes addToList().
A and B are created from static functions, so their initialization is not a problem, they have a lot of common code in my case which is why I'd like to keep a common type between them.
Is there a better way to do this ? I might have missed something entirely but it's kind of a specific case and I can't find anything similar