refs #413 Stream operator template of CLogPattern supports any class with a toQString method, not just CValueObject subclasses.

This commit is contained in:
Mathew Sutcliffe
2015-05-03 15:37:35 +01:00
parent 6b40cde862
commit 9f98943c1f

View File

@@ -24,6 +24,25 @@
namespace BlackMisc
{
/*!
* Trait to detect whether T contains a member toQString.
*/
template <typename T>
class HasToQString
{
// http://en.wikibooks.org/wiki/More_C++_Idioms/Member_Detector
struct Fallback { int toQString; };
template <int Fallback:: *> struct int_t { typedef int type; };
template <typename U, bool = std::is_class<U>::value> struct Derived : public U, public Fallback {};
template <typename U> struct Derived<U, false> : public Fallback {};
template <typename U> static char test(typename int_t<&Derived<U>::toQString>::type);
template <typename U> static int test(...);
public:
//! True if T contains a member toQString.
static const bool value = sizeof(test<T>(0)) > 1;
};
/*!
* Helper with static methods for dealing with metadata embedded in log message category strings.
*
@@ -138,7 +157,7 @@ namespace BlackMisc
CLogMessage &operator <<(QChar v) { return arg(v); }
CLogMessage &operator <<(char v) { return arg(QChar(v)); }
CLogMessage &operator <<(double v) { return arg(QString::number(v)); }
template <class T, class = typename std::enable_if<IsValueObject<T>::value>::type>
template <class T, class = typename std::enable_if<HasToQString<T>::value>::type>
CLogMessage &operator <<(const T &v) { return arg(v.toQString()); }
//! @}