Original Post
We're changing our software to run on multiple platforms(by platform I don't mean OS, but a software that our software runs on top of) that have very similar API's. I'm looking for a way to implement an implicit cast between pointers.
Our software originally looked like:
void f(A* x) {};
//........
B* y;
f(y);
Classes A and B are platform specific. The above code used to work since we had:
class A {};
class B : public A {};
In our new code, we cannot use classes A and B (since they are platform specific), and trying to run the code on some other platform wouldn't work since the new platform doesn't have classes A and B. Instead, it has classes C and D that basically do the same thing, but have a different name.
Our solution (which has been very much debated) is to make a wrapper over the 2 platforms like so:
#ifdef PLATFORM1
class xA : protected A
#else
class xA : protected C
#endif
{}
#ifdef PLATFORM1
class xB : protected B
#else
class xB : protected D
#endif
{}
This would be the general architecture of the wrapper.
In our code, function f becomes:
void f(xA* x) {};
//.......
xB* y;
f(y);
This obviously doesn't work any more, since xB is not derived from xA. What does work is having an explicit cast:
f((xA*)y);
However, we don't want to write this explicit cast everywhere in our code (very very much work). Instead i'm looking for a way to make this cast be called automatically.
Conversion constructors or cast operators would only work if the function were taking an argument of the respective type, not a pointer to an instance of the class. Overriding the function f to take an argument of type xB would be even more work than explicit casting since there are a lot of classes and even more functions. One other suggestion i found was making a function
f(B* x). This in principle works since both xA and xB are derived from B, however the function would only make sense on the first platform.
Any suggestions? Please reply only if you know how to do this, non-constructive criticism is not helpful. We have really worked hard on our architecture and it DOES work, we just want to get rid of the extra overhead.
Our software originally looked like:
void f(A* x) {};
//........
B* y;
f(y);
Classes A and B are platform specific. The above code used to work since we had:
class A {};
class B : public A {};
In our new code, we cannot use classes A and B (since they are platform specific), and trying to run the code on some other platform wouldn't work since the new platform doesn't have classes A and B. Instead, it has classes C and D that basically do the same thing, but have a different name.
Our solution (which has been very much debated) is to make a wrapper over the 2 platforms like so:
#ifdef PLATFORM1
class xA : protected A
#else
class xA : protected C
#endif
{}
#ifdef PLATFORM1
class xB : protected B
#else
class xB : protected D
#endif
{}
This would be the general architecture of the wrapper.
In our code, function f becomes:
void f(xA* x) {};
//.......
xB* y;
f(y);
This obviously doesn't work any more, since xB is not derived from xA. What does work is having an explicit cast:
f((xA*)y);
However, we don't want to write this explicit cast everywhere in our code (very very much work). Instead i'm looking for a way to make this cast be called automatically.
Conversion constructors or cast operators would only work if the function were taking an argument of the respective type, not a pointer to an instance of the class. Overriding the function f to take an argument of type xB would be even more work than explicit casting since there are a lot of classes and even more functions. One other suggestion i found was making a function
f(B* x). This in principle works since both xA and xB are derived from B, however the function would only make sense on the first platform.
Any suggestions? Please reply only if you know how to do this, non-constructive criticism is not helpful. We have really worked hard on our architecture and it DOES work, we just want to get rid of the extra overhead.