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

libFAUDES 2.33h --- 2025.06.18 --- c++ api documentaion by doxygen