Commit c8648d0f authored by Ronan's avatar Ronan

fix(General): remove macros when possible and clean code

parent a5e684ef
......@@ -27,6 +27,10 @@
// =============================================================================
// -----------------------------------------------------------------------------
// Namespace.
// -----------------------------------------------------------------------------
#ifdef __cplusplus
#define LINPHONE_BEGIN_NAMESPACE namespace LinphonePrivate {
#define LINPHONE_END_NAMESPACE }
......@@ -111,7 +115,15 @@ std::unique_ptr<T> makeUnique(Args && ...args) {
}
// -----------------------------------------------------------------------------
// Data access.
// Class tools.
// -----------------------------------------------------------------------------
#define L_DISABLE_COPY(CLASS) \
CLASS (const CLASS &) = delete; \
CLASS &operator= (const CLASS &) = delete;
// -----------------------------------------------------------------------------
// PImpl tools.
// -----------------------------------------------------------------------------
class BaseObject;
......@@ -121,34 +133,68 @@ class ClonableObjectPrivate;
class Object;
class ObjectPrivate;
namespace Private {
template<typename T>
using BetterPrivateAncestor = typename std::conditional<
std::is_base_of<BaseObject, T>::value,
BaseObject,
typename std::conditional<
std::is_base_of<ClonableObject, T>::value,
ClonableObject,
T
>::type
>::type;
// Generic public helper.
template<
typename R,
typename P,
typename C
>
constexpr R *getPublicHelper (P *object, const C *) {
return static_cast<R *>(object);
}
// Generic public helper. Deal with shared data.
template<
typename R,
typename P,
typename C
>
inline R *getPublicHelper (const P &objectSet, const C *) {
auto it = objectSet.cbegin();
L_ASSERT(it != objectSet.cend());
return static_cast<R *>(*it);
}
template<typename T, typename U>
struct AddConstMirror {
typedef U type;
};
template<typename T, typename U>
struct AddConstMirror<const T, U> {
typedef typename std::add_const<U>::type type;
};
}
#define L_INTERNAL_CHECK_OBJECT_INHERITANCE(CLASS) \
static_assert( \
!(std::is_base_of<BaseObject, CLASS>::value && std::is_base_of<ClonableObject, CLASS>::value), \
"Multiple inheritance between BaseObject and ClonableObject is not allowed." \
);
#define L_INTERNAL_GET_BETTER_PRIVATE_ANCESTOR(CLASS) \
std::conditional< \
std::is_base_of<BaseObject, CLASS>::value, \
BaseObject, \
std::conditional< \
std::is_base_of<ClonableObject, CLASS>::value, \
ClonableObject, \
CLASS \
>::type \
>::type
#define L_INTERNAL_DECLARE_PRIVATE(CLASS) \
inline CLASS ## Private *getPrivate () { \
L_INTERNAL_CHECK_OBJECT_INHERITANCE(CLASS); \
return reinterpret_cast<CLASS ## Private *>( \
L_INTERNAL_GET_BETTER_PRIVATE_ANCESTOR(CLASS)::mPrivate \
LinphonePrivate::Private::BetterPrivateAncestor<CLASS>::mPrivate \
); \
} \
inline const CLASS ## Private *getPrivate () const { \
L_INTERNAL_CHECK_OBJECT_INHERITANCE(CLASS); \
return reinterpret_cast<const CLASS ## Private *>( \
L_INTERNAL_GET_BETTER_PRIVATE_ANCESTOR(CLASS)::mPrivate \
LinphonePrivate::Private::BetterPrivateAncestor<CLASS>::mPrivate \
); \
} \
friend class CLASS ## Private; \
......@@ -164,61 +210,25 @@ class ObjectPrivate;
friend class Tester;
#endif
// Generic public helper.
template<
typename R,
typename P,
typename C
>
constexpr R *getPublicHelper (P *object, const C *) {
return static_cast<R *>(object);
}
// Generic public helper. Deal with shared data.
template<
typename R,
typename P,
typename C
>
inline R *getPublicHelper (const P &objectSet, const C *) {
auto it = objectSet.cbegin();
L_ASSERT(it != objectSet.cend());
return static_cast<R *>(*it);
}
#define L_DECLARE_PUBLIC(CLASS) \
inline CLASS *getPublic () { \
return getPublicHelper<CLASS>(mPublic, this); \
CLASS *getPublic () { \
return LinphonePrivate::Private::getPublicHelper<CLASS>(mPublic, this); \
} \
inline const CLASS *getPublic () const { \
return getPublicHelper<const CLASS>(mPublic, this); \
const CLASS *getPublic () const { \
return LinphonePrivate::Private::getPublicHelper<const CLASS>(mPublic, this); \
} \
friend class CLASS;
#define L_DISABLE_COPY(CLASS) \
CLASS (const CLASS &) = delete; \
CLASS &operator= (const CLASS &) = delete;
// Get Private data.
#define L_D() decltype(getPrivate()) const d = getPrivate();
// Get Public data.
#define L_Q() decltype(getPublic()) const q = getPublic();
template<typename T, typename U>
struct AddConstMirror {
typedef U type;
};
template<typename T, typename U>
struct AddConstMirror<const T, U> {
typedef typename std::add_const<U>::type type;
};
// Get Private data of class in a multiple inheritance case.
#define L_D_T(CLASS, NAME) \
auto const NAME = static_cast< \
AddConstMirror< \
LinphonePrivate::Private::AddConstMirror< \
std::remove_reference<decltype(*this)>::type, \
CLASS ## Private \
>::type * \
......@@ -227,12 +237,16 @@ struct AddConstMirror<const T, U> {
// Get Private data of class in a multiple inheritance case.
#define L_Q_T(CLASS, NAME) \
auto const NAME = static_cast< \
AddConstMirror< \
LinphonePrivate::Private::AddConstMirror< \
std::remove_reference<decltype(*this)>::type, \
CLASS \
>::type * \
>(getPublic());
// -----------------------------------------------------------------------------
// Overload.
// -----------------------------------------------------------------------------
#define L_OVERRIDE_SHARED_FROM_THIS(CLASS) \
inline std::shared_ptr<CLASS> getSharedFromThis () { \
return std::static_pointer_cast<CLASS>(Object::getSharedFromThis()); \
......@@ -241,10 +255,6 @@ struct AddConstMirror<const T, U> {
return std::static_pointer_cast<const CLASS>(Object::getSharedFromThis()); \
}
// -----------------------------------------------------------------------------
// Overload.
// -----------------------------------------------------------------------------
namespace Private {
template<typename... Args>
struct ResolveMemberFunctionOverload {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment