1 |
/** |
2 |
* symbols.h: Symbol table. This is a Proof-Of-Concept and is not used anywhere yet. It may never get any use. |
3 |
* |
4 |
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
5 |
* Rights to this code are as documented in doc/pod/gplicense.pod. |
6 |
* |
7 |
* $Id: symbols.h,v 1.2 2007-09-05 11:23:13 pippijn Exp $ |
8 |
*/ |
9 |
|
10 |
#ifndef ERMYTH_SYMBOL_H |
11 |
#define ERMYTH_SYMBOL_H |
12 |
|
13 |
#include <boost/any.hpp> |
14 |
|
15 |
#include <map> |
16 |
#include <string> |
17 |
#include <stdexcept> |
18 |
|
19 |
class symbol_table |
20 |
{ |
21 |
typedef std::map<std::string const, boost::any> map_type; |
22 |
map_type symbol_map; |
23 |
|
24 |
public: |
25 |
template<typename T> |
26 |
void insert (std::string const &name, T symbol) |
27 |
{ |
28 |
try |
29 |
{ |
30 |
symbol_map.at (name); |
31 |
throw std::runtime_error ("There is already a symbol with name " + name + " in the table"); |
32 |
} |
33 |
catch (std::out_of_range &e) |
34 |
{ |
35 |
boost::any sym = symbol; |
36 |
symbol_map[name] = sym; |
37 |
} |
38 |
} |
39 |
|
40 |
template<typename T> |
41 |
T retrieve (std::string const &name) |
42 |
{ |
43 |
try |
44 |
{ |
45 |
boost::any sym = symbol_map.at (name); |
46 |
return boost::any_cast<T> (sym); |
47 |
} |
48 |
catch (std::out_of_range &) |
49 |
{ |
50 |
throw std::runtime_error ("Symbol table does not contain symbol " + name); |
51 |
} |
52 |
catch (boost::bad_any_cast &) |
53 |
{ |
54 |
throw std::bad_cast (); |
55 |
} |
56 |
} |
57 |
}; |
58 |
|
59 |
extern symbol_table symbols; |
60 |
|
61 |
#endif |