Skip to content

Commit

Permalink
Fix compilation on VS2010 (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergiusTheBest committed Apr 26, 2022
1 parent 130801c commit e2650b8
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions include/plog/Record.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,43 @@ namespace plog

template<class T>
struct enableIf<true, T> { typedef T type; };

struct No { char a[1]; };
struct Yes { char a[2]; };

template <class From, class To>
struct isConvertible
{
// `+ sizeof(U*)` is required for GCC 4.5-4.7
template<class U>
static typename enableIf<!!(sizeof(static_cast<To>(meta::declval<U>())) + sizeof(U*)), Yes>::type test(int);

template<class U>
static No test(...);

enum { value = sizeof(test<From>(0)) == sizeof(Yes) };
};

template <class T>
struct isConvertibleToNString : isConvertible<T, std::basic_string<util::nchar> > {};

template <class T>
struct isContainer
{
template<class U>
static typename meta::enableIf<!!(sizeof(
#if defined(_MSC_VER) && _MSC_VER < 1700 // MSVC 2010 doesn't understand `typename T::const_iterator`
meta::declval<U>().begin()) + sizeof(meta::declval<U>().end()
#else
typename U::const_iterator
#endif
)), Yes>::type test(int);

template<class U>
static No test(...);

enum { value = sizeof(test<T>(0)) == sizeof(Yes) };
};
}

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -88,20 +125,20 @@ namespace plog
}

#if defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ > 4 // skip for GCC < 4.5 due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=38600
// Print data that can be casted to `std::basic_string`. `+ sizeof(T*)` is required for GCC 4.5-4.7.
template<typename T>
inline typename meta::enableIf<!!(sizeof(static_cast<std::basic_string<util::nchar> >(meta::declval<T>())) + sizeof(T*)), void>::type operator<<(util::nostringstream& stream, const T& data)
// Print data that can be casted to `std::basic_string`.
template<class T>
inline typename meta::enableIf<meta::isConvertibleToNString<T>::value, void>::type operator<<(util::nostringstream& stream, const T& data)
{
plog::detail::operator<<(stream, static_cast<std::basic_string<util::nchar> >(data));
}

// Print std containers
template<class Container>
inline typename meta::enableIf<!!(sizeof(typename Container::const_iterator)), void>::type operator<<(util::nostringstream& stream, const Container& data)
template<class T>
inline typename meta::enableIf<meta::isContainer<T>::value && !meta::isConvertibleToNString<T>::value, void>::type operator<<(util::nostringstream& stream, const T& data)
{
stream << "[";

for (typename Container::const_iterator it = data.begin(); it != data.end();)
for (typename T::const_iterator it = data.begin(); it != data.end();)
{
stream << *it;

Expand Down Expand Up @@ -129,12 +166,12 @@ namespace plog
namespace meta
{
template<class T, class Stream>
inline char operator<<(Stream&, const T&);
inline No operator<<(Stream&, const T&);

template <class T, class Stream>
struct isStreamable
{
enum { value = sizeof(operator<<(meta::declval<Stream>(), meta::declval<const T>())) != sizeof(char) };
enum { value = sizeof(operator<<(meta::declval<Stream>(), meta::declval<const T>())) != sizeof(No) };
};

template <class Stream>
Expand Down

0 comments on commit e2650b8

Please sign in to comment.