unlogic
Loading...
Searching...
No Matches
Scope.h
Go to the documentation of this file.
1#ifndef UNLOGIC_PARENT_SCOPE_H
2#define UNLOGIC_PARENT_SCOPE_H
3
4#include <city/value/Value.h>
5#include <optional>
6#include <ranges>
7#include <unordered_map>
8#include <vector>
9
10namespace unlogic
11{
12 class Scope
13 {
14 std::vector<std::unordered_map<std::string, city::Value *> > layers;
15
16 public:
17 std::optional<city::Value *> Lookup(std::string const &key)
18 {
19 for (auto &layer: this->layers | std::ranges::views::reverse)
20 {
21 if (layer.contains(key))
22 {
23 return layer[key];
24 }
25 }
26
27 return std::nullopt;
28 }
29
30 void Set(std::string const &key, city::Value *value)
31 {
32 this->layers.back()[key] = value;
33 }
34
35 void PushLayer()
36 {
37 this->layers.emplace_back();
38 }
39
40 void PopLayer()
41 {
42 this->layers.pop_back();
43 }
44 };
45}; // namespace unlogic
46
47#endif // UNLOGIC_PARENT_SCOPE_H
void PushLayer()
Definition Scope.h:35
std::optional< city::Value * > Lookup(std::string const &key)
Definition Scope.h:17
void PopLayer()
Definition Scope.h:40
void Set(std::string const &key, city::Value *value)
Definition Scope.h:30