txt-eng.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2009-2012 Michael Goffioul
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if ! defined (txt_eng_h)
00024 #define txt_eng_h 1
00025 
00026 #include "base-list.h"
00027 
00028 class text_element;
00029 class text_element_string;
00030 class text_element_list;
00031 class text_subscript_element;
00032 class text_superscript_element;
00033 
00034 class text_processor;
00035 
00036 class
00037 OCTINTERP_API
00038 text_element
00039 {
00040 public:
00041   text_element (void) { }
00042 
00043   virtual ~text_element (void) { }
00044 
00045   virtual void accept (text_processor& p) = 0;
00046 
00047 private:
00048   text_element (const text_element&);
00049 };
00050 
00051 class
00052 OCTINTERP_API
00053 text_element_string : public text_element
00054 {
00055 public:
00056   text_element_string (const std::string& s = "")
00057       : text_element (), str (s) { }
00058 
00059   ~text_element_string (void) { }
00060 
00061   std::string string_value (void) const { return str; }
00062 
00063   void accept (text_processor& p);
00064 
00065 private:
00066   std::string str;
00067 
00068 private:
00069   text_element_string (const text_element_string &);
00070 };
00071 
00072 class
00073 OCTINTERP_API
00074 text_element_list :
00075     public text_element,
00076     public octave_base_list<text_element *>
00077 {
00078 public:
00079   text_element_list (void)
00080       : text_element (), octave_base_list<text_element*> () { }
00081 
00082   ~text_element_list (void)
00083     {
00084       while (! empty ())
00085         {
00086           iterator it = begin ();
00087           delete (*it);
00088           erase (it);
00089         }
00090     }
00091 
00092   void accept (text_processor& p);
00093 };
00094 
00095 class
00096 OCTINTERP_API
00097 text_subscript_element : public text_element_list
00098 {
00099 public:
00100   text_subscript_element (void)
00101       : text_element_list () { }
00102 
00103   ~text_subscript_element (void) { }
00104 
00105   void accept (text_processor& p);
00106 };
00107 
00108 class
00109 OCTINTERP_API
00110 text_superscript_element : public text_element_list
00111 {
00112 public:
00113   text_superscript_element (void)
00114       : text_element_list () { }
00115 
00116   ~text_superscript_element (void) { }
00117 
00118   void accept (text_processor& p);
00119 };
00120 
00121 class
00122 OCTINTERP_API
00123 text_processor
00124 {
00125 public:
00126   virtual void visit (text_element_string& e) = 0;
00127 
00128   virtual void visit (text_element_list& e)
00129     {
00130       for (text_element_list::iterator it = e.begin ();
00131            it != e.end (); ++it)
00132         {
00133           (*it)->accept (*this);
00134         }
00135     }
00136 
00137   virtual void visit (text_subscript_element& e)
00138     { visit (dynamic_cast<text_element_list&> (e)); }
00139 
00140   virtual void visit (text_superscript_element& e)
00141     { visit (dynamic_cast<text_element_list&> (e)); }
00142 
00143   virtual void reset (void) { }
00144 
00145 protected:
00146   text_processor (void) { }
00147 
00148   virtual ~text_processor (void) { }
00149 };
00150 
00151 #define TEXT_ELEMENT_ACCEPT(cls) \
00152 inline void \
00153 cls::accept (text_processor& p) \
00154 { p.visit (*this); }
00155 
00156 TEXT_ELEMENT_ACCEPT(text_element_string)
00157 TEXT_ELEMENT_ACCEPT(text_element_list)
00158 TEXT_ELEMENT_ACCEPT(text_subscript_element)
00159 TEXT_ELEMENT_ACCEPT(text_superscript_element)
00160 
00161 class
00162 OCTINTERP_API
00163 text_parser
00164 {
00165 public:
00166   text_parser (void) { }
00167 
00168   virtual ~text_parser (void) { }
00169 
00170   virtual text_element* parse (const std::string& s) = 0;
00171 };
00172 
00173 class
00174 OCTINTERP_API
00175 text_parser_none : public text_parser
00176 {
00177 public:
00178   text_parser_none (void) : text_parser () { }
00179 
00180   ~text_parser_none (void) { }
00181 
00182   // FIXME: is it possible to use reference counting to manage the
00183   // memory for the object returned by the text parser?  That would be
00184   // preferable to having to know when and where to delete the object it
00185   // creates...
00186 
00187   text_element* parse (const std::string& s)
00188     {
00189       return new text_element_string (s);
00190     }
00191 };
00192 
00193 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines