message_stencil.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___message_stencil_hh___included
00029 #define lestes__msg___message_stencil_hh___included
00030 
00031 /*! \file
00032   \brief Message stencils.
00033 
00034   Declaration of message_stencil class and message_stencilX templates representing parametrised message stencils.
00035   \author pt
00036 */
00037 #include <lestes/common.hh>
00038 #include <lestes/std/vector.hh>
00039 #include <lestes/msg/message.hh>
00040 #include <lestes/msg/formatter.hh>
00041 
00042 package(lestes);
00043 package(msg);
00044 
00045 /*!
00046   \brief Message stencil base.
00047   
00048   Represents base class parametrised stencil for creating formatted messages.
00049   Each instance of stencil represents one unique message family (generating the same kind of messages).
00050   Each instance is assigned an unique identification number to help
00051   expressing sets of message kinds efficiently, for example via bit vectors.
00052   It is however, not assured, that one object will get the same id each time.
00053   
00054   The format text recognizes parameter reference in the form %D where D is a decimal digit.
00055   The parameter numbering starts with 0 and only valid indexes are accepted.
00056   The reference to a single parameter can appear zero or more times, in no particular order.
00057   To express a single %, %% is used.
00058   Any other character after % is not allowed and neither is end of string.
00059 
00060   Example: "Value %0 is invalid in %3, %0 is out of range %1 - %2 allowed for %3"
00061   
00062   Implementation details:
00063   The parsed format text is stored as a sequence of parameter indexes with a special value
00064   (equal to the number of parameters) designating use of next part of text, stored in another sequence.
00065 */
00066 class message_stencil : public ::lestes::std::object {
00067 public:
00068         //! Returns the number of parameters.
00069         ulint nparams_get(void) const;
00070         //! Returns the unique kind identification.
00071         ulint kind_get(void) const;
00072         //! Tests equality.
00073         bool equals(const ptr<message_stencil> &other) const;
00074 protected:
00075         //! Type of message flags.
00076         typedef message::flags_type flags_type;
00077         //! Type of argument list.
00078         typedef ::lestes::std::vector<lstring> args_type;
00079         //! Creates the message stencil.
00080         message_stencil(ulint a_nparams, const lstring &a_format, flags_type a_flags);
00081         //! Returns a formatted message.
00082         ptr<message> generate(const ptr<args_type> &args) const;
00083         //! Marks the object.
00084         void gc_mark(void);
00085 private:
00086         //! Type of storage for message texts.
00087         typedef ::lestes::std::vector<lstring> texts_type;
00088         //! Type of storage for parameters.
00089         typedef ::lestes::std::vector<ulint> params_type;
00090         //! Parses the format string.
00091         void parse(const lstring &a_format);
00092         //! Number of parameters.
00093         ulint nparams;
00094         //! The unique identification of message kind.
00095         ulint kind;
00096         //! Message flags.
00097         flags_type flags;
00098         //! The parsed texts.
00099         srp<texts_type> texts;
00100         //! The parsed parameters.
00101         srp<params_type> params;
00102         //! Hides copy constructor.
00103         message_stencil(const message_stencil &);
00104         //! Hides assignment operator.
00105         message_stencil &operator=(const message_stencil &);
00106         //! The internal counter to distinguish instances.
00107         static ulint kind_counter;
00108 };
00109 
00110 /*!
00111   \brief Message stencil.
00112 
00113   Represents message stencil with no parameters.
00114   \param T Fake parameter to enable templatization.
00115 */
00116 template <typename T>
00117 class message_stencil0 : public message_stencil {
00118 public:
00119         //! Formats a message according to the stencil.
00120         ptr<message> format(void) const;
00121         //! Returns new stencil. 
00122         static ptr< message_stencil0<T> > create(const lstring &a_format, flags_type a_flags);
00123 protected:
00124         //! Creates new stencil.
00125         message_stencil0(const lstring &a_format, flags_type a_flags);
00126         //! Marks the object.
00127         void gc_mark(void);
00128 private:
00129         //! Fake field to enable templatization.
00130         //T fake_field;
00131         //! Fake method to enable templatization.
00132         void fake_method(T &);
00133         //! Hides copy constructor.
00134         message_stencil0(const message_stencil0<T> &);
00135         //! Hides assignment operator.
00136         message_stencil0<T> &operator=(const message_stencil0<T> &);
00137 };
00138 
00139 /*!
00140   Creates the stencil.
00141   \param a_format  The message format.
00142   \param a_flags  The message flags.
00143 */
00144 template <typename T>
00145 inline message_stencil0<T>::message_stencil0(const lstring &a_text, flags_type a_flags):
00146         message_stencil(0,a_text,a_flags)
00147 {
00148 }
00149 
00150 /*!
00151   Formats a messge from the stencil.
00152   \return The formatted message.
00153 */
00154 template <typename T>
00155 ptr<message> message_stencil0<T>::format(void) const
00156 {
00157         ptr<args_type> args = args_type::create();
00158         return generate(args);
00159 }
00160 
00161 /*!
00162   Returns the zero-parameter stencil, initializes with values to fill into the message.
00163   \pre  The format string is well-formed.
00164   \param a_format  The format for the message, with % designating indexed parameter slots.
00165   \param a_flags  The flags for the message.
00166   \return  The stencil with given 
00167 */
00168 template <typename T>
00169 ptr< message_stencil0<T> > message_stencil0<T>::create(const lstring &a_format, flags_type a_flags)
00170 {
00171         return new message_stencil0<T>(a_format,a_flags);
00172 }
00173 
00174 /*!
00175   Marks the object for garbage collection.
00176 */
00177 template <typename T>
00178 void message_stencil0<T>::gc_mark(void)
00179 {
00180         message_stencil::gc_mark();
00181 }
00182 
00183 /*!
00184   \brief Message stencil.
00185 
00186   Represents message stencil with one formatted parameter.
00187   \param P0  The type of the zeroth parameter.
00188 */
00189 template <typename P0>
00190 class message_stencil1 : public message_stencil {
00191 public:
00192         //! The type of formatter for zeroth parameter
00193         typedef formatter<P0> f0_type;
00194         //! Formats a message according to the stencil.
00195         ptr<message> format(const P0 &p0) const;
00196         //! Returns new stencil. 
00197         static ptr< message_stencil1<P0> > create(const lstring &a_format, flags_type a_flags, 
00198                         const ptr<f0_type> &a_f0);
00199 protected:
00200         //! Creates new stencil. 
00201         message_stencil1(const lstring &a_format, flags_type a_flags,
00202                         const ptr<f0_type> &a_f0);
00203         //! Marks the object.
00204         void gc_mark(void);
00205 private:
00206         //! The formatter for the zeroth parameter.
00207         srp<f0_type> f0;
00208         //! Hides copy constructor.
00209         message_stencil1(const message_stencil1<P0> &);
00210         //! Hides assignment operator.
00211         message_stencil1<P0> &operator=(const message_stencil1<P0> &);
00212 };
00213 
00214 /*!
00215   Marks the object for garbage collection.
00216 */
00217 template <typename P0>
00218 void message_stencil1<P0>::gc_mark(void)
00219 {
00220         f0.gc_mark();
00221         message_stencil::gc_mark();
00222 }
00223 
00224 /*!
00225   Creates the stencil.
00226   \param a_format  The message format.
00227   \param a_flags  The message flags.
00228   \param a_f0  The formatter for the zeroth parameter.
00229 */
00230 template <typename P0>
00231 message_stencil1<P0>::message_stencil1(const lstring &a_text, flags_type a_flags,
00232                 const ptr<f0_type> &a_f0):
00233         message_stencil(1,a_text,a_flags),
00234         f0(checked(a_f0))
00235 {
00236 }
00237 
00238 /*!
00239   Formats a messge from the stencil.
00240   \param p0  The zeroth parameter to format.
00241   \return The formatted message.
00242 */
00243 template <typename P0>
00244 ptr<message> message_stencil1<P0>::format(const P0 &p0) const
00245 {
00246         ptr<args_type> args = args_type::create();
00247         args->push_back(f0->format(p0));
00248         return generate(args);
00249 }
00250 
00251 /*!
00252   Returns the one-parameter stencil, initializes with values to fill into the message.
00253   \pre  The format string is well-formed.
00254   \pre  a_f0 != NULL
00255   \param a_format  The format for the message, with % designating indexed parameter slots.
00256   \param a_flags  The flags for the message.
00257   \param a_f0  The formatter for the zeroth parameter.
00258   \return  The stencil with given values.
00259 */
00260 template <typename P0>
00261 ptr< message_stencil1<P0> > message_stencil1<P0>::create(const lstring &a_format, flags_type a_flags,
00262                 const ptr<f0_type> &a_f0)
00263 {
00264         return new message_stencil1(a_format,a_flags,a_f0);
00265 }
00266 
00267 /*!
00268   \brief Message stencil.
00269 
00270   Represents message stencil with two formatted parameter.
00271   \param P0  The type of the zeroth parameter.
00272   \param P1  The type of the first parameter.
00273 */
00274 template < typename P0, typename P1>
00275 class message_stencil2 : public message_stencil {
00276 public:
00277         //! The type of formatter for zeroth parameter.
00278         typedef formatter<P0> f0_type;
00279         //! The type of formatter for first parameter.
00280         typedef formatter<P1> f1_type;
00281         //! Formats a message according to the stencil.
00282         ptr<message> format(const P0 &p0, const P1 &p1) const;
00283         //! Returns new stencil. 
00284         static ptr< message_stencil2<P0,P1> > create(const lstring &a_format, flags_type a_flags,
00285                         const ptr<f0_type> &a_f0, const ptr<f1_type> &a_f1);
00286 protected:
00287         //! Creates new stencil. 
00288         message_stencil2(const lstring &a_format, flags_type a_flags,
00289                         const ptr<f0_type> &a_f0, const ptr<f1_type> &a_f1);
00290         //! Marks the object.
00291         void gc_mark(void);
00292 private:
00293         //! The formatter for the zeroth parameter.
00294         srp<f0_type> f0;
00295         //! The formatter for the first parameter.
00296         srp<f1_type> f1;
00297         //! Hides copy constructor.
00298         message_stencil2(const message_stencil2<P0,P1> &);
00299         //! Hides assignment operator.
00300         message_stencil2<P0,P1> &operator=(const message_stencil2<P0,P1> &);
00301 };
00302 
00303 /*!
00304   Marks the object for garbage collection.
00305 */
00306 template <typename P0, typename P1>
00307 void message_stencil2<P0,P1>::gc_mark(void)
00308 {
00309         f0.gc_mark();
00310         f1.gc_mark();
00311         message_stencil::gc_mark();
00312 }
00313 
00314 /*!
00315   Creates the stencil.
00316   \param a_format  The message format.
00317   \param a_flags  The message flags.
00318   \param a_f0  The formatter for the zeroth parameter.
00319   \param a_f1  The formatter for the first parameter.
00320 */
00321 template <typename P0, typename P1>
00322 message_stencil2<P0,P1>::message_stencil2(const lstring &a_text, flags_type a_flags,
00323                 const ptr<f0_type> &a_f0, const ptr<f1_type> &a_f1):
00324         message_stencil(2,a_text,a_flags),
00325         f0(checked(a_f0)),
00326         f1(checked(a_f1))
00327 {
00328 }
00329 
00330 /*!
00331   Formats a messge from the stencil.
00332   \param p0  The zeroth argument to format.
00333   \param p1  The first argument to format.
00334   \return The formatted message.
00335 */
00336 template <typename P0, typename P1>
00337 ptr<message> message_stencil2<P0,P1>::format(const P0 &p0, const P1 &p1) const
00338 {
00339         ptr<args_type> args = args_type::create();
00340         args->push_back(f0->format(p0));
00341         args->push_back(f1->format(p1));
00342         return generate(args);
00343 }
00344 
00345 /*!
00346   Returns the two-parameter stencil, initializes with values to fill into the message.
00347   \pre  The format string is well-formed.
00348   \pre  a_f0 != NULL
00349   \pre  a_f1 != NULL
00350   \param a_format  The format for the message, with % designating indexed parameter slots.
00351   \param a_flags  The flags for the message.
00352   \param a_f0  The formatter for the zeroth parameter.
00353   \param a_f1  The formatter for the first parameter.
00354   \return  The stencil with given values.
00355 */
00356 template <typename P0, typename P1>
00357 ptr< message_stencil2<P0,P1> > message_stencil2<P0,P1>::create(const lstring &a_format, flags_type a_flags,
00358                 const ptr<f0_type> &a_f0, const ptr<f1_type> &a_f1)
00359 {
00360         return new message_stencil2(a_format,a_flags,a_f0,a_f1);
00361 }
00362 
00363 /*!
00364   \brief Message stencil.
00365 
00366   Represents message stencil with three formatted parameters.
00367   \param P0  The type of the zeroth parameter.
00368   \param P1  The type of the first parameter.
00369   \param P1  The type of the second parameter.
00370 */
00371 template < typename P0, typename P1, typename P2>
00372 class message_stencil3 : public message_stencil {
00373 public:
00374         //! The type of formatter for zeroth parameter.
00375         typedef formatter<P0> f0_type;
00376         //! The type of formatter for first parameter.
00377         typedef formatter<P1> f1_type;
00378         //! The type of formatter for second parameter.
00379         typedef formatter<P2> f2_type;
00380         //! Formats a message according to the stencil.
00381         ptr<message> format(const P0 &p0, const P1 &p1, const P2 &p2) const;
00382         //! Returns new stencil. 
00383         static ptr< message_stencil3<P0,P1,P2> > create(const lstring &a_format,
00384                 flags_type a_flags, const ptr<f0_type> &a_f0,
00385                 const ptr<f1_type> &a_f1, const ptr<f2_type> &a_f2);
00386 protected:
00387         //! Creates new stencil. 
00388         message_stencil3(const lstring &a_format, flags_type a_flags, const ptr<f0_type> &a_f0,
00389                 const ptr<f1_type> &a_f1, const ptr<f2_type> &a_f2);
00390         //! Marks the object.
00391         void gc_mark(void);
00392 private:
00393         //! The formatter for the zeroth parameter.
00394         srp<f0_type> f0;
00395         //! The formatter for the first parameter.
00396         srp<f1_type> f1;
00397         //! The formatter for the second parameter.
00398         srp<f2_type> f2;
00399         //! Hides copy constructor.
00400         message_stencil3(const message_stencil3<P0,P1,P2> &);
00401         //! Hides assignment operator.
00402         message_stencil3<P0,P1,P2> &operator=(const message_stencil3<P0,P1,P2> &);
00403 };
00404 
00405 /*!
00406   Marks the object for garbage collection.
00407 */
00408 template <typename P0, typename P1, typename P2>
00409 void message_stencil3<P0,P1,P2>::gc_mark(void)
00410 {
00411         f0.gc_mark();
00412         f1.gc_mark();
00413         f2.gc_mark();
00414         message_stencil::gc_mark();
00415 }
00416 
00417 /*!
00418   Creates the stencil.
00419   \param a_format  The message format.
00420   \param a_flags  The message flags.
00421   \param a_f0  The formatter for the zeroth parameter.
00422   \param a_f1  The formatter for the first parameter.
00423   \param a_f2  The formatter for the second parameter.
00424 */
00425 template <typename P0, typename P1, typename P2>
00426 message_stencil3<P0,P1,P2>::message_stencil3(const lstring &a_text, flags_type a_flags,
00427                 const ptr<f0_type> &a_f0, const ptr<f1_type> &a_f1, const ptr<f2_type> &a_f2):
00428         message_stencil(3,a_text,a_flags),
00429         f0(checked(a_f0)),
00430         f1(checked(a_f1)),
00431         f2(checked(a_f2))
00432 {
00433 }
00434 
00435 /*!
00436   Formats a messge from the stencil.
00437   \param p0  The zeroth argument to format.
00438   \param p1  The first argument to format.
00439   \param p2  The second argument to format.
00440   \return The formatted message.
00441 */
00442 template <typename P0, typename P1, typename P2>
00443 ptr<message> message_stencil3<P0,P1,P2>::format(const P0 &p0, const P1 &p1, const P2 &p2) const
00444 {
00445         ptr<args_type> args = args_type::create();
00446         args->push_back(f0->format(p0));
00447         args->push_back(f1->format(p1));
00448         args->push_back(f2->format(p2));
00449         return generate(args);
00450 }
00451 
00452 /*!
00453   Returns the two-parameter stencil, initializes with values to fill into the message.
00454   \pre  The format string is well-formed.
00455   \pre  a_f0 != NULL
00456   \pre  a_f1 != NULL
00457   \pre  a_f2 != NULL
00458   \param a_format  The format for the message, with % designating indexed parameter slots.
00459   \param a_flags  The flags for the message.
00460   \param a_f0  The formatter for the zeroth parameter.
00461   \param a_f1  The formatter for the first parameter.
00462   \param a_f2  The formatter for the second parameter.
00463   \return  The stencil with given values.
00464 */
00465 template <typename P0, typename P1, typename P2>
00466 ptr< message_stencil3<P0,P1,P2> > message_stencil3<P0,P1,P2>::create(const lstring &a_format, flags_type a_flags,
00467                 const ptr<f0_type> &a_f0, const ptr<f1_type> &a_f1, const ptr<f2_type> &a_f2)
00468 {
00469         return new message_stencil3(a_format,a_flags,a_f0,a_f1,a_f2);
00470 }
00471 
00472 end_package(msg);
00473 end_package(lestes);
00474 #endif
00475 /* vim: set ft=lestes : */

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