| 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 |
|
| 20 |
/** @file fcml_symbols.hpp |
| 21 |
* C++ API for symbols handling. Currently used only by parsers. |
| 22 |
* |
| 23 |
* @copyright Copyright (C) 2010-2020 Slawomir Wojtasiak. All rights reserved. |
| 24 |
* This project is released under the GNU Lesser General Public License. |
| 25 |
*/ |
| 26 |
|
| 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 |
|
| 37 |
/** |
| 38 |
* Represents one named symbol with associated value. |
| 39 |
* @since 1.1.0 |
| 40 |
*/ |
| 41 |
class Symbol { |
| 42 |
public: |
| 43 |
|
| 44 |
/** |
| 45 |
* Creates an empty symbol. |
| 46 |
* @since 1.1.0 |
| 47 |
*/ |
| 48 |
Symbol() : |
| 49 |
_value(0) { |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Creates a symbol instance for the given name and value. |
| 54 |
* |
| 55 |
* @param name The symbol name. |
| 56 |
* @param value The symbol value. |
| 57 |
* @since 1.1.0 |
| 58 |
*/ |
| 59 |
Symbol(const fcml_cstring &name, fcml_int64_t value) : |
| 60 |
_name(name), _value(value) { |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Gets symbol value. |
| 65 |
* @since 1.1.0 |
| 66 |
*/ |
| 67 |
operator fcml_uint64_t() const { |
| 68 |
return _value; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Gets symbol value. |
| 73 |
* @since 1.1.0 |
| 74 |
*/ |
| 75 |
operator fcml_int64_t() const { |
| 76 |
return static_cast<fcml_int64_t>(_value); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Gets true if symbol is empty. |
| 81 |
* |
| 82 |
* @return True if symbol is empty. |
| 83 |
* @since 1.1.0 |
| 84 |
*/ |
| 85 |
bool isEmpty() const { |
| 86 |
return _name.empty(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Gets the symbol name. |
| 91 |
* |
| 92 |
* @return The symbol name. |
| 93 |
* @since 1.1.0 |
| 94 |
*/ |
| 95 |
const fcml_cstring& getName() const { |
| 96 |
return _name; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Gets the symbol value. |
| 101 |
* |
| 102 |
* @return The symbol value. |
| 103 |
* @since 1.1.0 |
| 104 |
*/ |
| 105 |
fcml_uint64_t getValue() const { |
| 106 |
return _value; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Sets a new symbol name. |
| 111 |
* |
| 112 |
* @param name The symbol name. |
| 113 |
* @since 1.1.0 |
| 114 |
*/ |
| 115 |
void setName(const fcml_cstring &name) { |
| 116 |
_name = name; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Sets a new symbol value. |
| 121 |
* |
| 122 |
* @param value The symbol value. |
| 123 |
* @since 1.1.0 |
| 124 |
*/ |
| 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 |
|
| 146 |
/** |
| 147 |
* Creates an empty symbol table. |
| 148 |
* @throw InitException The symbol table can't be allocated. |
| 149 |
* @since 1.1.0 |
| 150 |
*/ |
| 151 |
SymbolTable() : |
| 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 |
|
| 161 |
/** |
| 162 |
* Destructor. |
| 163 |
* @since 1.1.0 |
| 164 |
*/ |
| 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 |
|
| 176 |
/** |
| 177 |
* Gets native FCML symbol table. |
| 178 |
* @return The native symbol table. |
| 179 |
* @since 1.1.0 |
| 180 |
*/ |
| 181 |
fcml_st_symbol_table& getSymbolTable() { |
| 182 |
return _symbolTable; |
| 183 |
} |
| 184 |
|
| 185 |
public: |
| 186 |
|
| 187 |
/** |
| 188 |
* Adds a new symbol to the table. |
| 189 |
* |
| 190 |
* @param symbolName The symbol name. |
| 191 |
* @param value The symbol value. |
| 192 |
* @since 1.1.0 |
| 193 |
*/ |
| 194 |
void add(const fcml_cstring &symbolName, fcml_int64_t value) { |
| 195 |
if (::fcml_fn_symbol_add_raw(_symbolTable, symbolName.c_str(), value) |
| 196 |
== FCML_CEH_GEC_OUT_OF_MEMORY) { |
| 197 |
throw std::bad_alloc(); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Removes symbol from the table. |
| 203 |
* |
| 204 |
* @param symbolName The symbol name. |
| 205 |
* @since 1.1.0 |
| 206 |
*/ |
| 207 |
void remove(const fcml_cstring &symbolName) { |
| 208 |
const fcml_string key = symbolName.c_str(); |
| 209 |
::fcml_fn_symbol_remove(_symbolTable, key); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Gets true if there is a symbol for the given name in the table. |
| 214 |
* |
| 215 |
* @param symbolName The symbol name. |
| 216 |
* @return true If symbol of the name exists in the table. |
| 217 |
* @since 1.1.0 |
| 218 |
*/ |
| 219 |
bool contains(const fcml_cstring &symbolName) const { |
| 220 |
return ::fcml_fn_symbol_get(_symbolTable, symbolName.c_str()) != NULL; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Gets the symbol of the given name. Checks Symbol.isEmpty method of the |
| 225 |
* returned symbol to check if there is such a symbol. |
| 226 |
* |
| 227 |
* @param symbolName The symbol name. |
| 228 |
* @return The found symbol. |
| 229 |
* @since 1.1.0 |
| 230 |
*/ |
| 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: |
| 243 |
/** The native FCML symbol table. */ |
| 244 |
fcml_st_symbol_table _symbolTable; |
| 245 |
}; |
| 246 |
|
| 247 |
/** |
| 248 |
* Derive from this class if you really need access to the native symbol table. |
| 249 |
* @since 1.1.0 |
| 250 |
*/ |
| 251 |
class SymbolTableAware { |
| 252 |
public: |
| 253 |
|
| 254 |
/** |
| 255 |
* Extracts the native symbol table for the given symbol table wrapper. |
| 256 |
* @param symbolTable The symbol table wrapper. |
| 257 |
* @since 1.1.0 |
| 258 |
*/ |
| 259 |
fcml_st_symbol_table& extractSymbolTable(SymbolTable &symbolTable) { |
| 260 |
return symbolTable.getSymbolTable(); |
| 261 |
} |
| 262 |
}; |
| 263 |
|
| 264 |
} |
| 265 |
#endif /* FCML_SYMBOLS_HPP_ */ |