Main Page   Reference Manual   Namespace List   Compound List   Namespace Members   Compound Members   File Members  

cwprint.h
Go to the documentation of this file.
1 // $Header$
2 //
3 // Copyright (C) 2000 - 2004, by
4 //
5 // Carlo Wood, Run on IRC <carlo@alinoe.com>
6 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
7 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
8 //
9 // This file may be distributed under the terms of the Q Public License
10 // version 1.0 as appearing in the file LICENSE.QPL included in the
11 // packaging of this file.
12 //
13 
26 #ifndef LIBCWD_CWPRINT_H
27 #define LIBCWD_CWPRINT_H
28 
29 namespace libcwd {
30 
31 //===================================================================================================
32 // cwprint
33 //
34 
35 template<class PRINTABLE_OBJECT>
36  class cwprint_tct {
37  private:
38  PRINTABLE_OBJECT const& M_printable_object;
39  public:
40  cwprint_tct(PRINTABLE_OBJECT const& printable_object) : M_printable_object(printable_object) { }
41 
42  friend
43  inline // Must be defined inside the class declaration in order to avoid a compiler warning.
44  std::ostream&
45  operator<<(std::ostream& os, cwprint_tct<PRINTABLE_OBJECT> const& __cwprint)
46  {
47  __cwprint.M_printable_object.print_on(os);
48  return os;
49  }
50  };
51 
85 template<class T>
86  inline
87  cwprint_tct<T>
88  cwprint(T const& printable_object)
89  {
90  return cwprint_tct<T>(printable_object);
91  }
92 
93 //===================================================================================================
94 // cwprint_using
95 //
96 
97 template<class PRINTABLE_OBJECT>
98  class cwprint_using_tct {
99  typedef void (PRINTABLE_OBJECT::* print_on_method_t)(std::ostream&) const;
100  private:
101  PRINTABLE_OBJECT const& M_printable_object;
102  print_on_method_t M_print_on_method;
103  public:
104  cwprint_using_tct(PRINTABLE_OBJECT const& printable_object, print_on_method_t print_on_method) :
105  M_printable_object(printable_object), M_print_on_method(print_on_method) { }
106 
107  friend
108  inline // Must be defined inside the class declaration in order to avoid a compiler warning.
109  std::ostream&
110  operator<<(std::ostream& os, cwprint_using_tct<PRINTABLE_OBJECT> __cwprint_using)
111  {
112  (__cwprint_using.M_printable_object.*__cwprint_using.M_print_on_method)(os);
113  return os;
114  }
115  };
116 
150 // The use of T_OR_BASE_OF_T as extra parameter is a compiler bug around.
151 // Without it you'd run into bug
152 // http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=38
153 // when `print_on_method' is a method of the base class of T.
154 
155 template<class T, class T_OR_BASE_OF_T>
156  inline
157  cwprint_using_tct<T_OR_BASE_OF_T>
158  cwprint_using(T const& printable_object, void (T_OR_BASE_OF_T::*print_on_method)(std::ostream&) const)
159  {
160  T_OR_BASE_OF_T const& base(printable_object);
161  return cwprint_using_tct<T_OR_BASE_OF_T>(base, print_on_method);
162  }
163 
164 } // namespace libcwd
165 
166 #endif // LIBCWD_CWPRINT_H
namespace for libcwd.
Definition: debug.cc:87
Copyright © 2001 - 2004 Carlo Wood.  All rights reserved.