libFAUDES

Sections

Index

symboltable.h

Go to the documentation of this file.
00001 /** @file 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 "definitions.h"
00028 #include "rtitypes.h"
00029 #include "exception.h"
00030 #include "helper.h" 
00031 #include "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  * 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   /** Return name of SymbolTable */
00068   const std::string& Name(void) const;
00069     
00070   /** Set name of SymbolTable */
00071   void Name(const std::string& rName);
00072 
00073   /** Size of symboltabel */
00074   Idx Size(void);
00075 
00076   /** Clear all entries */
00077   void Clear(void);
00078 
00079 
00080   /** Get maximum index which this SymbolTable accepts */
00081   Idx MaxIndex(void) const;
00082 
00083   /** 
00084    * Set maximum index which this SymbolTable accapts
00085    *
00086    * @param index
00087    *   New maximum index
00088    * @exception Exception
00089    *   - symboltable overflow (id 40)
00090    */
00091   void MaxIndex(Idx index);
00092 
00093   /** Get the largest index in use */
00094   Idx LastIndex(void) const;
00095 
00096   /** 
00097    * Symbolic name lookup
00098    *
00099    * @param index
00100    *   Index to lookup
00101    * @return
00102    *   Symbolic name of index, or empty string "" for non-existent index
00103    */
00104   std::string Symbol(Idx index) const;
00105 
00106   /** 
00107    * Index lookup
00108    *
00109    * @param rName
00110    *   Symbolic name to lookup
00111    * 
00112    * @return 
00113    *   Index of symbolic name, or 0 for non-existent name
00114    */
00115   Idx Index(const std::string& rName) const;
00116 
00117   /** 
00118    * Test existence of index 
00119    *
00120    * @param index
00121    *   Index to test
00122    * @return 
00123    *   True, if index exists
00124    */
00125   bool Exists(Idx index) const;
00126   
00127   /** 
00128    * Test existence of symbol
00129    *
00130    * @param rName
00131    *   Symbolic name to test
00132    * @return
00133    *   True, if name exists
00134    */
00135   bool Exists(const std::string& rName) const;
00136 
00137 
00138   /** 
00139    * Test validiy of candidate symbol.
00140    * The current implementation insists in printable ascii,
00141    * no quotes (") and no blanks. The no-quotes-restriction
00142    * simplifies file io (see tokenreader.h), other restrictions
00143    * are cosmetic. 
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, the 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 
00253  private:
00254 
00255   /** Name of the SymbolTable */
00256   std::string mMyName;
00257     
00258   /** Index lookup map */
00259   std::map<std::string,Idx> mIndexMap;
00260     
00261   /** Name lookup map */
00262   std::map<Idx,std::string> mNameMap;
00263 
00264   /** Upper limit (incl) */
00265   Idx mMaxIndex;
00266     
00267   /** Largest used index + 1 */
00268   Idx mNextIndex;
00269 
00270   /** static event symbol table (should go to faudes::vGenerator, or faudes) */
00271   static SymbolTable msEventSymbolTable;
00272 
00273   /** Dummy, symboltable does not provide token io */
00274   void DoRead(TokenReader& rTr,  const std::string& rLabel = "", const Type* pContext=0);
00275 
00276   /** Dummy, symboltable does not provide token io */
00277   void DoWrite(TokenWriter& rTw, const std::string& rLabel="",const Type* pContext=0) const;
00278 };
00279 
00280 
00281 } // namespace faudes
00282 
00283 #endif

libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6