formatter.hh

Go to the documentation of this file.
00001 /*
00002    The lestes compiler suite
00003    Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy
00004    Copyright (C) 2002, 2003, 2004, 2005 Petr Zika
00005    Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala
00006    Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina
00007    Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda
00008    Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar
00009    Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas
00010 
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; version 2 of the License.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    See the full text of the GNU General Public License version 2, and
00021    the limitations in the file doc/LICENSE.
00022 
00023    By accepting the license the licensee waives any and all claims
00024    against the copyright holder(s) related in whole or in part to the
00025    work, its use, and/or the inability to use it.
00026  
00027  */
00028 #ifndef lestes__msg___formatter_hh___included
00029 #define lestes__msg___formatter_hh___included
00030 
00031 /*! \file
00032   \brief Formatter template.
00033 
00034   Declaration of formatter template representing object formatter.
00035   \author pt
00036 */
00037 #include <lestes/common.hh>
00038 #include <sstream>
00039 #include <ostream>
00040 
00041 package(lestes);
00042 package(msg);
00043 
00044 /*!
00045   \brief Object formatter.
00046 
00047   Represents formatter template performing conversion of objects to lstring representation.
00048   The default formatting is done through ostream and is thus suitable for basic types.
00049   \param T  The type of object to format.
00050 */
00051 template <typename T>
00052 class formatter : public object {
00053 public:
00054         //! Formats object of type T.
00055         virtual lstring format(const T &x);
00056         //! Returns the instance.
00057         static ptr< formatter<T> > instance(void);
00058 protected:
00059         //! Creates the object.
00060         formatter(void);
00061 private:
00062         //! The singleton instance.
00063         static srp< formatter<T> > singleton;
00064         //! Hides copy constructor.
00065         formatter(const formatter<T> &);
00066         //! Hides assignment operator.
00067         formatter<T> &operator=(const formatter<T> &);
00068 };
00069 
00070 /*!
00071   The singleton instance of the formatter.
00072 */
00073 template <typename T>
00074 srp< formatter<T> > formatter<T>::singleton;
00075 
00076 /*!
00077   Creates the formatter.
00078 */
00079 template <typename T>
00080 formatter<T>::formatter()
00081 {
00082 }
00083 
00084 /*!
00085   Returns the instance of the formatter.
00086   \return  The singleton instance.
00087 */
00088 template <typename T>
00089 ptr< formatter<T> > formatter<T>::instance(void)
00090 {
00091         if (!singleton) {
00092                 singleton = new formatter<T>();
00093         }
00094         return singleton;
00095 }
00096 
00097 /*!
00098   Converts argument into readable form.
00099   \param x  The object to format.
00100   \return  The text representation of the argument.
00101 */
00102 template <typename T>
00103 lstring formatter<T>::format(const T &x)
00104 {
00105         ::std::stringbuf sb;
00106         ::std::ostream os(&sb);
00107         os << x;
00108         return sb.str();
00109 }
00110 
00111 #if 0
00112 /*!
00113   \brief Formatter for ulint.
00114   
00115   Specialization of formatter for ulint.
00116 */
00117 template <>
00118 class formatter<ulint>
00119 {
00120 public:
00121         //! Formats ulint.
00122         static lstring format(ulint x);
00123 private:
00124         //! Hides copy constructor.
00125         formatter(const formatter<ulint> &);
00126         //! Hides assignment operator.
00127         formatter<ulint> &operator=(const formatter<ulint> &);
00128 };
00129 
00130 /*!
00131   Converts argument into readable form.
00132   \param x  The object to format.
00133   \return  The text representation of the argument.
00134 */
00135 lstring formatter<ulint>::format(ulint x)
00136 {
00137         ::std::stringbuf sb;
00138         ::std::ostream os(&sb);
00139         os << x;
00140         return sb.str();
00141 }
00142 #endif
00143 
00144 end_package(msg);
00145 end_package(lestes);
00146 
00147 #include <lestes/msg/as_id_formatter.hh>
00148 
00149 #endif
00150 /* vim: set ft=lestes : */

Generated on Mon Feb 12 18:22:33 2007 for lestes by doxygen 1.5.1-20070107