fcml  1.2.2
fcml_symbols.hpp
Go to the documentation of this file.
1 /*
2  * FCML - Free Code Manipulation Library.
3  * Copyright (C) 2010-2020 Slawomir Wojtasiak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
27 #ifndef FCML_SYMBOLS_HPP_
28 #define FCML_SYMBOLS_HPP_
29 
30 #include <new>
31 
32 #include "fcml_common.hpp"
33 #include "fcml_symbols.h"
34 
35 namespace fcml {
36 
41 class Symbol {
42 public:
43 
48  Symbol() :
49  _value(0) {
50  }
51 
59  Symbol(const fcml_cstring &name, fcml_int64_t value) :
60  _name(name), _value(value) {
61  }
62 
67  operator fcml_uint64_t() const {
68  return _value;
69  }
70 
75  operator fcml_int64_t() const {
76  return static_cast<fcml_int64_t>(_value);
77  }
78 
85  bool isEmpty() const {
86  return _name.empty();
87  }
88 
95  const fcml_cstring& getName() const {
96  return _name;
97  }
98 
105  fcml_uint64_t getValue() const {
106  return _value;
107  }
108 
115  void setName(const fcml_cstring &name) {
116  _name = name;
117  }
118 
125  void setValue(fcml_uint64_t value) {
126  _value = value;
127  }
128 
129 private:
130 
131  // Symbol name.
132  fcml_cstring _name;
133  // Symbol value.
134  fcml_uint64_t _value;
135 
136 };
137 
138 /* Due to the performance purposes and the way symbol table is managed
139  * internally, it has to be wrapped directly without doing any conversions
140  * when needed.
141  * @since 1.1.0
142  */
143 class SymbolTable: public NonCopyable {
144 public:
145 
152  _symbolTable(NULL) {
153  _symbolTable = ::fcml_fn_symbol_table_alloc();
154  if (!_symbolTable) {
155  throw InitException(
156  FCML_TEXT(
157  "Symbol table can not be initialized correctly."));
158  }
159  }
160 
165  virtual ~SymbolTable() {
166  if (_symbolTable) {
167  ::fcml_fn_symbol_table_free(_symbolTable);
168  _symbolTable = NULL;
169  }
170  }
171 
172 protected:
173 
174  friend class SymbolTableAware;
175 
182  return _symbolTable;
183  }
184 
185 public:
186 
194  void add(const fcml_cstring &symbolName, fcml_int64_t value) {
195  if (::fcml_fn_symbol_add_raw(_symbolTable, symbolName.c_str(), value)
197  throw std::bad_alloc();
198  }
199  }
200 
207  void remove(const fcml_cstring &symbolName) {
208  const fcml_string key = symbolName.c_str();
209  ::fcml_fn_symbol_remove(_symbolTable, key);
210  }
211 
219  bool contains(const fcml_cstring &symbolName) const {
220  return ::fcml_fn_symbol_get(_symbolTable, symbolName.c_str()) != NULL;
221  }
222 
231  Symbol get(const fcml_cstring &symbolName) const {
232  Symbol symbol;
233  fcml_st_symbol *found_symbol = ::fcml_fn_symbol_get(_symbolTable,
234  symbolName.c_str());
235  if (found_symbol) {
236  symbol.setName(found_symbol->symbol);
237  symbol.setValue(found_symbol->value);
238  }
239  return symbol;
240  }
241 
242 private:
244  fcml_st_symbol_table _symbolTable;
245 };
246 
252 public:
253 
260  return symbolTable.getSymbolTable();
261  }
262 };
263 
264 }
265 #endif /* FCML_SYMBOLS_HPP_ */
fcml_ptr fcml_st_symbol_table
Type for symbol tables.
Definition: fcml_symbols.h:35
C++ wrappers common classes.
bool contains(const fcml_cstring &symbolName) const
Gets true if there is a symbol for the given name in the table.
Definition: fcml_symbols.hpp:219
void setName(const fcml_cstring &name)
Sets a new symbol name.
Definition: fcml_symbols.hpp:115
There is not enough memory to complete operation.
Definition: fcml_errors.h:44
std::basic_string< fcml_char > fcml_cstring
By using this type definition here, it will be definitely much easier to support UNICODE in future re...
Definition: fcml_common.hpp:53
LIB_EXPORT void LIB_CALL fcml_fn_symbol_table_free(fcml_st_symbol_table symbol_table)
Frees a symbol table.
fcml_st_symbol_table & extractSymbolTable(SymbolTable &symbolTable)
Extracts the native symbol table for the given symbol table wrapper.
Definition: fcml_symbols.hpp:259
#define FCML_TEXT(x)
Used to code literal strings.
Definition: fcml_types.h:61
Definition: fcml_assembler.hpp:39
const fcml_cstring & getName() const
Gets the symbol name.
Definition: fcml_symbols.hpp:95
LIB_EXPORT void LIB_CALL fcml_fn_symbol_remove(fcml_st_symbol_table symbol_table, const fcml_string symbol)
Removes the symbol from the symbol table.
void setValue(fcml_uint64_t value)
Sets a new symbol value.
Definition: fcml_symbols.hpp:125
SymbolTable()
Creates an empty symbol table.
Definition: fcml_symbols.hpp:151
Derive from this class if you really need access to the native symbol table.
Definition: fcml_symbols.hpp:251
LIB_EXPORT fcml_st_symbol *LIB_CALL fcml_fn_symbol_get(fcml_st_symbol_table symbol_table, const fcml_string symbol)
Gets the symbol with the given name from the symbol table.
Symbol()
Creates an empty symbol.
Definition: fcml_symbols.hpp:48
Symbol(const fcml_cstring &name, fcml_int64_t value)
Creates a symbol instance for the given name and value.
Definition: fcml_symbols.hpp:59
LIB_EXPORT fcml_st_symbol_table LIB_CALL fcml_fn_symbol_table_alloc()
Allocates new symbol table.
fcml_st_symbol_table & getSymbolTable()
Gets native FCML symbol table.
Definition: fcml_symbols.hpp:181
virtual ~SymbolTable()
Destructor.
Definition: fcml_symbols.hpp:165
Represents one named symbol with associated value.
Definition: fcml_symbols.hpp:41
API for symbols handling.
Represents one named symbol with associated value.
Definition: fcml_symbols.h:42
LIB_EXPORT fcml_ceh_error LIB_CALL fcml_fn_symbol_add_raw(fcml_st_symbol_table symbol_table, const fcml_string symbol, fcml_int64_t value)
Adds new symbol to the symbol table.
fcml_uint64_t getValue() const
Gets the symbol value.
Definition: fcml_symbols.hpp:105
Definition: fcml_symbols.hpp:143
void add(const fcml_cstring &symbolName, fcml_int64_t value)
Adds a new symbol to the table.
Definition: fcml_symbols.hpp:194
Component can not be initialized correctly.
Definition: fcml_common.hpp:231
bool isEmpty() const
Gets true if symbol is empty.
Definition: fcml_symbols.hpp:85
Object which shouldn&#39;t be copied can inherit from this class.
Definition: fcml_common.hpp:288