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, 2024 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_utils.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  using Type::operator=;
65 
66  /** Creates a new SymbolTable */
67  SymbolTable(void);
68 
69  /** Copy constructor */
70  SymbolTable(const SymbolTable& rSrc);
71 
72  /** Return name of SymbolTable */
73  const std::string& Name(void) const;
74 
75  /** Set name of SymbolTable */
76  void Name(const std::string& rName);
77 
78  /** Size of symboltabel */
79  Idx Size(void) const;
80 
81  /** Clear all entries */
82  void Clear(void);
83 
84 
85  /** Get maximum index which this SymbolTable accepts */
86  Idx MaxIndex(void) const;
87 
88  /**
89  * Set maximum index which this SymbolTable accepts
90  *
91  * @param index
92  * New maximum index
93  * @exception Exception
94  * - symboltable overflow (id 40)
95  */
96  void MaxIndex(Idx index);
97 
98  /** Get the largest index in use */
99  Idx LastIndex(void) const;
100 
101  /**
102  * Symbolic name lookup
103  *
104  * @param index
105  * Index to lookup
106  * @return
107  * Symbolic name of index, or empty string "" for non-existent index
108  */
109  std::string Symbol(Idx index) const;
110 
111  /**
112  * Index lookup
113  *
114  * @param rName
115  * Symbolic name to lookup
116  *
117  * @return
118  * Index of symbolic name, or 0 for non-existent name
119  */
120  Idx Index(const std::string& rName) const;
121 
122  /**
123  * Test existence of index
124  *
125  * @param index
126  * Index to test
127  * @return
128  * True, if index exists
129  */
130  bool Exists(Idx index) const;
131 
132  /**
133  * Test existence of symbol
134  *
135  * @param rName
136  * Symbolic name to test
137  * @return
138  * True, if name exists
139  */
140  bool Exists(const std::string& rName) const;
141 
142  /**
143  * Test validiy of candidate symbol.
144  * The current implementation insists in printable ascii,
145  * no quotes ("), no hash (#) and no blanks.
146  *
147  * @param rName
148  * Symbolic name to test
149  * @return
150  * True, if name is a valid symbol
151  */
152  static bool ValidSymbol(const std::string& rName);
153 
154  /**
155  * Create unique symbolic name by adding an underscore and extra digits.
156  * The current implementation tries to be smart and overwrites
157  * any previously added digits. It is also slow.
158  *
159  * @param rName
160  * Candidate symbolic name
161  * @return
162  * Unique symbolic name
163  */
164  std::string UniqueSymbol(const std::string& rName) const;
165 
166  /**
167  * Add new entry (aka symbolic name and index) to symboltable,
168  *
169  * @param index
170  * New index
171  * @param rName
172  * New symbolic name
173  * @return
174  * New index
175  * @exception Exception
176  * - name already associated with another index (id 41)
177  * - index already associated with another name (id 42)
178  * - invalid name (id 43)
179  */
180  Idx InsEntry(Idx index, const std::string& rName);
181 
182 
183  /**
184  * Merge a symbolic name with symboltable.
185  * If the symbol does not exist, find a new index and add the new entry.
186  * If the symbol does exist, lookup its index.
187  *
188  * @param rName
189  * Symbolic name to merge
190  * @return
191  * Index of rName
192  * @exception Exception
193  * - invalid name (id 43)
194  */
195  Idx InsEntry(const std::string& rName);
196 
197 
198  /**
199  * Set symbolic name for existing entry.
200  * If the name is the emptystring, an existing entry is cleared.
201  * All other invalid names throw an exception.
202  *
203  * @param index
204  * Index to specify entry
205  * @param rName
206  * New esymbolic name for index
207  *
208  * @exception Exception
209  * - name already associated with another index (id 41)
210  * - invalid name (id 43)
211  */
212  void SetEntry(Idx index, const std::string& rName);
213 
214  /**
215  * Set default names ("1", "2", "3", ...) for index.
216  *
217  * @param index
218  * Index for which to set the default name
219  */
220  void SetDefaultSymbol(Idx index);
221 
222  /**
223  * Delete entry by index
224  *
225  * @param index
226  * Index to delete
227  *
228  */
229  void ClrEntry(Idx index);
230 
231  /**
232  * Delete entry by symbol
233  *
234  * @param rName
235  * Symbolic name to delete
236  *
237  */
238  void ClrEntry(const std::string& rName);
239 
240  /**
241  * Restrict to specified indicees.
242  *
243  * @param rDomain
244  * Indicees to keep.
245  *
246  */
247  void RestrictDomain(const IndexSet& rDomain);
248 
249  /**
250  * Get Static Symboltable ref
251  * (initialize on first use pattern)
252  */
253  static SymbolTable* GlobalEventSymbolTablep(void);
254 
255  protected:
256 
257  /** assign my members */
258  void DoAssign(const SymbolTable& rSrc);
259 
260  private:
261 
262  /** Name of the SymbolTable */
263  std::string mMyName;
264 
265  /** Index lookup map */
266  std::map<std::string,Idx> mIndexMap;
267 
268  /** Name lookup map */
269  std::map<Idx,std::string> mNameMap;
270 
271  /** Upper limit (incl) */
273 
274  /** Largest used index + 1 */
276 
277  /** Symboltable token io */
278  void DoRead(TokenReader& rTr, const std::string& rLabel = "", const Type* pContext=0);
279 
280  /** Symboltable token io */
281  void DoWrite(TokenWriter& rTw, const std::string& rLabel="",const Type* pContext=0) const;
282 };
283 
284 
285 } // namespace faudes
286 
287 #endif
Compiletime options.
Class Exception.
Classes IndexSet, TaIndexSet.
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:80
Runtime interface, faudes types.
C-level utilities functions.
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.32f --- 2024.12.22 --- c++ api documentaion by doxygen