From c8648d0f794a5ddc98782e48b922d8d8d5414d54 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Tue, 13 Feb 2018 12:08:04 +0100 Subject: [PATCH] fix(General): remove macros when possible and clean code --- include/linphone/utils/general.h | 130 +++++++++++++++++-------------- 1 file changed, 70 insertions(+), 60 deletions(-) diff --git a/include/linphone/utils/general.h b/include/linphone/utils/general.h index 5b2260446..5a4c3b15f 100644 --- a/include/linphone/utils/general.h +++ b/include/linphone/utils/general.h @@ -27,6 +27,10 @@ // ============================================================================= +// ----------------------------------------------------------------------------- +// Namespace. +// ----------------------------------------------------------------------------- + #ifdef __cplusplus #define LINPHONE_BEGIN_NAMESPACE namespace LinphonePrivate { #define LINPHONE_END_NAMESPACE } @@ -111,7 +115,15 @@ std::unique_ptr 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 + using BetterPrivateAncestor = typename std::conditional< + std::is_base_of::value, + BaseObject, + typename std::conditional< + std::is_base_of::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(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(*it); + } + + template + struct AddConstMirror { + typedef U type; + }; + + template + struct AddConstMirror { + typedef typename std::add_const::type type; + }; +} + #define L_INTERNAL_CHECK_OBJECT_INHERITANCE(CLASS) \ static_assert( \ !(std::is_base_of::value && std::is_base_of::value), \ "Multiple inheritance between BaseObject and ClonableObject is not allowed." \ ); -#define L_INTERNAL_GET_BETTER_PRIVATE_ANCESTOR(CLASS) \ - std::conditional< \ - std::is_base_of::value, \ - BaseObject, \ - std::conditional< \ - std::is_base_of::value, \ - ClonableObject, \ - CLASS \ - >::type \ - >::type - #define L_INTERNAL_DECLARE_PRIVATE(CLASS) \ inline CLASS ## Private *getPrivate () { \ L_INTERNAL_CHECK_OBJECT_INHERITANCE(CLASS); \ return reinterpret_cast( \ - L_INTERNAL_GET_BETTER_PRIVATE_ANCESTOR(CLASS)::mPrivate \ + LinphonePrivate::Private::BetterPrivateAncestor::mPrivate \ ); \ } \ inline const CLASS ## Private *getPrivate () const { \ L_INTERNAL_CHECK_OBJECT_INHERITANCE(CLASS); \ return reinterpret_cast( \ - L_INTERNAL_GET_BETTER_PRIVATE_ANCESTOR(CLASS)::mPrivate \ + LinphonePrivate::Private::BetterPrivateAncestor::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(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(*it); -} - #define L_DECLARE_PUBLIC(CLASS) \ - inline CLASS *getPublic () { \ - return getPublicHelper(mPublic, this); \ + CLASS *getPublic () { \ + return LinphonePrivate::Private::getPublicHelper(mPublic, this); \ } \ - inline const CLASS *getPublic () const { \ - return getPublicHelper(mPublic, this); \ + const CLASS *getPublic () const { \ + return LinphonePrivate::Private::getPublicHelper(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 -struct AddConstMirror { - typedef U type; -}; - -template -struct AddConstMirror { - typedef typename std::add_const::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::type, \ CLASS ## Private \ >::type * \ @@ -227,12 +237,16 @@ struct AddConstMirror { // 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::type, \ CLASS \ >::type * \ >(getPublic()); +// ----------------------------------------------------------------------------- +// Overload. +// ----------------------------------------------------------------------------- + #define L_OVERRIDE_SHARED_FROM_THIS(CLASS) \ inline std::shared_ptr getSharedFromThis () { \ return std::static_pointer_cast(Object::getSharedFromThis()); \ @@ -241,10 +255,6 @@ struct AddConstMirror { return std::static_pointer_cast(Object::getSharedFromThis()); \ } -// ----------------------------------------------------------------------------- -// Overload. -// ----------------------------------------------------------------------------- - namespace Private { template struct ResolveMemberFunctionOverload {