fcml  1.1.1
fcml_symbols.hpp
Go to the documentation of this file.
1 /*
2  * FCML - Free Code Manipulation Library.
3  * Copyright (C) 2010-2015 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 internally, it
139  * has to be wrapped directly without doing any conversions when needed.
140  * @since 1.1.0
141  */
142 class SymbolTable: public NonCopyable {
143 public:
144 
150  SymbolTable() : _symbolTable(NULL) {
151  _symbolTable = ::fcml_fn_symbol_table_alloc();
152  if( !_symbolTable ) {
153  throw InitException( FCML_TEXT( "Symbol table can not be initialized correctly." ) );
154  }
155  }
156 
161  virtual ~SymbolTable() {
162  if( _symbolTable ) {
163  ::fcml_fn_symbol_table_free( _symbolTable );
164  _symbolTable = NULL;
165  }
166  }
167 
168 protected:
169 
170  friend class SymbolTableAware;
171 
178  return _symbolTable;
179  }
180 
181 public:
182 
190  void add( const fcml_cstring &symbolName, fcml_int64_t value ) {
191  if( ::fcml_fn_symbol_add_raw( _symbolTable, symbolName.c_str(), value ) == FCML_CEH_GEC_OUT_OF_MEMORY ) {
192  throw std::bad_alloc();
193  }
194  }
195 
202  void remove( const fcml_cstring &symbolName ) {
203  const fcml_string key = symbolName.c_str();
204  ::fcml_fn_symbol_remove( _symbolTable, key);
205  }
206 
214  bool contains( const fcml_cstring &symbolName ) const {
215  return ::fcml_fn_symbol_get( _symbolTable, symbolName.c_str() ) != NULL;
216  }
217 
226  Symbol get( const fcml_cstring &symbolName ) const {
227  Symbol symbol;
228  fcml_st_symbol *found_symbol = ::fcml_fn_symbol_get( _symbolTable, symbolName.c_str() );
229  if( found_symbol ) {
230  symbol.setName( found_symbol->symbol );
231  symbol.setValue( found_symbol->value );
232  }
233  return symbol;
234  }
235 
236 private:
238  fcml_st_symbol_table _symbolTable;
239 };
240 
246 public:
247 
254  return symbolTable.getSymbolTable();
255  }
256 };
257 
258 }
259 #endif /* FCML_SYMBOLS_HPP_ */
fcml_ptr fcml_st_symbol_table
Type for symbol tables.
Definition: fcml_symbols.h:35
const fcml_cstring & getName() const
Gets the symbol name.
Definition: fcml_symbols.hpp:95
C++ wrappers common classes.
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
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:253
#define FCML_TEXT(x)
Used to code literal strings.
Definition: fcml_types.h:61
Definition: fcml_assembler.hpp:39
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:150
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:214
Derive from this class if you really need access to the native symbol table.
Definition: fcml_symbols.hpp:245
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:177
virtual ~SymbolTable()
Destructor.
Definition: fcml_symbols.hpp:161
Represents one named symbol with associated value.
Definition: fcml_symbols.hpp:41
API for symbols handling.
bool isEmpty() const
Gets true if symbol is empty.
Definition: fcml_symbols.hpp:85
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:142
void add(const fcml_cstring &symbolName, fcml_int64_t value)
Adds a new symbol to the table.
Definition: fcml_symbols.hpp:190
Component can not be initialized correctly.
Definition: fcml_common.hpp:206
Object which shouldn't be copied can inherit from this class.
Definition: fcml_common.hpp:263