I'm moving part of a threading library from C++03 to C++11, yet I'd like to keep it compatible with Boost, as an option for users who don't have C++11 compilers. Since C++11 STL and Boost 1.54 basically have the same interface (at least for the part I'm interested in), it would be nice to have something like this:
#ifdef USING_BOOST
#define NAMESPACE boost
#else
#define NAMESPACE std
#endif
typedef NAMESPACE::mutex MyLibrary::Mutex;
typedef NAMESPACE::condition_variable MyLibrary::ConditionVariable;
...
The problem comes with templates, e.g. unique_lock. A better approach could be this:
#define TYPEDEF(WHAT, AS) typedef LIBPREFIX::WHAT AS
#define TYPEDEF_T(WHAT, AS) \
template <typename T> \
struct AS \
{ \
typedef LIBPREFIX::WHAT<T> type; \
};
TYPEDEF( thread, Thread );
TYPEDEF( mutex, Mutex );
TYPEDEF( condition_variable, ConditionVariable );
TYPEDEF_T( lock_guard, LockGuard );
TYPEDEF_T( unique_lock, UniqueLock );
TYPEDEF_T( shared_ptr, SharedPtr );
However I'll have to use it this way:
LockGuard<Mutex>::type lock(...);
E.g. I don't like having to write ::type all the times.
Also, I'd like to override the make_shared function, i.e. mapping it as follows:
MyLibrary::MakeShared<T> --> NAMESPACE::make_shared<T>
where NAMESPACE is either 'std' or 'boost'. I guess that something like
#define USING(WHAT) using NAMESPACE::WHAT
USING( make_shared );
is not a viable option... right?
Overriding the entire boost namespace is not a solution, as other parts of Boost could be used in the code. What would be the best way to achieve this?
Thanks!