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

libcwd/cwprint.h

Go to the documentation of this file.
00001 // $Header$
00002 //
00003 // Copyright (C) 2000 - 2004, by
00004 // 
00005 // Carlo Wood, Run on IRC <carlo@alinoe.com>
00006 // RSA-1024 0x624ACAD5 1997-01-26                    Sign & Encrypt
00007 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6  F6 F6 55 DD 1C DC FF 61
00008 //
00009 // This file may be distributed under the terms of the Q Public License
00010 // version 1.0 as appearing in the file LICENSE.QPL included in the
00011 // packaging of this file.
00012 //
00013 
00026 #ifndef LIBCWD_CWPRINT_H
00027 #define LIBCWD_CWPRINT_H
00028 
00029 namespace libcwd {
00030 
00031 //===================================================================================================
00032 // cwprint
00033 //
00034 
00035 template<class PRINTABLE_OBJECT>
00036   class cwprint_tct {
00037   private:
00038     PRINTABLE_OBJECT const& M_printable_object;
00039   public:
00040     cwprint_tct(PRINTABLE_OBJECT const& printable_object) : M_printable_object(printable_object) { }
00041 
00042     friend
00043     inline      // Must be defined inside the class declaration in order to avoid a compiler warning.
00044     std::ostream&
00045     operator<<(std::ostream& os, cwprint_tct<PRINTABLE_OBJECT> const& __cwprint)
00046     {
00047       __cwprint.M_printable_object.print_on(os);
00048       return os;
00049     }
00050   };
00051 
00085 template<class T>
00086   inline
00087   cwprint_tct<T>
00088   cwprint(T const& printable_object)
00089   {
00090     return cwprint_tct<T>(printable_object);
00091   }
00092 
00093 //===================================================================================================
00094 // cwprint_using
00095 //
00096 
00097 template<class PRINTABLE_OBJECT>
00098   class cwprint_using_tct {
00099     typedef void (PRINTABLE_OBJECT::* print_on_method_t)(std::ostream&) const;
00100   private:
00101     PRINTABLE_OBJECT const& M_printable_object;
00102     print_on_method_t M_print_on_method;
00103   public:
00104     cwprint_using_tct(PRINTABLE_OBJECT const& printable_object, print_on_method_t print_on_method) :
00105         M_printable_object(printable_object), M_print_on_method(print_on_method) { }
00106 
00107     friend
00108     inline      // Must be defined inside the class declaration in order to avoid a compiler warning.
00109     std::ostream&
00110     operator<<(std::ostream& os, cwprint_using_tct<PRINTABLE_OBJECT> __cwprint_using)
00111     {
00112       (__cwprint_using.M_printable_object.*__cwprint_using.M_print_on_method)(os);
00113       return os;
00114     }
00115   };
00116 
00150 // The use of T_OR_BASE_OF_T as extra parameter is a compiler bug around.
00151 // Without it you'd run into bug
00152 // http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=38
00153 // when `print_on_method' is a method of the base class of T.
00154 
00155 template<class T, class T_OR_BASE_OF_T>
00156   inline
00157   cwprint_using_tct<T_OR_BASE_OF_T>
00158   cwprint_using(T const& printable_object, void (T_OR_BASE_OF_T::*print_on_method)(std::ostream&) const)
00159   {
00160     T_OR_BASE_OF_T const& base(printable_object);
00161     return cwprint_using_tct<T_OR_BASE_OF_T>(base, print_on_method);
00162   }
00163 
00164 } // namespace libcwd
00165 
00166 #endif // LIBCWD_CWPRINT_H
Copyright © 2001 - 2004 Carlo Wood.  All rights reserved.