Jpp  18.0.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JPrintHelper.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JPRINTHELPER__
2 #define __JLANG__JPRINTHELPER__
3 
4 #include <ostream>
5 
6 #include "JLang/JType.hh"
7 #include "JLang/JBool.hh"
8 #include "JLang/JTest.hh"
9 #include "JLang/JClass.hh"
10 
11 
12 /**
13  * \author mdejong
14  */
15 
16 namespace JLANG {}
17 namespace JPP { using namespace JLANG; }
18 
19 namespace JLANG {
20 
21  /**
22  * Auxiliary class to print via member method <tt>const char* __str__() const;</tt>.
23  */
24  class JPrintHelper {
25  /**
26  * Print interface.
27  */
28  struct JTypeout {
29  /**
30  * Virtual destructor.
31  */
32  virtual ~JTypeout()
33  {}
34 
35 
36  /**
37  * Print object.
38  *
39  * \param out output stream
40  * \param p pointer to object
41  * \return output stream
42  */
43  virtual std::ostream& print(std::ostream& out, const void* p) const = 0;
44  };
45 
46 
47  /**
48  * Type writer implementation of interface JTypeout based on member method <tt>const char* __str__() const;</tt>
49  */
50  template<class T>
51  struct JTypewriter :
52  public JTypeout
53  {
54  /**
55  * Print object.
56  *
57  * \param out output stream
58  * \param p pointer to object
59  * \return output stream
60  */
61  virtual std::ostream& print(std::ostream& out, const void* p) const override
62  {
63  return out << ((const T*) p)->__str__();;
64  }
65  };
66 
67 
68  /**
69  * Get type writer.
70  *
71  * \param type type
72  * \param option true
73  */
74  template<class T>
75  static JTypeout* get(JType<T> type, JBool<true> option)
76  {
77  return new JTypewriter<T>();
78  }
79 
80  const void* p; //!< pointer to object
81  JTypeout* typeout; //!< pointer to printer interface
82 
83 
84  public:
85  /**
86  * Test for availability of member method <tt>const char* __str__() const;</tt>.
87  */
88  template<class T, bool is_primitive = JClass<T>::is_primitive>
89  class JMemberMethod :
90  public JTest
91  {
92  using JTest::test;
93 
94  template<class U>
95  static JTrue test(JTypecheck<const char* (U::*)() const, &U::__str__>*);
96 
97  public:
98  static const bool __str__ = JTEST(test<T>(0));
99  };
100 
101 
102  /**
103  * Specialisation of JMemberMethod for primitive data types.
104  */
105  template<class T>
106  struct JMemberMethod<T, true>
107  {
108  static const bool __str__ = false;
109  };
110 
111 
112  /**
113  * Constructor
114  *
115  * \param object object
116  */
117  template<class T>
118  JPrintHelper(const T& object) :
119  p(&object),
120  typeout(get(JType<T>(), JBool<JMemberMethod<T>::__str__>()))
121  {}
122 
123 
124  /**
125  * Destructor.
126  */
128  {
129  delete typeout;
130  }
131 
132 
133  /**
134  * Print object.
135  *
136  * \param out output stream
137  * \return output stream
138  */
139  inline std::ostream& print(std::ostream& out) const
140  {
141  return typeout->print(out, p);
142  }
143 
144  private:
145  JPrintHelper(const JPrintHelper&);
149  };
150 
151 
152  /**
153  * Auxiliary class to temporarily replace std::ostream.
154  */
155  struct JPrinter {
156  /**
157  * Constructor.
158  *
159  * \param out output stream
160  */
161  JPrinter(std::ostream& out) :
162  __out(out)
163  {}
164 
165 
166  /**
167  * Type definition of I/O operator.
168  */
169  typedef std::ostream& (*io_manip) (std::ostream&);
170 
171 
172  /**
173  * Parse I/O manipulator.
174  *
175  * \param manip I/O manipulator
176  * \return output stream
177  */
178  inline std::ostream& operator<<(io_manip manip)
179  {
180  return __out << manip;
181  }
182 
183 
184  /**
185  * Parse object.
186  *
187  * \param object object
188  * \return output stream
189  */
190  template<class T>
191  inline std::ostream& operator<<(const T& object)
192  {
193  return __out << object;
194  }
195 
196  private:
197  std::ostream& __out;
198  };
199 }
200 
201 
202 /**
203  * Print object via helper.
204  *
205  * \param out output stream
206  * \param object object
207  * \return output stream
208  */
209 inline JLANG::JPrinter operator<<(std::ostream& out, const JLANG::JPrintHelper& object)
210 {
211  return object.print(out);
212 }
213 
214 #endif
Auxiliary class to print via member method const char* str() const;.
Definition: JPrintHelper.hh:24
#define JTEST(__A__)
Test macro.
Definition: JTest.hh:45
std::ostream & print(std::ostream &out) const
Print object.
static JFalse test(...)
default false
JTypeout * typeout
pointer to printer interface
Definition: JPrintHelper.hh:81
Auxiliary class to temporarily replace std::ostream.
std::ostream & operator<<(io_manip manip)
Parse I/O manipulator.
std::ostream &(* io_manip)(std::ostream &)
Type definition of I/O operator.
Auxiliary class for a type holder.
Definition: JType.hh:19
JPrintHelper & operator=(const JPrintHelper &)
virtual ~JTypeout()
Virtual destructor.
Definition: JPrintHelper.hh:32
JPrinter(std::ostream &out)
Constructor.
Test for availability of member method const char* str() const;.
Definition: JPrintHelper.hh:89
Auxiliary template class for type bool.
Definition: JBool.hh:20
Auxiliary base class for compile time evaluation of test.
Definition: JTest.hh:21
definition of true
Definition: JTest.hh:24
do set_variable OUTPUT_DIRECTORY $WORKDIR T
~JPrintHelper()
Destructor.
JPrintHelper(const T &object)
Constructor.
Type writer implementation of interface JTypeout based on member method const char* str() const; ...
Definition: JPrintHelper.hh:51
static JTypeout * get(JType< T > type, JBool< true > option)
Get type writer.
Definition: JPrintHelper.hh:75
virtual std::ostream & print(std::ostream &out, const void *p) const =0
Print object.
const void * p
pointer to object
Definition: JPrintHelper.hh:80
std::ostream & __out
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
static JTrue test(JTypecheck< const char *(U::*)() const,&U::__str__ > *)
std::ostream & operator<<(const T &object)
Parse object.
virtual std::ostream & print(std::ostream &out, const void *p) const override
Print object.
Definition: JPrintHelper.hh:61
Auxiliary class for type checking.
Definition: JTest.hh:36