| 1 |
// file : cutl/compiler/context.txx |
| 2 |
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC |
| 3 |
// license : MIT; see accompanying LICENSE file |
| 4 |
|
| 5 |
namespace cutl |
| 6 |
{ |
| 7 |
namespace compiler |
| 8 |
{ |
| 9 |
template <typename X> |
| 10 |
X& context:: |
| 11 |
get (std::string const& key) |
| 12 |
{ |
| 13 |
map::iterator i (map_.find (key)); |
| 14 |
|
| 15 |
if (i == map_.end ()) |
| 16 |
throw no_entry (); |
| 17 |
|
| 18 |
try |
| 19 |
{ |
| 20 |
return i->second. template value<X> (); |
| 21 |
} |
| 22 |
catch (container::any::typing const&) |
| 23 |
{ |
| 24 |
throw typing (); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
template <typename X> |
| 29 |
X const& context:: |
| 30 |
get (std::string const& key) const |
| 31 |
{ |
| 32 |
map::const_iterator i (map_.find (key)); |
| 33 |
|
| 34 |
if (i == map_.end ()) |
| 35 |
throw no_entry (); |
| 36 |
|
| 37 |
try |
| 38 |
{ |
| 39 |
return i->second. template value<X> (); |
| 40 |
} |
| 41 |
catch (container::any::typing const&) |
| 42 |
{ |
| 43 |
throw typing (); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
template <typename X> |
| 48 |
X const& context:: |
| 49 |
get (std::string const& key, X const& default_value) const |
| 50 |
{ |
| 51 |
map::const_iterator i (map_.find (key)); |
| 52 |
|
| 53 |
if (i == map_.end ()) |
| 54 |
return default_value; |
| 55 |
|
| 56 |
try |
| 57 |
{ |
| 58 |
return i->second. template value<X> (); |
| 59 |
} |
| 60 |
catch (container::any::typing const&) |
| 61 |
{ |
| 62 |
throw typing (); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
template <typename X> |
| 67 |
X& context:: |
| 68 |
set (std::string const& key, X const& value) |
| 69 |
{ |
| 70 |
try |
| 71 |
{ |
| 72 |
std::pair<map::iterator, bool> r ( |
| 73 |
map_.insert (map::value_type (key, value))); |
| 74 |
|
| 75 |
X& x (r.first->second. template value<X> ()); |
| 76 |
|
| 77 |
if (!r.second) |
| 78 |
x = value; |
| 79 |
|
| 80 |
return x; |
| 81 |
} |
| 82 |
catch (container::any::typing const&) |
| 83 |
{ |
| 84 |
throw typing (); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
} |