Jpp - 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 
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),
96  message(message)
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
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.
#define JTEST(__A__)
Test macro.
Definition: JTest.hh:45
STREAM(const std::string &message="")
Constructor.
static JFalse test(...)
default false
std::ostream & operator<<(const T &value)
Write value to output stream.
std::ostream & out
static T & getReference()
friend JStream operator<<(std::ostream &out, const STREAM &format)
Format specifier.
Test availability of stream operators.
Auxiliary class for any type definition.
Definition: JAnyType.hh:19
Auxiliary template class for type bool.
Definition: JBool.hh:20
std::string message
Auxiliary base class for compile time evaluation of test.
Definition: JTest.hh:21
definition of true
Definition: JTest.hh:24
Auxiliary data structure for handling std::ostream.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
static const bool has_istream
true if std::istream&amp; operator&gt;&gt;(std::istream&amp;, T&amp;) is defined; else false
Auxiliary class for no type definition.
Definition: JNullType.hh:19
static const bool has_ostream
true if std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const T&amp;) is defined; else false
static std::istream & is()
std::ostream & print(std::ostream &out, const T &value, const JLANG::JBool< false > &option)
Print value if given option is true.
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1618
static JTrue test(std::istream &)
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:38
static std::ostream & os()