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

/usr/src/libcwd/libcwd/include/libcwd/cwprint.h

Go to the documentation of this file.
00001 // $Header: /cvsroot/libcwd/libcwd/include/libcwd/cwprint.h,v 1.5 2004/07/14 00:29:35 libcw Exp $
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 
00027 #ifndef LIBCWD_CWPRINT_H
00028 #define LIBCWD_CWPRINT_H
00029 
00030 namespace libcwd {
00031 
00032 //===================================================================================================
00033 // cwprint
00034 //
00035 
00036 template<class PRINTABLE_OBJECT>
00037   class cwprint_tct {
00038   private:
00039     PRINTABLE_OBJECT const& M_printable_object;
00040   public:
00041     cwprint_tct(PRINTABLE_OBJECT const& printable_object) : M_printable_object(printable_object) { }
00042 
00043     friend
00044     inline      // Must be defined inside the class declaration in order to avoid a compiler warning.
00045     std::ostream&
00046     operator<<(std::ostream& os, cwprint_tct<PRINTABLE_OBJECT> const& __cwprint)
00047     {
00048       __cwprint.M_printable_object.print_on(os);
00049       return os;
00050     }
00051   };
00052 
00086 template<class T>
00087   inline
00088   cwprint_tct<T>
00089   cwprint(T const& printable_object)
00090   {
00091     return cwprint_tct<T>(printable_object);
00092   }
00093 
00094 //===================================================================================================
00095 // cwprint_using
00096 //
00097 
00098 template<class PRINTABLE_OBJECT>
00099   class cwprint_using_tct {
00100     typedef void (PRINTABLE_OBJECT::* print_on_method_t)(std::ostream&) const;
00101   private:
00102     PRINTABLE_OBJECT const& M_printable_object;
00103     print_on_method_t M_print_on_method;
00104   public:
00105     cwprint_using_tct(PRINTABLE_OBJECT const& printable_object, print_on_method_t print_on_method) :
00106         M_printable_object(printable_object), M_print_on_method(print_on_method) { }
00107 
00108     friend
00109     inline      // Must be defined inside the class declaration in order to avoid a compiler warning.
00110     std::ostream&
00111     operator<<(std::ostream& os, cwprint_using_tct<PRINTABLE_OBJECT> __cwprint_using)
00112     {
00113       (__cwprint_using.M_printable_object.*__cwprint_using.M_print_on_method)(os);
00114       return os;
00115     }
00116   };
00117 
00151 // The use of T_OR_BASE_OF_T as extra parameter is a compiler bug around.
00152 // Without it you'd run into bug
00153 // http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=38
00154 // when `print_on_method' is a method of the base class of T.
00155 
00156 template<class T, class T_OR_BASE_OF_T>
00157   inline
00158   cwprint_using_tct<T_OR_BASE_OF_T>
00159   cwprint_using(T const& printable_object, void (T_OR_BASE_OF_T::*print_on_method)(std::ostream&) const)
00160   {
00161     T_OR_BASE_OF_T const& base(printable_object);
00162     return cwprint_using_tct<T_OR_BASE_OF_T>(base, print_on_method);
00163   }
00164 
00165 } // namespace libcwd
00166 
00167 #endif // LIBCWD_CWPRINT_H
Copyright © 2001 - 2004 Carlo Wood.  All rights reserved.