From e3219d2ee9be3b9e5aa7bdeaa061f8811ffbcbd9 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Wed, 10 Jan 2018 12:00:13 +0100 Subject: [PATCH] feat(utils/general): provide a new L_RESOLVE_OVERLOAD macro to get a function pointer of overloaded functions set --- include/linphone/utils/general.h | 56 ++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/include/linphone/utils/general.h b/include/linphone/utils/general.h index a4dee6eeb..8f93f891b 100644 --- a/include/linphone/utils/general.h +++ b/include/linphone/utils/general.h @@ -38,6 +38,10 @@ LINPHONE_BEGIN_NAMESPACE +// ----------------------------------------------------------------------------- +// Export. +// ----------------------------------------------------------------------------- + #ifndef LINPHONE_PUBLIC #if defined(_MSC_VER) #ifdef LINPHONE_STATIC @@ -66,6 +70,10 @@ LINPHONE_BEGIN_NAMESPACE #ifdef __cplusplus +// ----------------------------------------------------------------------------- +// Debug. +// ----------------------------------------------------------------------------- + void l_assert (const char *condition, const char *file, int line); #ifdef DEBUG @@ -74,6 +82,10 @@ void l_assert (const char *condition, const char *file, int line); #define L_ASSERT(CONDITION) static_cast(false && (CONDITION)) #endif +// ----------------------------------------------------------------------------- +// Optimization. +// ----------------------------------------------------------------------------- + #ifndef _MSC_VER #define L_LIKELY(EXPRESSION) __builtin_expect(static_cast(EXPRESSION), true) #define L_UNLIKELY(EXPRESSION) __builtin_expect(static_cast(EXPRESSION), false) @@ -82,9 +94,17 @@ void l_assert (const char *condition, const char *file, int line); #define L_UNLIKELY(EXPRESSION) EXPRESSION #endif +// ----------------------------------------------------------------------------- +// Misc. +// ----------------------------------------------------------------------------- + // Define an integer version like: 0xXXYYZZ, XX=MAJOR, YY=MINOR, and ZZ=PATCH. #define L_VERSION(MAJOR, MINOR, PATCH) (((MAJOR) << 16) | ((MINOR) << 8) | (PATCH)) +// ----------------------------------------------------------------------------- +// Data access. +// ----------------------------------------------------------------------------- + class BaseObject; class BaseObjectPrivate; class ClonableObject; @@ -212,6 +232,42 @@ struct AddConstMirror { return std::static_pointer_cast(Object::getSharedFromThis()); \ } +// ----------------------------------------------------------------------------- +// Overload. +// ----------------------------------------------------------------------------- + +namespace Private { + template + struct ResolveMemberFunctionOverload { + template + constexpr auto operator() (Ret (Obj::*func)(Args...)) const -> decltype(func) { + return func; + } + }; + + template + struct ResolveConstMemberFunctionOverload { + template + constexpr auto operator() (Ret (Obj::*func)(Args...) const) const -> decltype(func) { + return func; + } + }; + + template + struct ResolveOverload : ResolveMemberFunctionOverload, ResolveConstMemberFunctionOverload { + using ResolveMemberFunctionOverload::operator(); + using ResolveConstMemberFunctionOverload::operator(); + + template + constexpr auto operator() (Ret (*func)(Args...)) const -> decltype(func) { + return func; + } + }; +} + +// Useful to select a specific overloaded function. (Avoid usage of static_cast.) +#define L_RESOLVE_OVERLOAD(ARGS) LinphonePrivate::Private::ResolveOverload + // ----------------------------------------------------------------------------- // Wrapper public. // -----------------------------------------------------------------------------