blob: a193e241ca88071ddf07d2989c02d12fd0e62ab1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
/**
** \file misc/scoped-map.hh
** \brief Declaration of misc::scoped_map.
**
** This implements a stack of dictionnaries. Each time a scope is
** opened, a new dictionnary is added on the top of the stack; the
** dictionary is removed when the scope is closed. Lookup of keys
** is done in the last added dictionnary first (LIFO).
**
** In particular this class is used to implement symbol tables.
**/
#pragma once
#include <map>
#include <ostream>
#include <stack>
#include <vector>
namespace misc
{
template <typename Key, typename Data> class scoped_map
{
// FIXME DONE: Some code was deleted here.
public:
// initializes a new stack with an empty map on top as the first scope
scoped_map()
{
scopes_.push_back(std::map<Key,Data>());
}
void put(const Key& key, const Data& value);
Data get(const Key& key) const requires(!std::is_pointer_v<Data>);
Data get(const Key& key) const requires(std::is_pointer_v<Data>);
std::ostream& dump(std::ostream& ostr) const;
void scope_begin();
void scope_end();
private:
std::vector<std::map<Key, Data>> scopes_;
};
template <typename Key, typename Data>
std::ostream& operator<<(std::ostream& ostr,
const scoped_map<Key, Data>& tbl);
// FIXME DONE: Some code was deleted here.
// heehee
} // namespace misc
#include <misc/scoped-map.hxx>
|