cfl_symboltable.h

Go to the documentation of this file.
00001 /** @file cfl_symboltable.h @brief Class SymbolTable */
00002 
00003 /* FAU Discrete Event Systems Library (libfaudes)
00004 
00005 Copyright (C) 2006  Bernd Opitz
00006 Copywrite (C) 2007 Thomas Moor
00007 Exclusive copyright is granted to Klaus Schmidt
00008 
00009 This library is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU Lesser General Public
00011 License as published by the Free Software Foundation; either
00012 version 2.1 of the License, or (at your option) any later version.
00013 
00014 This library is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public
00020 License along with this library; if not, write to the Free Software
00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00022 
00023 
00024 #ifndef FAUDES_SYMBOLTABLE_H
00025 #define FAUDES_SYMBOLTABLE_H
00026 
00027 #include "cfl_definitions.h"
00028 #include "cfl_types.h"
00029 #include "cfl_exception.h"
00030 #include "cfl_helper.h" 
00031 #include "cfl_indexset.h" 
00032 
00033 #include <algorithm>
00034 #include <map>
00035 #include <set>
00036 #include <limits>
00037 #include <iostream>
00038 #include <sstream>
00039 
00040 
00041 
00042 namespace faudes {
00043 
00044 // forward
00045 class IndexSet;
00046 
00047 /**  
00048  * A SymbolTable associates sybolic names with indices. 
00049  *
00050  * Symbolic names are restricted to printable ascii, no quoatation ("),
00051  * no hash (#) and and no blanks. Indices are of type faudes Idx aka (long unsigned) 
00052  * integer and must be positive. Both, indices and symbolic names must
00053  * be unique within each SymbolTable. Consistency checks on input data
00054  * are preformed, regardless of the FAUDES_CHECKED compiletime option.
00055  *
00056  * Generators refer to a global symboltable for event names and hold a local 
00057  * symboltable for state names.
00058  *
00059  *
00060  */
00061 class SymbolTable : public Type {
00062 public:
00063 
00064   /** Creates a new SymbolTable */
00065   SymbolTable(void);
00066 
00067   /** Copy constructor */
00068   SymbolTable(const SymbolTable& rSrc);
00069 
00070   /** Return name of SymbolTable */
00071   const std::string& Name(void) const;
00072     
00073   /** Set name of SymbolTable */
00074   void Name(const std::string& rName);
00075 
00076   /** Size of symboltabel */
00077   Idx Size(void) const;
00078 
00079   /** Clear all entries */
00080   void Clear(void);
00081 
00082 
00083   /** Get maximum index which this SymbolTable accepts */
00084   Idx MaxIndex(void) const;
00085 
00086   /** 
00087    * Set maximum index which this SymbolTable accepts
00088    *
00089    * @param index
00090    *   New maximum index
00091    * @exception Exception
00092    *   - symboltable overflow (id 40)
00093    */
00094   void MaxIndex(Idx index);
00095 
00096   /** Get the largest index in use */
00097   Idx LastIndex(void) const;
00098 
00099   /** 
00100    * Symbolic name lookup
00101    *
00102    * @param index
00103    *   Index to lookup
00104    * @return
00105    *   Symbolic name of index, or empty string "" for non-existent index
00106    */
00107   std::string Symbol(Idx index) const;
00108 
00109   /** 
00110    * Index lookup
00111    *
00112    * @param rName
00113    *   Symbolic name to lookup
00114    * 
00115    * @return 
00116    *   Index of symbolic name, or 0 for non-existent name
00117    */
00118   Idx Index(const std::string& rName) const;
00119 
00120   /** 
00121    * Test existence of index 
00122    *
00123    * @param index
00124    *   Index to test
00125    * @return 
00126    *   True, if index exists
00127    */
00128   bool Exists(Idx index) const;
00129   
00130   /** 
00131    * Test existence of symbol
00132    *
00133    * @param rName
00134    *   Symbolic name to test
00135    * @return
00136    *   True, if name exists
00137    */
00138   bool Exists(const std::string& rName) const;
00139 
00140   /** 
00141    * Test validiy of candidate symbol.
00142    * The current implementation insists in printable ascii,
00143    * no quotes ("), no hash (#) and no blanks. 
00144    *
00145    * @param rName
00146    *   Symbolic name to test
00147    * @return
00148    *   True, if name is a valid symbol
00149    */
00150   static bool ValidSymbol(const std::string& rName);
00151     
00152   /** 
00153    * Create unique symbolic name by adding an underscore and extra digits.
00154    * The current implementation tries to be smart and overwrites
00155    * any previously added digits. It is also slow.
00156    *
00157    * @param rName
00158    *   Candidate symbolic name
00159    * @return
00160    *   Unique symbolic name
00161    */
00162   std::string UniqueSymbol(const std::string& rName) const;
00163     
00164   /** 
00165    * Add new entry (aka symbolic name and index) to symboltable, 
00166    * 
00167    * @param index
00168    *   New index
00169    * @param rName
00170    *   New symbolic name
00171    * @return
00172    *   New index
00173    * @exception Exception
00174    *   - name already associated with another index (id 41)
00175    *   - index already associated with another name (id 42)
00176    *   - invalid name (id 43)
00177    */
00178   Idx InsEntry(Idx index, const std::string& rName);
00179 
00180     
00181   /** 
00182    * Merge a symbolic name with symboltable.
00183    * If the symbol does not exist, find a new index and add the new entry. 
00184    * If the symbol does exist, lookup its index.
00185    * 
00186    * @param rName
00187    *   Symbolic name to merge
00188    * @return
00189    *   Index of rName
00190    * @exception Exception
00191    *   - invalid name (id 43)
00192    */
00193   Idx InsEntry(const std::string& rName);
00194 
00195 
00196   /** 
00197    * Set symbolic name for existing entry.
00198    * If the name is the emptystring, an existing entry is cleared.
00199    * All other invalid names throw an exception.
00200    *
00201    * @param index
00202    *   Index to specify entry
00203    * @param rName
00204    *   New esymbolic name for index
00205    *
00206    * @exception Exception
00207    *   - name already associated with another index (id 41)
00208    *   - invalid name (id 43)
00209    */
00210   void SetEntry(Idx index, const std::string& rName);
00211     
00212   /**
00213    * Set default names ("1", "2", "3", ...) for index. 
00214    *
00215    * @param index
00216    *   Index for which to set the default name
00217    */
00218   void SetDefaultSymbol(Idx index);
00219 
00220   /** 
00221    * Delete entry by index 
00222    *
00223    * @param index
00224    *   Index to delete
00225    *
00226    */
00227   void ClrEntry(Idx index);
00228     
00229   /** 
00230    * Delete entry by symbol
00231    *
00232    * @param rName
00233    *   Symbolic name to delete
00234    * 
00235    */
00236   void ClrEntry(const std::string& rName);
00237 
00238   /** 
00239    * Restrict to specified indicees.
00240    *
00241    * @param rDomain
00242    *   Indicees to keep.
00243    * 
00244    */
00245   void RestrictDomain(const IndexSet& rDomain);
00246 
00247   /**
00248    * Get Static Symboltable ref
00249    */
00250   static SymbolTable* GlobalEventSymbolTablep(void);
00251 
00252  protected:
00253    
00254   /** assign my members */
00255   virtual void DoAssign(const SymbolTable& rSrc);
00256 
00257  private:
00258 
00259   /** Name of the SymbolTable */
00260   std::string mMyName;
00261     
00262   /** Index lookup map */
00263   std::map<std::string,Idx> mIndexMap;
00264     
00265   /** Name lookup map */
00266   std::map<Idx,std::string> mNameMap;
00267 
00268   /** Upper limit (incl) */
00269   Idx mMaxIndex;
00270     
00271   /** Largest used index + 1 */
00272   Idx mNextIndex;
00273 
00274   /** static event symbol table (should go to faudes::Generator, or faudes) */
00275   static SymbolTable msEventSymbolTable;
00276 
00277   /** Symboltable token io */
00278   void DoRead(TokenReader& rTr,  const std::string& rLabel = "", const Type* pContext=0);
00279 
00280   /** Symboltable token io */
00281   void DoWrite(TokenWriter& rTw, const std::string& rLabel="",const Type* pContext=0) const;
00282 };
00283 
00284 
00285 } // namespace faudes
00286 
00287 #endif

libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen