cfl_symboltable.h
Go to the documentation of this file.
1 /** @file cfl_symboltable.h @brief Class SymbolTable */
2 
3 /* FAU Discrete Event Systems Library (libfaudes)
4 
5 Copyright (C) 2006 Bernd Opitz
6 Copywrite (C) 2007 Thomas Moor
7 Exclusive copyright is granted to Klaus Schmidt
8 
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13 
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 
24 #ifndef FAUDES_SYMBOLTABLE_H
25 #define FAUDES_SYMBOLTABLE_H
26 
27 #include "cfl_definitions.h"
28 #include "cfl_types.h"
29 #include "cfl_exception.h"
30 #include "cfl_helper.h"
31 #include "cfl_indexset.h"
32 
33 #include <algorithm>
34 #include <map>
35 #include <set>
36 #include <limits>
37 #include <iostream>
38 #include <sstream>
39 
40 
41 
42 namespace faudes {
43 
44 // forward
45 class IndexSet;
46 
47 /**
48  * A SymbolTable associates sybolic names with indices.
49  *
50  * Symbolic names are restricted to printable ascii, no quoatation ("),
51  * no hash (#) and and no blanks. Indices are of type faudes Idx aka (long unsigned)
52  * integer and must be positive. Both, indices and symbolic names must
53  * be unique within each SymbolTable. Consistency checks on input data
54  * are preformed, regardless of the FAUDES_CHECKED compiletime option.
55  *
56  * Generators refer to a global symboltable for event names and hold a local
57  * symboltable for state names.
58  *
59  *
60  */
61 class FAUDES_API SymbolTable : public Type {
62 public:
63 
64  /** Creates a new SymbolTable */
65  SymbolTable(void);
66 
67  /** Copy constructor */
68  SymbolTable(const SymbolTable& rSrc);
69 
70  /** Return name of SymbolTable */
71  const std::string& Name(void) const;
72 
73  /** Set name of SymbolTable */
74  void Name(const std::string& rName);
75 
76  /** Size of symboltabel */
77  Idx Size(void) const;
78 
79  /** Clear all entries */
80  void Clear(void);
81 
82 
83  /** Get maximum index which this SymbolTable accepts */
84  Idx MaxIndex(void) const;
85 
86  /**
87  * Set maximum index which this SymbolTable accepts
88  *
89  * @param index
90  * New maximum index
91  * @exception Exception
92  * - symboltable overflow (id 40)
93  */
94  void MaxIndex(Idx index);
95 
96  /** Get the largest index in use */
97  Idx LastIndex(void) const;
98 
99  /**
100  * Symbolic name lookup
101  *
102  * @param index
103  * Index to lookup
104  * @return
105  * Symbolic name of index, or empty string "" for non-existent index
106  */
107  std::string Symbol(Idx index) const;
108 
109  /**
110  * Index lookup
111  *
112  * @param rName
113  * Symbolic name to lookup
114  *
115  * @return
116  * Index of symbolic name, or 0 for non-existent name
117  */
118  Idx Index(const std::string& rName) const;
119 
120  /**
121  * Test existence of index
122  *
123  * @param index
124  * Index to test
125  * @return
126  * True, if index exists
127  */
128  bool Exists(Idx index) const;
129 
130  /**
131  * Test existence of symbol
132  *
133  * @param rName
134  * Symbolic name to test
135  * @return
136  * True, if name exists
137  */
138  bool Exists(const std::string& rName) const;
139 
140  /**
141  * Test validiy of candidate symbol.
142  * The current implementation insists in printable ascii,
143  * no quotes ("), no hash (#) and no blanks.
144  *
145  * @param rName
146  * Symbolic name to test
147  * @return
148  * True, if name is a valid symbol
149  */
150  static bool ValidSymbol(const std::string& rName);
151 
152  /**
153  * Create unique symbolic name by adding an underscore and extra digits.
154  * The current implementation tries to be smart and overwrites
155  * any previously added digits. It is also slow.
156  *
157  * @param rName
158  * Candidate symbolic name
159  * @return
160  * Unique symbolic name
161  */
162  std::string UniqueSymbol(const std::string& rName) const;
163 
164  /**
165  * Add new entry (aka symbolic name and index) to symboltable,
166  *
167  * @param index
168  * New index
169  * @param rName
170  * New symbolic name
171  * @return
172  * New index
173  * @exception Exception
174  * - name already associated with another index (id 41)
175  * - index already associated with another name (id 42)
176  * - invalid name (id 43)
177  */
178  Idx InsEntry(Idx index, const std::string& rName);
179 
180 
181  /**
182  * Merge a symbolic name with symboltable.
183  * If the symbol does not exist, find a new index and add the new entry.
184  * If the symbol does exist, lookup its index.
185  *
186  * @param rName
187  * Symbolic name to merge
188  * @return
189  * Index of rName
190  * @exception Exception
191  * - invalid name (id 43)
192  */
193  Idx InsEntry(const std::string& rName);
194 
195 
196  /**
197  * Set symbolic name for existing entry.
198  * If the name is the emptystring, an existing entry is cleared.
199  * All other invalid names throw an exception.
200  *
201  * @param index
202  * Index to specify entry
203  * @param rName
204  * New esymbolic name for index
205  *
206  * @exception Exception
207  * - name already associated with another index (id 41)
208  * - invalid name (id 43)
209  */
210  void SetEntry(Idx index, const std::string& rName);
211 
212  /**
213  * Set default names ("1", "2", "3", ...) for index.
214  *
215  * @param index
216  * Index for which to set the default name
217  */
218  void SetDefaultSymbol(Idx index);
219 
220  /**
221  * Delete entry by index
222  *
223  * @param index
224  * Index to delete
225  *
226  */
227  void ClrEntry(Idx index);
228 
229  /**
230  * Delete entry by symbol
231  *
232  * @param rName
233  * Symbolic name to delete
234  *
235  */
236  void ClrEntry(const std::string& rName);
237 
238  /**
239  * Restrict to specified indicees.
240  *
241  * @param rDomain
242  * Indicees to keep.
243  *
244  */
245  void RestrictDomain(const IndexSet& rDomain);
246 
247  /**
248  * Get Static Symboltable ref
249  * (initialize on first use pattern)
250  */
251  static SymbolTable* GlobalEventSymbolTablep(void);
252 
253  protected:
254 
255  /** assign my members */
256  void DoAssign(const SymbolTable& rSrc);
257 
258  private:
259 
260  /** Name of the SymbolTable */
261  std::string mMyName;
262 
263  /** Index lookup map */
264  std::map<std::string,Idx> mIndexMap;
265 
266  /** Name lookup map */
267  std::map<Idx,std::string> mNameMap;
268 
269  /** Upper limit (incl) */
271 
272  /** Largest used index + 1 */
274 
275  /** Symboltable token io */
276  void DoRead(TokenReader& rTr, const std::string& rLabel = "", const Type* pContext=0);
277 
278  /** Symboltable token io */
279  void DoWrite(TokenWriter& rTw, const std::string& rLabel="",const Type* pContext=0) const;
280 };
281 
282 
283 } // namespace faudes
284 
285 #endif
Compiletime options.
Class Exception.
Helper functions.
Classes IndexSet, TaIndexSet.
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:81
Runtime interface, faudes types.
Set of indices.
Definition: cfl_indexset.h:78
A SymbolTable associates sybolic names with indices.
Idx mNextIndex
Largest used index + 1.
std::map< Idx, std::string > mNameMap
Name lookup map.
std::map< std::string, Idx > mIndexMap
Index lookup map.
Idx mMaxIndex
Upper limit (incl)
std::string mMyName
Name of the SymbolTable.
A TokenReader reads sequential tokens from a file or string.
A TokenWriter writes sequential tokens to a file, a string or stdout.
Base class of all libFAUDES objects that participate in the run-time interface.
Definition: cfl_types.h:239
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen