From a509a61bfbecefb09765af9e40fef9ea8c064537 Mon Sep 17 00:00:00 2001 From: Stefano Sanfilippo Date: Thu, 5 Mar 2015 22:30:08 +0100 Subject: [PATCH] Adding Scope lookup utility class. --- Scope.hpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Scope.hpp diff --git a/Scope.hpp b/Scope.hpp new file mode 100644 index 0000000..a5a657f --- /dev/null +++ b/Scope.hpp @@ -0,0 +1,47 @@ +#ifndef SCOPE_HPP +#define SCOPE_HPP + +#include +#include + +#include +#include + + +namespace monicelli { + +template +class Scope { +public: + boost::optional lookup(Key name) { + for (auto const& table: boost::adaptors::reverse(tables)) { + auto result = table.find(name); + if (result != table.end()) { + return result->second; + } + } + + return boost::none; + } + + void push(Key const& key, Value const& value) { + tables.back().insert({key, value}); + } + + void enter() { + tables.emplace_back(); + } + + void leave() { + if (!tables.empty()) { + tables.pop_back(); + } + } + +private: + std::vector> tables; +}; + +} + +#endif