Re: Choose between class implementations at compiletime
"Christoph Mathys" <cmathys@gmail.com> wrote in message
news:24a9e72f-9f2a-4974-9561-6b75c6c21193@g23g2000yqh.googlegroups.com...
Hello!
Well, nothing too spectacular, but I'd still be interested in how one
solves this problem in a clean way: I need to a implement a class
which uses different APIs and provides a uniform interface (APIs are
fixed at compiletime). I can't use an ABC directly because the class
is only used as value type. But I'd still like to have an interface
common to all classes, defined at a single place.
[...]
Perhaps you could try something like:
<pseudo-code typed in newsreader>
________________________________________________________________
// your_lib_windows.hpp
#include <windows.h>
namespace your_lib_windows
{
class mutex
{
CRITICAL_SECTION m_mutex;
public:
void lock() throw() {...}
void unlock() throw() {...}
};
}
namespace your_lib_os = your_lib_windows;
// your_lib_posix.hpp
#include <pthread.h>
namespace your_lib_posix
{
class mutex
{
pthread_mutex_t m_mutex;
public:
void lock() throw() {...}
void unlock() throw() {...}
};
}
namespace your_lib_os = your_lib_posix;
// your_lib.hpp
#if defined (WINDOWS_PLATFORM)
# include "your_lib_windows.hpp"
#elif defined (POSIX_PLATFORM)
# include "your_lib_posix.hpp"
#else
# error Platform Not Supported!
#endif
namespace your_lib
{
namespace os = your_lib_os;
}
// main.cpp
#include "your_lib.hpp"
int main()
{
{
your_lib::os::mutex mtx;
mtx.lock();
mtx.unlock();
}
return 0;
}
________________________________________________________________
;^)