Jpp
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 
7 #include "JLang/JAnyType.hh"
8 #include "JLang/JNullType.hh"
9 #include "JLang/JTest.hh"
10 #include "JLang/JBool.hh"
11 #include "JLang/JPrintHelper.hh"
12 
13 
14 /**
15  * \author mdejong
16  */
17 
18 /**
19  * Fallback implementation for <tt>std::istream& operator>>(std::istream, T&)</tt>
20  * for types that don't support the stream operator.
21  *
22  * \param in input stream
23  * \param any_type any type
24  * \return null type
25  */
26 JLANG::JNullType operator>>(std::istream& in, JLANG::JAnyType any_type);
27 
28 
29 /**
30  * \cond NEVER
31  * Fallback implementation for <tt>std::ostream& operator<<(std::ostream, const T&)</tt>
32  * for types that don't support the stream operator.
33  *
34  * \param out output stream
35  * \param any_type any type
36  * \return null type
37  * \endcond
38  */
39 //JLANG::JNullType operator<<(std::ostream& out, JLANG::JAnyType any_type);
40 
41 
42 /**
43  * Test availability of stream operators.
44  */
45 template<class T, bool __str__ = JLANG::JPrintHelper::JMemberMethod<T>::__str__>
47  public JLANG::JTest
48 {
49  using JLANG::JTest::test;
50 
51  static JTrue test(std::istream&);
52  static JTrue test(std::ostream&);
53 
54  static T& getReference();
55 
56  static std::istream& is();
57  static std::ostream& os();
58 
59 public:
60  static const bool has_istream = JTEST(test(is() >> getReference())); //!< true if <tt>std::istream& operator>>(std::istream&, T&)</tt> is defined; else false
61  static const bool has_ostream = JTEST(test(os() << getReference())); //!< true if <tt>std::ostream& operator<<(std::ostream&, const T&)</tt> is defined; else false
62 };
63 
64 
65 /**
66  * Specialisation of JStreamAvailable for class without member method <tt>__str__</tt>.
67  */
68 template<class T>
69 class JStreamAvailable<T, true> :
70  public JLANG::JTest
71 {
72 public:
74  static const bool has_ostream = true;
75 };
76 
77 
78 /**
79  * Auxiliary data structure for handling std::ostream.
80  */
81 struct STREAM {
82 protected:
83  /**
84  * Auxiliary class for format stream.
85  */
86  struct JStream
87  {
88  /**
89  * Constructor.
90  *
91  * \param out output stream
92  * \param message message printed in case operator std::ostream<< is unavailable
93  */
94  JStream(std::ostream& out, const std::string& message) :
95  out (out),
97  {}
98 
99 
100  /**
101  * Write value to output stream.
102  *
103  * \param value value
104  * \return this JStream
105  */
106  template<class T>
107  std::ostream& operator<<(const T& value)
108  {
109  using namespace JPP;
110 
111  return print(out, value, JBool<JStreamAvailable<T>::has_ostream>());
112  }
113 
114  private:
115  /**
116  * Print value if given option is true.
117  *
118  * \param out output stream
119  * \param value value
120  * \param option true
121  * \return output stream
122  */
123  template<class T>
124  std::ostream& print(std::ostream& out, const T& value, const JLANG::JBool<true>& option)
125  {
126  return out << value;
127  }
128 
129  /**
130  * Print value if given option is true.
131  *
132  * \param out output stream
133  * \param value value
134  * \param option false
135  * \return output stream
136  */
137  template<class T>
138  std::ostream& print(std::ostream& out, const T& value, const JLANG::JBool<false>& option)
139  {
140  return out << message;
141  }
142 
143  std::ostream& out;
144  std::string message;
145  };
146 
147  std::string message;
148 
149 public:
150  /**
151  * Constructor.
152  *
153  * \param message message printed in case operator std::ostream<< is unavailable
154  */
155  STREAM(const std::string& message = "") :
157  {}
158 
159 
160  /**
161  * Format specifier.
162  *
163  * \param out output stream
164  * \param format format
165  * \return output stream
166  */
167  friend inline JStream operator<<(std::ostream& out, const STREAM& format)
168  {
169  return JStream(out, format.message);
170  }
171 };
172 
173 #endif
JLANG::JTest::JTrue
definition of true
Definition: JTest.hh:24
STREAM::JStream::JStream
JStream(std::ostream &out, const std::string &message)
Constructor.
Definition: JStreamAvailable.hh:94
JStreamAvailable::has_ostream
static const bool has_ostream
true if std::ostream& operator<<(std::ostream&, const T&) is defined; else false
Definition: JStreamAvailable.hh:61
STREAM::JStream::message
std::string message
Definition: JStreamAvailable.hh:144
JStreamAvailable::is
static std::istream & is()
JBool.hh
JStreamAvailable::has_istream
static const bool has_istream
true if std::istream& operator>>(std::istream&, T&) is defined; else false
Definition: JStreamAvailable.hh:60
STREAM::STREAM
STREAM(const std::string &message="")
Constructor.
Definition: JStreamAvailable.hh:155
JLANG::JNullType
Auxiliary class for no type definition.
Definition: JNullType.hh:19
JLANG::JTest::test
static JFalse test(...)
default false
JLANG::JTest
Auxiliary base class for compile time evaluation of test.
Definition: JTest.hh:21
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JTEST
#define JTEST(__A__)
Test macro.
Definition: JTest.hh:45
JStreamAvailable::test
static JTrue test(std::istream &)
JAnyType.hh
JStreamAvailable::os
static std::ostream & os()
STREAM::operator<<
friend JStream operator<<(std::ostream &out, const STREAM &format)
Format specifier.
Definition: JStreamAvailable.hh:167
STREAM::JStream::print
std::ostream & print(std::ostream &out, const T &value, const JLANG::JBool< true > &option)
Print value if given option is true.
Definition: JStreamAvailable.hh:124
JStreamAvailable
Test availability of stream operators.
Definition: JStreamAvailable.hh:46
JStreamAvailable::getReference
static T & getReference()
STREAM::JStream::out
std::ostream & out
Definition: JStreamAvailable.hh:143
STREAM::JStream::print
std::ostream & print(std::ostream &out, const T &value, const JLANG::JBool< false > &option)
Print value if given option is true.
Definition: JStreamAvailable.hh:138
JLANG::JBool
Auxiliary template class for type bool.
Definition: JBool.hh:20
STREAM::JStream
Auxiliary class for format stream.
Definition: JStreamAvailable.hh:86
STREAM::message
std::string message
Definition: JStreamAvailable.hh:147
JNullType.hh
STREAM::JStream::operator<<
std::ostream & operator<<(const T &value)
Write value to output stream.
Definition: JStreamAvailable.hh:107
STREAM
Auxiliary data structure for handling std::ostream.
Definition: JStreamAvailable.hh:81
JLANG::JAnyType
Auxiliary class for any type definition.
Definition: JAnyType.hh:19
JTest.hh
operator>>
JLANG::JNullType operator>>(std::istream &in, JLANG::JAnyType any_type)
Fallback implementation for std::istream& operator>>(std::istream, T&) for types that don't support t...
JPrintHelper.hh