tlx
Loading...
Searching...
No Matches
wrap_unprintable.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * tlx/logger/wrap_unprintable.hpp
3 *
4 * Part of tlx - http://panthema.net/tlx
5 *
6 * Copyright (C) 2019 Timo Bingmann <tb@panthema.net>
7 *
8 * All rights reserved. Published under the Boost Software License, Version 1.0
9 ******************************************************************************/
10
11#ifndef TLX_LOGGER_WRAP_UNPRINTABLE_HEADER
12#define TLX_LOGGER_WRAP_UNPRINTABLE_HEADER
13
15
16#include <ostream>
17#include <utility>
18
19namespace tlx {
20
21//! SFINAE magic helper for wrap_unprintable()
22template <typename, typename = void>
24{ static constexpr bool value = false; };
25
26template <typename Type>
28 Type, decltype(
29 std::declval<std::ostream&>() << std::declval<Type const&>(), void())>
30{ static constexpr bool value = true; };
31
32//! SFINAE magic to return "<unprintable>" instead if the value HAS NO ostream
33//! operator << available. Identical to shorter wrap_unp().
34template <typename Type>
35typename enable_if<!has_ostream_operator<Type>::value, const char*>::type
36wrap_unprintable(Type, const char* instead = "<unprintable>") {
37 return instead;
38}
39
40//! SFINAE magic to return the value if the value HAS a ostream operator <<
41//! available. Identical to shorter wrap_unp().
42template <typename Type>
43typename enable_if<has_ostream_operator<Type>::value, Type>::type
44wrap_unprintable(Type value, const char* = nullptr) {
45 return value;
46}
47
48//! SFINAE magic to return "<unprintable>" instead if the value HAS NO ostream
49//! operator << available. Shortened name of wrap_unprintable()
50template <typename Type>
51typename enable_if<!has_ostream_operator<Type>::value, const char*>::type
52wrap_unp(Type, const char* instead = "<unprintable>") {
53 return instead;
54}
55
56//! SFINAE magic to return the value if the value HAS a ostream operator <<
57//! available. Shortened name of wrap_unprintable()
58template <typename Type>
59typename enable_if<has_ostream_operator<Type>::value, Type>::type
60wrap_unp(Type value, const char* = nullptr) {
61 return value;
62}
63
64} // namespace tlx
65
66#endif // !TLX_LOGGER_WRAP_UNPRINTABLE_HEADER
67
68/******************************************************************************/
STL namespace.
enable_if<!has_ostream_operator< Type >::value, constchar * >::type wrap_unp(Type, const char *instead="<unprintable>")
SFINAE magic to return "<unprintable>" instead if the value HAS NO ostream operator << available.
enable_if<!has_ostream_operator< Type >::value, constchar * >::type wrap_unprintable(Type, const char *instead="<unprintable>")
SFINAE magic to return "<unprintable>" instead if the value HAS NO ostream operator << available.
SFINAE enable_if – copy of std::enable_if<> with less extra cruft.
Definition: enable_if.hpp:22
SFINAE magic helper for wrap_unprintable()
static constexpr bool value