| 1 |
// file : cutl/compiler/context.hxx |
| 2 |
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC |
| 3 |
// license : MIT; see accompanying LICENSE file |
| 4 |
|
| 5 |
#ifndef CUTL_COMPILER_CONTEXT_HXX |
| 6 |
#define CUTL_COMPILER_CONTEXT_HXX |
| 7 |
|
| 8 |
#include <map> |
| 9 |
#include <string> |
| 10 |
#include <cstddef> // std::size_t |
| 11 |
#include <typeinfo> |
| 12 |
|
| 13 |
#include <cutl/exception.hxx> |
| 14 |
#include <cutl/container/any.hxx> |
| 15 |
|
| 16 |
#include <cutl/details/export.hxx> |
| 17 |
|
| 18 |
namespace cutl |
| 19 |
{ |
| 20 |
namespace compiler |
| 21 |
{ |
| 22 |
class LIBCUTL_EXPORT context |
| 23 |
{ |
| 24 |
public: |
| 25 |
struct LIBCUTL_EXPORT no_entry: exception {}; |
| 26 |
struct LIBCUTL_EXPORT typing: exception {}; |
| 27 |
|
| 28 |
public: |
| 29 |
context () {} |
| 30 |
|
| 31 |
void |
| 32 |
swap (context& c) |
| 33 |
{ |
| 34 |
map_.swap (c.map_); |
| 35 |
} |
| 36 |
|
| 37 |
private: |
| 38 |
context (context const&); |
| 39 |
|
| 40 |
context& |
| 41 |
operator= (context const&); |
| 42 |
|
| 43 |
public: |
| 44 |
std::size_t |
| 45 |
count (char const* key) const |
| 46 |
{ |
| 47 |
return count (std::string (key)); |
| 48 |
} |
| 49 |
|
| 50 |
std::size_t |
| 51 |
count (std::string const& key) const |
| 52 |
{ |
| 53 |
return map_.count (key); |
| 54 |
} |
| 55 |
|
| 56 |
template <typename X> |
| 57 |
X& |
| 58 |
get (char const* key) |
| 59 |
{ |
| 60 |
return get<X> (std::string (key)); |
| 61 |
} |
| 62 |
|
| 63 |
template <typename X> |
| 64 |
X& |
| 65 |
get (std::string const& key); |
| 66 |
|
| 67 |
template <typename X> |
| 68 |
X const& |
| 69 |
get (char const* key) const |
| 70 |
{ |
| 71 |
return get<X> (std::string (key)); |
| 72 |
} |
| 73 |
|
| 74 |
template <typename X> |
| 75 |
X const& |
| 76 |
get (std::string const& key) const; |
| 77 |
|
| 78 |
template <typename X> |
| 79 |
X const& |
| 80 |
get (char const* key, X const& default_value) const |
| 81 |
{ |
| 82 |
return get<X> (std::string (key), default_value); |
| 83 |
} |
| 84 |
|
| 85 |
template <typename X> |
| 86 |
X const& |
| 87 |
get (std::string const& key, X const& default_value) const; |
| 88 |
|
| 89 |
template <typename X> |
| 90 |
X& |
| 91 |
set (char const* key, X const& value) |
| 92 |
{ |
| 93 |
return set<X> (std::string (key), value); |
| 94 |
} |
| 95 |
|
| 96 |
template <typename X> |
| 97 |
X& |
| 98 |
set (std::string const& key, X const& value); |
| 99 |
|
| 100 |
void |
| 101 |
set (char const* key, container::any const& value) |
| 102 |
{ |
| 103 |
return set (std::string (key), value); |
| 104 |
} |
| 105 |
|
| 106 |
void |
| 107 |
set (std::string const& key, container::any const& value); |
| 108 |
|
| 109 |
void |
| 110 |
remove (char const* key) |
| 111 |
{ |
| 112 |
remove (std::string (key)); |
| 113 |
} |
| 114 |
|
| 115 |
void |
| 116 |
remove (std::string const& key); |
| 117 |
|
| 118 |
std::type_info const& |
| 119 |
type_info (char const* key) const |
| 120 |
{ |
| 121 |
return type_info (std::string (key)); |
| 122 |
} |
| 123 |
|
| 124 |
std::type_info const& |
| 125 |
type_info (std::string const& key) const; |
| 126 |
|
| 127 |
private: |
| 128 |
typedef std::map<std::string, container::any> map; |
| 129 |
|
| 130 |
map map_; |
| 131 |
}; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
#include <cutl/compiler/context.txx> |
| 136 |
|
| 137 |
#endif // CUTL_COMPILER_CONTEXT_HXX |