I'm trying to refactor some code but I'm bumping into some practical problems...
Basically I'm trying to do this:
class Interface
{
public:
virtual void f1() = 0;
virtual void f2() = 0;
};
class Helper : public Interface
{
public:
void formula()
{
f1();
f2();
f1();
}
};
class Implementation : public Interface
{
public:
void f1()
{
printf("f1()\n");
}
void f2()
{
printf("f2()\n");
}
};
class Concrete : public Implementation, public Helper
{
public:
void concrete()
{
f1();
formula();
f2();
}
};
If you try to compile this you'll get errors like: " ambiguous access of 'f1' in 'Concrete', could be the 'f1' in base 'Implementation::f1' or the 'f1' in base 'Interface::f1'". Clearly though, there's only one implementation of these functions so in theory it should work.Ok, it might not be clear what I'm trying to achieve here... In the actual code there will be multiple Helper classes, and multiple Concrete classes. The Helper classes actually merely store a 'sequence of function calls'. They are not supposed to know about any Implementation of these functions, so they just use the Interface. The Concrete classes are the actual end products. They are supposed to have access to the Implementation's functions, and can use the Helper's functions to fulfill their task.
Of course it could all work without this whole hierarchy, just by letting the Concrete classes use the Implementation directly and substituting the function call sequences of the helper classes. However, this would result in huge Concrete classes, and there wouldn't be any code reuse between different Concrete classes.
Still confused why I want to do this? It's intended to be used with SoftWire, my run-time code generator. The Implementation class would be SoftWire's CodeGenerator class, which implements -thousands- of so-called 'run-time intrinsics'. The Helper classes correspond to frequently used code sequences. For example, the SSE code to compute a dot product, or the implementation of a for loop.
Any ideas would be greatly appreciated, because it could mean an imporant step forward for SoftWire. The low-level things have been complete for a while now, but the high-level structure is seriously lacking in functionality to start creating something useful with it, in a convenient way. The goal is to provide 'libraries' of Helper classes so you can build a complete application making use of dynamic code generation, with a minimum (if any) knowledge of assembly language.
All the best,
Nick













