Jpp  18.5.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  */
18 template<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 
27 public:
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  */
36 struct STREAM {
37 protected:
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),
51  message(message)
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 
66  return print(out, value, JBool<JStreamAvailable<T>::has_ostream>());
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;
100  };
101 
103 
104 public:
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
JStream(std::ostream &out, const std::string &message)
Constructor.
Auxiliary class for format stream.
std::ostream & print(std::ostream &out, const T &value, const JLANG::JBool< true > &option)
Print value if given option is true.
STREAM(const std::string &message="")
Constructor.
std::string message
std::ostream & operator<<(const T &value)
Write value to output stream.
std::ostream & out
friend JStream operator<<(std::ostream &out, const STREAM &format)
Format specifier.
static auto os(U *) -> decltype(std::declval< std::ostream & >()<< std::declval< const U & >())
Test availability of stream operators.
Auxiliary template class for type bool.
Definition: JBool.hh:20
std::string message
Auxiliary data structure for handling std::ostream.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
then awk string
std::ostream & print(std::ostream &out, const T &value, const JLANG::JBool< false > &option)
Print value if given option is true.
static const bool has_ostream
true if std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const T&amp;) is defined; else false
static auto is(U *) -> decltype(std::declval< std::istream & >() >> std::declval< U & >())
static const bool has_istream
true if std::istream&amp; operator&gt;&gt;(std::istream&amp;, T&amp;) is defined; else false