/*
* Copyright (c) 2010-2019 Belledonne Communications SARL.
*
* This file is part of Liblinphone.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#ifndef _L_GENERAL_H_
#define _L_GENERAL_H_
#ifdef __cplusplus
#include
#include
#endif
// =============================================================================
// -----------------------------------------------------------------------------
// Namespace.
// -----------------------------------------------------------------------------
#ifdef __cplusplus
#define LINPHONE_BEGIN_NAMESPACE namespace LinphonePrivate {
#define LINPHONE_END_NAMESPACE }
#else
#define LINPHONE_BEGIN_NAMESPACE
#define LINPHONE_END_NAMESPACE
#endif
// -----------------------------------------------------------------------------
LINPHONE_BEGIN_NAMESPACE
// -----------------------------------------------------------------------------
// Export.
// -----------------------------------------------------------------------------
#ifndef LINPHONE_PUBLIC
#if defined(_MSC_VER)
#ifdef LINPHONE_STATIC
#define LINPHONE_PUBLIC
#else
#ifdef LINPHONE_EXPORTS
#define LINPHONE_PUBLIC __declspec(dllexport)
#else
#define LINPHONE_PUBLIC __declspec(dllimport)
#endif
#endif
#else
#define LINPHONE_PUBLIC
#endif
#endif
#ifndef LINPHONE_DEPRECATED
#if defined(_MSC_VER)
#define LINPHONE_DEPRECATED __declspec(deprecated)
#else
#define LINPHONE_DEPRECATED __attribute__((deprecated))
#endif
#endif
// -----------------------------------------------------------------------------
#ifdef __cplusplus
// -----------------------------------------------------------------------------
// Debug.
// -----------------------------------------------------------------------------
void lAssert (const char *condition, const char *file, int line);
#ifdef DEBUG
#define L_ASSERT(CONDITION) ((CONDITION) ? static_cast(0) : LinphonePrivate::lAssert(#CONDITION, __FILE__, __LINE__))
#else
#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)
#else
#define L_LIKELY(EXPRESSION) EXPRESSION
#define L_UNLIKELY(EXPRESSION) EXPRESSION
#endif
// -----------------------------------------------------------------------------
// Misc.
// -----------------------------------------------------------------------------
// Define an integer version like: 0xXXYYZZ, XX=MAJOR, YY=MINOR, and ZZ=PATCH.
constexpr unsigned int makeVersion (unsigned int major, unsigned int minor, unsigned int patch) {
return ((major << 16) | (minor << 8) | patch);
}
// Not available in C++11...
template
std::unique_ptr makeUnique(Args && ...args) {
return std::unique_ptr(new T(std::forward(args)...));
}
#define L_AUTO_RETURN(VALUE) -> decltype(VALUE) { return VALUE; }
// -----------------------------------------------------------------------------
// Class tools.
// -----------------------------------------------------------------------------
#define L_DISABLE_COPY(CLASS) \
CLASS (const CLASS &) = delete; \
CLASS &operator= (const CLASS &) = delete;
// -----------------------------------------------------------------------------
// PImpl tools.
// -----------------------------------------------------------------------------
class BaseObject;
class BaseObjectPrivate;
class ClonableObject;
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_DECLARE_PRIVATE(CLASS) \
inline CLASS ## Private *getPrivate () { \
L_INTERNAL_CHECK_OBJECT_INHERITANCE(CLASS); \
using TypeAncestor = LinphonePrivate::Private::BetterPrivateAncestor; \
return reinterpret_cast(TypeAncestor::mPrivate); \
} \
inline const CLASS ## Private *getPrivate () const { \
L_INTERNAL_CHECK_OBJECT_INHERITANCE(CLASS); \
using TypeAncestor = LinphonePrivate::Private::BetterPrivateAncestor; \
return reinterpret_cast(TypeAncestor::mPrivate); \
} \
friend class CLASS ## Private; \
friend class Wrapper;
// Allows access to private internal data.
// Gives a control to C Wrapper.
#ifndef LINPHONE_TESTER
#define L_DECLARE_PRIVATE(CLASS) L_INTERNAL_DECLARE_PRIVATE(CLASS)
#else
#define L_DECLARE_PRIVATE(CLASS) \
L_INTERNAL_DECLARE_PRIVATE(CLASS) \
friend class Tester;
#endif
#define L_DECLARE_PUBLIC(CLASS) \
CLASS *getPublic () { \
return LinphonePrivate::Private::getPublicHelper(mPublic, this); \
} \
const CLASS *getPublic () const { \
return LinphonePrivate::Private::getPublicHelper(mPublic, this); \
} \
friend class CLASS;
// Get Private data.
#define L_D() decltype(getPrivate()) const d = getPrivate();
// Get Public data.
#define L_Q() decltype(getPublic()) const q = getPublic();
// Get Private data of class in a multiple inheritance case.
#define L_D_T(CLASS, NAME) \
auto const NAME = static_cast< \
LinphonePrivate::Private::AddConstMirror< \
std::remove_reference::type, \
CLASS ## Private \
>::type * \
>(CLASS::mPrivate);
// Get Public data of class in a multiple inheritance case.
#define L_Q_T(CLASS, NAME) \
auto const NAME = static_cast< \
LinphonePrivate::Private::AddConstMirror< \
std::remove_reference::type, \
CLASS \
>::type * \
>(getPublic());
// -----------------------------------------------------------------------------
// Macro.
// -----------------------------------------------------------------------------
#define EPHEMERAL_MESSAGE_TASKS_MAX_NB 10
// -----------------------------------------------------------------------------
// Overload.
// -----------------------------------------------------------------------------
#define L_OVERRIDE_SHARED_FROM_THIS(CLASS) \
inline std::shared_ptr getSharedFromThis () { \
return std::static_pointer_cast(Object::getSharedFromThis()); \
} \
inline std::shared_ptr getSharedFromThis () const { \
return std::static_pointer_cast(Object::getSharedFromThis()); \
}
namespace Private {
template
struct ResolveMemberFunctionOverload {
template
constexpr auto operator() (Ret (Obj::*func)(Args...)) const L_AUTO_RETURN(func);
};
template
struct ResolveConstMemberFunctionOverload {
template
constexpr auto operator() (Ret (Obj::*func)(Args...) const) const L_AUTO_RETURN(func);
};
template
struct ResolveOverload : ResolveMemberFunctionOverload, ResolveConstMemberFunctionOverload {
using ResolveMemberFunctionOverload::operator();
using ResolveConstMemberFunctionOverload::operator();
template
constexpr auto operator() (Ret (*func)(Args...)) const L_AUTO_RETURN(func);
};
}
// Useful to select a specific overloaded function. (Avoid usage of static_cast.)
template
using resolveOverload = Private::ResolveOverload;
// -----------------------------------------------------------------------------
// Math.
// -----------------------------------------------------------------------------
// Get the length of one integer.
constexpr int getIntLength (int n) {
return n < 0 ? 1 + getIntLength(-n) : (n < 10 ? 1 : 1 + getIntLength(n / 10));
}
namespace Private {
constexpr int pow10Impl (int n, int acc) {
return n == 0 ? acc : pow10Impl(n - 1, acc * 10);
}
}
template
constexpr T abs (const T &value) {
return value < 0 ? -value : value;
}
constexpr int pow10 (int n) {
return (n < 0 ? -1 : +1) * Private::pow10Impl(abs(n), 1);
}
// Returns the sum of n elements.
constexpr int sums () {
return 0;
}
template
constexpr int sums (T i, Args... args) {
return i + sums(args...);
}
// -----------------------------------------------------------------------------
// Wrapper public.
// -----------------------------------------------------------------------------
#define L_DECL_C_STRUCT(STRUCT) typedef struct _ ## STRUCT STRUCT;
#define L_DECL_C_STRUCT_PREFIX_LESS(STRUCT) typedef struct STRUCT STRUCT;
#endif // ifdef __cplusplus
LINPHONE_END_NAMESPACE
#endif // ifndef _L_GENERAL_H_