Jpp 21.0.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JStreamAvailable.hh
Go to the documentation of this file.
1#ifndef __JLANG__JSTREAMAVAILABLE__
2#define __JLANG__JSTREAMAVAILABLE__
3
4#include <istream>
5#include <ostream>
6#include <type_traits>
7
8
9
10/**
11 * \author mdejong
12 */
13
14/**
15 * Test availability of stream operators.
16 */
17template<class T>
19
20 template<class U> static auto is(U*) -> decltype(std::declval<std::istream&>() >> std::declval<U&>());
21 template<class U> static auto os(U*) -> decltype(std::declval<std::ostream&>() << std::declval<const U&>());
22
23 template<typename> static auto is(...) -> std::false_type;
24 template<typename> static auto os(...) -> std::false_type;
25
26public:
27 static const bool has_istream = std::is_same<std::istream&, decltype(is<T>(0))>::value; //!< true if <tt>std::istream& operator>>(std::istream&, T&)</tt> is defined; else false
28 static const bool has_ostream = std::is_same<std::ostream&, decltype(os<T>(0))>::value; //!< true if <tt>std::ostream& operator<<(std::ostream&, const T&)</tt> is defined; else false
29};
30
31
32/**
33 * Auxiliary data structure for handling std::ostream.
34 */
35struct STREAM {
36protected:
37 /**
38 * Auxiliary class for format stream.
39 */
40 struct JStream
41 {
42 /**
43 * Constructor.
44 *
45 * \param out output stream
46 * \param message message printed in case operator std::ostream<< is unavailable
47 */
48 JStream(std::ostream& out, const std::string& message) :
49 out (out),
51 {}
52
53
54 /**
55 * Write value to output stream.
56 *
57 * \param value value
58 * \return this JStream
59 */
60 template<class T>
61 std::ostream& operator<<(const T& value)
62 {
63 return print(out, value, std::bool_constant<JStreamAvailable<T>::has_ostream>());
64 }
65
66 private:
67 /**
68 * Print value if given option is true.
69 *
70 * \param out output stream
71 * \param value value
72 * \param option true
73 * \return output stream
74 */
75 template<class T>
76 std::ostream& print(std::ostream& out, const T& value, const std::true_type option)
77 {
78 return out << value;
79 }
80
81 /**
82 * Print value if given option is true.
83 *
84 * \param out output stream
85 * \param value value
86 * \param option false
87 * \return output stream
88 */
89 template<class T>
90 std::ostream& print(std::ostream& out, const T& value, const std::false_type option)
91 {
92 return out << message;
93 }
94
95 std::ostream& out;
96 std::string message;
97 };
98
99 std::string message;
100
101public:
102 /**
103 * Constructor.
104 *
105 * \param message message printed in case operator std::ostream<< is unavailable
106 */
107 STREAM(const std::string& message = "") :
109 {}
110
111
112 /**
113 * Format specifier.
114 *
115 * \param out output stream
116 * \param format format
117 * \return output stream
118 */
119 friend inline JStream operator<<(std::ostream& out, const STREAM& format)
120 {
121 return JStream(out, format.message);
122 }
123};
124
125#endif
Test availability of stream operators.
static const bool has_ostream
true if std::ostream& operator<<(std::ostream&, const T&) is defined; else false
static auto os(...) -> std::false_type
static auto is(...) -> std::false_type
static const bool has_istream
true if std::istream& operator>>(std::istream&, T&) is defined; else false
static auto os(U *) -> decltype(std::declval< std::ostream & >()<< std::declval< const U & >())
static auto is(U *) -> decltype(std::declval< std::istream & >() > > std::declval< U & >())
Auxiliary class for format stream.
std::ostream & out
std::ostream & operator<<(const T &value)
Write value to output stream.
std::ostream & print(std::ostream &out, const T &value, const std::false_type option)
Print value if given option is true.
JStream(std::ostream &out, const std::string &message)
Constructor.
std::ostream & print(std::ostream &out, const T &value, const std::true_type option)
Print value if given option is true.
Auxiliary data structure for handling std::ostream.
STREAM(const std::string &message="")
Constructor.
friend JStream operator<<(std::ostream &out, const STREAM &format)
Format specifier.
std::string message