Jpp 19.3.0-rc.3
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#include "JLang/JBool.hh"
9
10
11/**
12 * \author mdejong
13 */
14
15/**
16 * Test availability of stream operators.
17 */
18template<class T>
20
21 template<class U> static auto is(U*) -> decltype(std::declval<std::istream&>() >> std::declval<U&>());
22 template<class U> static auto os(U*) -> decltype(std::declval<std::ostream&>() << std::declval<const U&>());
23
24 template<typename> static auto is(...) -> std::false_type;
25 template<typename> static auto os(...) -> std::false_type;
26
27public:
28 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
29 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
30};
31
32
33/**
34 * Auxiliary data structure for handling std::ostream.
35 */
36struct STREAM {
37protected:
38 /**
39 * Auxiliary class for format stream.
40 */
41 struct JStream
42 {
43 /**
44 * Constructor.
45 *
46 * \param out output stream
47 * \param message message printed in case operator std::ostream<< is unavailable
48 */
49 JStream(std::ostream& out, const std::string& message) :
50 out (out),
52 {}
53
54
55 /**
56 * Write value to output stream.
57 *
58 * \param value value
59 * \return this JStream
60 */
61 template<class T>
62 std::ostream& operator<<(const T& value)
63 {
64 using namespace JPP;
65
67 }
68
69 private:
70 /**
71 * Print value if given option is true.
72 *
73 * \param out output stream
74 * \param value value
75 * \param option true
76 * \return output stream
77 */
78 template<class T>
79 std::ostream& print(std::ostream& out, const T& value, const JLANG::JBool<true>& option)
80 {
81 return out << value;
82 }
83
84 /**
85 * Print value if given option is true.
86 *
87 * \param out output stream
88 * \param value value
89 * \param option false
90 * \return output stream
91 */
92 template<class T>
93 std::ostream& print(std::ostream& out, const T& value, const JLANG::JBool<false>& option)
94 {
95 return out << message;
96 }
97
98 std::ostream& out;
99 std::string message;
100 };
101
102 std::string message;
103
104public:
105 /**
106 * Constructor.
107 *
108 * \param message message printed in case operator std::ostream<< is unavailable
109 */
110 STREAM(const std::string& message = "") :
112 {}
113
114
115 /**
116 * Format specifier.
117 *
118 * \param out output stream
119 * \param format format
120 * \return output stream
121 */
122 friend inline JStream operator<<(std::ostream& out, const STREAM& format)
123 {
124 return JStream(out, format.message);
125 }
126};
127
128#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 & >())
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary template class for type bool.
Definition JBool.hh:21
Auxiliary class for format stream.
std::ostream & print(std::ostream &out, const T &value, const JLANG::JBool< false > &option)
Print value if given option is true.
std::ostream & out
std::ostream & operator<<(const T &value)
Write value to output stream.
std::ostream & print(std::ostream &out, const T &value, const JLANG::JBool< true > &option)
Print value if given option is true.
JStream(std::ostream &out, const std::string &message)
Constructor.
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