-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathVariableCollection.cpp
More file actions
89 lines (77 loc) · 2.61 KB
/
Copy pathVariableCollection.cpp
File metadata and controls
89 lines (77 loc) · 2.61 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "VariableCollection.h"
#include <iostream>
#include "FetlangException.h"
bool VariableCollection::has(const std::string& variable_name) const {
return variables.find(variable_name) != variables.end();
}
Variable& VariableCollection::get(const std::string& variable_name) {
if (!has(variable_name)) {
throw FetlangException("No such variable with name " + variable_name);
}
return variables[variable_name];
}
Variable& VariableCollection::access(const std::string& variable_name) {
Variable& return_value = get(variable_name);
if (variable_access_list.empty() || variable_access_list.back() != &return_value) {
variable_access_list.push_back(&return_value);
}
return return_value;
}
Variable& VariableCollection::getLastAdded() {
if (variable_added_list.empty()) {
throw FetlangException(
"Can't retrieve last variable added because the variable list is empty");
}
return *(variable_added_list.back());
}
Variable& VariableCollection::getLastAccessed() {
if (variable_added_list.empty()) {
throw FetlangException(
"Can't retrieve last variable accessed because the access list is empty");
}
return *(variable_access_list.back());
}
void VariableCollection::add(const Variable& variable) {
if (has(variable.getName())) {
throw FetlangException("Variable with " + variable.getName() + " already exists");
}
variables[variable.getName()] = variable;
variable_added_list.push_back(&(variables[variable.getName()]));
}
Variable& VariableCollection::getLastOfGender(Gender gender, bool exclude_last) {
for (auto it = variable_access_list.rbegin() + (exclude_last ? 1 : 0);
it != variable_access_list.rend(); it++) {
if ((*it)->getGender() == gender) {
return **it;
}
if ((*it)->getGender() == UNASSIGNED_GENDER) {
(*it)->setGender(gender);
return **it;
}
}
throw FetlangException("No variable with the appropriate gender");
}
Variable& VariableCollection::getLastOfGenderExcludingLast(Gender gender) {
return getLastOfGender(gender, true);
}
bool VariableCollection::hasLastOfGender(Gender gender, bool exclude_last) {
for (auto it = variable_access_list.rbegin() + (exclude_last ? 1 : 0);
it != variable_access_list.rend(); it++) {
if ((*it)->getGender() == gender) {
return true;
}
if ((*it)->getGender() == UNASSIGNED_GENDER) {
return true;
}
}
return false;
}
bool VariableCollection::hasLastOfGenderExcludingLast(Gender gender) {
return hasLastOfGender(gender, true);
}
void VariableCollection::display() {
for (const auto& varpair : variables) {
Variable var = varpair.second;
std::cout << var.getName() << "(" << var.getCode() << "): " << var.getType() << "\n";
}
}