message_stencil.cc

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 /*! \file
00029   \brief Message stencils.
00030 
00031   Definition of message_stencil class representing parametrised message stencil.
00032   \author pt
00033 */
00034 #include <lestes/common.hh>
00035 #include <lestes/msg/message_stencil.hh>
00036 
00037 package(lestes);
00038 package(msg);
00039 
00040 /*!
00041   Creates the stencil, initializes with number of expected parameters and values to fill into the message.
00042   Assigns a unique id number, starting from zero.
00043   \pre  The format string is well-formed.
00044   \param a_nparams  Number of parameters for the stencil.
00045   \param a_format  The format for the message, with % designating indexed parameter slots.
00046   \param a_flags  The flags for the message.
00047 */
00048 message_stencil::message_stencil(ulint a_nparams, const lstring &a_format, flags_type a_flags):
00049         nparams(a_nparams),
00050         kind(kind_counter++),
00051         flags(a_flags),
00052         texts(texts_type::create()),
00053         params(params_type::create())
00054 {
00055         // attempt to fill internal structures for faster message creation
00056         parse(a_format);
00057 }
00058 
00059 /*!
00060   Returns the number of parameters for the message.
00061   \return  The number of parameters.
00062 */
00063 ulint message_stencil::nparams_get(void) const
00064 {
00065         return nparams;
00066 }
00067 
00068 /*!
00069   Returns the assigned unique identification number.
00070   The number is only for internal use and may vary between compilations.
00071   \return The kind identification number.
00072 */
00073 ulint message_stencil::kind_get(void) const
00074 {
00075         return kind;
00076 }
00077 
00078 /*!
00079   Tests equality to other stencil. Each stencil object represents one message
00080   kind and is thus unique.
00081   \param other  The stencil to compare to.
00082   \return true  If both stencils point to the same object.
00083 */
00084 bool message_stencil::equals(const ptr<message_stencil> &other) const
00085 {
00086         return other && other == this;
00087 }
00088 
00089 /*!
00090   Parses the format text.
00091   \pre The text is well-formed according to the class documentation.
00092   \param text  The text to parse.
00093 */
00094 void message_stencil::parse(const lstring &a_format)
00095 {
00096         bool percent = false;
00097         // start of not yet stored part
00098         ulint start = 0;
00099         lstring out;
00100         
00101         for (ulint i = 0, len = a_format.length(); i < len; i++) {
00102                 char c = a_format[i];
00103                 if (percent) {
00104                         // add the part of a_format before the % sequence
00105                         out += a_format.substr(start,i - start - 1);
00106                         if (c == '%') {
00107                                 out += '%';
00108                         } else {
00109                                 ucn u = character::create_from_host(c);
00110                         
00111                                 lassert2(character::is_digit(u),"Invalid `%' sequence.");
00112                                 ulint x = character::extract_digit(u);
00113                                 lassert2(x < nparams,"Invalid parameter in `%' sequence.");
00114                                 
00115                                 if (out.length()) {
00116                                         // flush the a_format before the parameter
00117                                         texts->push_back(out);
00118                                         out = "";
00119                                         // signal a_format output
00120                                         params->push_back(nparams);
00121                                 }
00122                                 params->push_back(x);
00123                         }
00124                         start = i + 1;
00125                         percent = false;
00126                 } else {
00127                         percent = (c == '%');
00128                 }
00129         }
00130 
00131         lassert2(!percent,"Unterminated `%' sequence.");
00132 
00133         out += a_format.substr(start);
00134         if (out.length()) {
00135                 // flush the last a_format
00136                 texts->push_back(out);
00137                 // signal a_format output
00138                 params->push_back(nparams);
00139         }
00140 }
00141 
00142 /*!
00143   Returns message with arguments filled into the parameter slots.
00144   \pre args != NULL
00145   \pre args->size() == nparams_get()
00146   \param args  The arguments for the message.
00147 */
00148 ptr<message> message_stencil::generate(const ptr<args_type> &args) const
00149 {
00150         lassert(args);
00151         lassert(args->size() == nparams);
00152         
00153         lstring result;
00154         
00155         texts_type::iterator tit = texts->begin();
00156         texts_type::iterator tend = texts->end();
00157         
00158         for (params_type::iterator it = params->begin(), end = params->end(); it != end; ++it) {
00159                 ulint idx = *it;
00160                 if (idx == nparams) {
00161                         lassert(tit != tend);
00162                         result += *tit;
00163                         ++tit;
00164                 } else {
00165                         lassert(idx < nparams);
00166                         // operator[] does no range check
00167                         result += args->operator[](idx);
00168                 }
00169         }
00170 
00171         // pass all stored info
00172         return message::create(kind,result,flags);
00173 }
00174 
00175 /*!
00176   Marks the object for garbage collection.
00177 */
00178 void message_stencil::gc_mark(void)
00179 {
00180         texts.gc_mark();
00181         params.gc_mark();
00182         object::gc_mark();
00183 }
00184 
00185 /*!
00186   Internal message stencil instance identification number counter.
00187 */
00188 ulint message_stencil::kind_counter = 0;
00189 
00190 end_package(msg);
00191 end_package(lestes);
00192 /* vim: set ft=lestes : */
00193 

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