Jpp
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
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 
145 
146  /**
147  * Auxiliary class to temporarily replace std::ostream.
148  */
149  struct JPrinter {
150  /**
151  * Constructor.
152  *
153  * \param out output stream
154  */
155  JPrinter(std::ostream& out) :
156  __out(out)
157  {}
158 
159 
160  /**
161  * Type definition of I/O operator.
162  */
163  typedef std::ostream& (*io_manip) (std::ostream&);
164 
165 
166  /**
167  * Parse I/O manipulator.
168  *
169  * \param manip I/O manipulator
170  * \return output stream
171  */
172  inline std::ostream& operator<<(io_manip manip)
173  {
174  return __out << manip;
175  }
176 
177 
178  /**
179  * Parse object.
180  *
181  * \param object object
182  * \return output stream
183  */
184  template<class T>
185  inline std::ostream& operator<<(const T& object)
186  {
187  return __out << object;
188  }
189 
190  private:
191  std::ostream& __out;
192  };
193 }
194 
195 
196 /**
197  * Print object via helper.
198  *
199  * \param out output stream
200  * \param object object
201  * \return output stream
202  */
203 inline JLANG::JPrinter operator<<(std::ostream& out, const JLANG::JPrintHelper& object)
204 {
205  return object.print(out);
206 }
207 
208 #endif
JLANG::JTest::JTrue
definition of true
Definition: JTest.hh:24
JLANG::JPrintHelper::JTypewriter::print
virtual std::ostream & print(std::ostream &out, const void *p) const
Print object.
Definition: JPrintHelper.hh:61
JLANG::JPrinter::io_manip
std::ostream &(* io_manip)(std::ostream &)
Type definition of I/O operator.
Definition: JPrintHelper.hh:163
JLANG::JType
Auxiliary class for a type holder.
Definition: JType.hh:19
JLANG::JPrintHelper::JTypeout::print
virtual std::ostream & print(std::ostream &out, const void *p) const =0
Print object.
JBool.hh
JLANG::JPrintHelper::JTypeout::~JTypeout
virtual ~JTypeout()
Virtual destructor.
Definition: JPrintHelper.hh:32
JLANG::JPrintHelper::typeout
JTypeout * typeout
pointer to printer interface
Definition: JPrintHelper.hh:81
JLANG::JPrintHelper::JTypeout
Print interface.
Definition: JPrintHelper.hh:28
JLANG::JPrintHelper::print
std::ostream & print(std::ostream &out) const
Print object.
Definition: JPrintHelper.hh:139
JLANG::JPrintHelper::get
static JTypeout * get(JType< T > type, JBool< true > option)
Get type writer.
Definition: JPrintHelper.hh:75
JLANG::JPrintHelper::JMemberMethod::__str__
static const bool __str__
Definition: JPrintHelper.hh:98
JLANG::JTest::test
static JFalse test(...)
default false
JLANG::JPrintHelper::p
const void * p
pointer to object
Definition: JPrintHelper.hh:80
JLANG::JTest
Auxiliary base class for compile time evaluation of test.
Definition: JTest.hh:21
JLANG::JPrinter::operator<<
std::ostream & operator<<(io_manip manip)
Parse I/O manipulator.
Definition: JPrintHelper.hh:172
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
JLANG::JPrinter::__out
std::ostream & __out
Definition: JPrintHelper.hh:191
JLANG::JPrintHelper::JPrintHelper
JPrintHelper(const T &object)
Constructor.
Definition: JPrintHelper.hh:118
JLANG::JBool
Auxiliary template class for type bool.
Definition: JBool.hh:20
JLANG::JPrinter
Auxiliary class to temporarily replace std::ostream.
Definition: JPrintHelper.hh:149
JLANG::JPrintHelper::JMemberMethod
Test for availability of member method const char* str() const;.
Definition: JPrintHelper.hh:89
JLANG::JPrinter::operator<<
std::ostream & operator<<(const T &object)
Parse object.
Definition: JPrintHelper.hh:185
JLANG::JPrintHelper::JMemberMethod::test
static JTrue test(JTypecheck< const char *(U::*)() const, &U::__str__ > *)
operator<<
JLANG::JPrinter operator<<(std::ostream &out, const JLANG::JPrintHelper &object)
Print object via helper.
Definition: JPrintHelper.hh:203
JLANG::JTest::JTypecheck
Auxiliary class for type checking.
Definition: JTest.hh:36
JClass.hh
JLANG
Auxiliary classes and methods for language specific functionality.
Definition: JAbstractClass.hh:10
JLANG::JPrintHelper::JTypewriter
Type writer implementation of interface JTypeout based on member method const char* str() const;
Definition: JPrintHelper.hh:51
JLANG::JPrintHelper
Auxiliary class to print via member method const char* str() const;.
Definition: JPrintHelper.hh:24
JType.hh
JLANG::JPrintHelper::~JPrintHelper
~JPrintHelper()
Destructor.
Definition: JPrintHelper.hh:127
JTest.hh
JLANG::JPrinter::JPrinter
JPrinter(std::ostream &out)
Constructor.
Definition: JPrintHelper.hh:155