Commit 9604156a authored by Ronan's avatar Ronan

feat(utils): add a toString function for enums

parent 5394f91a
......@@ -66,10 +66,10 @@ LINPHONE_BEGIN_NAMESPACE
enum class NAME { \
VALUES(L_DECLARE_ENUM_VALUE) \
}; \
friend constexpr const char *getResolvedEnumNameAsString (NAME) { \
friend constexpr const char *getEnumNameAsString (NAME) { \
return #NAME; \
} \
friend const char *getResolvedEnumValueAsString (NAME value) { \
friend const char *getEnumValueAsString (const NAME &value) { \
switch (value) { \
L_APPLY_WITHOUT_COMMA(L_DECLARE_ENUM_VALUE_STR_CASE, NAME, L_GET_ENUM_VALUE_NAMES(VALUES)) \
} \
......@@ -77,14 +77,12 @@ LINPHONE_BEGIN_NAMESPACE
}
template<typename T>
inline const char *getEnumNameAsString () {
return getResolvedEnumNameAsString(T());
}
inline char getEnumValueAsString (const T &) { return 0; }
template<typename T>
inline const char *getEnumValueAsString (T value) {
return getResolvedEnumValueAsString(value);
}
struct IsDefinedEnum {
enum { value = sizeof(getEnumValueAsString(std::declval<T>())) == sizeof(const char *) };
};
#endif
......
......@@ -25,7 +25,7 @@
#include <string>
#include <vector>
#include "linphone/utils/general.h"
#include "linphone/utils/enum-generator.h"
// =============================================================================
......@@ -81,6 +81,10 @@ namespace Utils {
LINPHONE_PUBLIC std::string toString (long double val);
LINPHONE_PUBLIC std::string toString (const void *val);
LINPHONE_PUBLIC
template<typename T, typename = typename std::enable_if<IsDefinedEnum<T>::value, T>::type>
inline std::string toString (const T &val) { return getEnumValueAsString(val); }
LINPHONE_PUBLIC int stoi (const std::string &str, size_t *idx = 0, int base = 10);
LINPHONE_PUBLIC long long stoll (const std::string &str, size_t *idx = 0, int base = 10);
LINPHONE_PUBLIC unsigned long long stoull (const std::string &str, size_t *idx = 0, int base = 10);
......
......@@ -81,8 +81,8 @@ void ChatMessagePrivate::setState (ChatMessage::State s, bool force) {
)
return;
lInfo() << "Chat message " << this << ": moving from " << getEnumValueAsString(state) <<
" to " << getEnumValueAsString(s);
lInfo() << "Chat message " << this << ": moving from " << Utils::toString(state) <<
" to " << Utils::toString(s);
state = s;
LinphoneChatMessage *msg = L_GET_C_BACK_PTR(q);
......
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