Im doing some maintenance work at the moment. I've got to take an old code-base, strip off an old API that it sends data to and instead, have it send data to a newly build API.
The code that uses the old API is quite convoluted in places, but it has been tested to be very solid.
I don't really want to edit any of this 'user code', instead I would just like to replace the api object with a different class and recompile.
The problem is that a large part of the old API's public interface are variables, not functions. For example, the old API publicly exposed a 2D array of widgets, whereas my new API doesn't.
api.pWidget->ptrs[x][y]->SetVisible(z);
As you can see below, the new API is a lot simpler than the old one - it just has functions and hides it's internal details:
class NewApi
{
public:
virtual bool SetVisible( int foo, int bar, bool state ) = 0;
};
struct OldApi
{
class Widget
{
public:
class Label
{
public:
void SetVisible(bool){}
};
Label *ptrs[MAX_FOO][MAX_BAR];
};
Widget* pWidget;
};
Obviously I can't just recompile the 'user' code using the new API, because the interfaces are completely different... which is why I came up with the following adaptor class.
This class looks exactly like the old API as far as the 'user' can tell (which means all of the existing code will continue to compile), but it actually uses the new API:
//WARNING - UGLY C++ CODES BE HERE!
struct ApiAdaptor
{
struct Widget
{
Widget* operator->() { return this; }
struct
{
struct FooIndex
{
FooIndex( int f ) : f(f) {} int f;
struct FooBarIndex
{
FooBarIndex( int f, int b ) : f(f), b(b) {} int f,b;
FooBarIndex* operator->() { return this; }
void SetVisible(bool s){ g_pNewApi->SetVisible(f,b, s); }
};
FooBarIndex operator[]( int bar ) { return FooBarIndex(f,bar); }
};
FooIndex operator[]( int foo ) { return FooIndex(foo); }
} ptrs;
} pWidget;
};
Is this adaptor too evil? Should I give in and just edit the 'user' files to use the new API (and maintain two branches of that code), or should I put up with the ugliness of the adaptor to lessen the overall amount of maintenance work?