aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/misc
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic-simulation/misc')
-rw-r--r--src/openvic-simulation/misc/Define.cpp25
-rw-r--r--src/openvic-simulation/misc/Define.hpp7
2 files changed, 17 insertions, 15 deletions
diff --git a/src/openvic-simulation/misc/Define.cpp b/src/openvic-simulation/misc/Define.cpp
index c866b5d..cfd96a6 100644
--- a/src/openvic-simulation/misc/Define.cpp
+++ b/src/openvic-simulation/misc/Define.cpp
@@ -37,27 +37,30 @@ bool DefineManager::add_define(std::string_view name, std::string&& value, Defin
}
Date DefineManager::get_start_date() const {
- return *start_date;
+ return start_date ? *start_date : Date {};
}
Date DefineManager::get_end_date() const {
- return *end_date;
+ return end_date ? *end_date : Date {};
}
-bool DefineManager::add_date_define(std::string_view name, Date date) {
- if (name != "start_date" && name != "end_date") {
+bool DefineManager::in_game_period(Date date) const {
+ if (start_date && end_date) {
+ return date.in_range(*start_date, *end_date);
+ } else {
return false;
}
+}
- bool ret = defines.add_item({ name, date.to_string(), Define::Type::None });
-
+bool DefineManager::add_date_define(std::string_view name, Date date) {
if (name == "start_date") {
- start_date.reset(new Date(date));
+ start_date = date;
} else if (name == "end_date") {
- end_date.reset(new Date(date));
+ end_date = date;
+ } else {
+ return false;
}
-
- return ret;
+ return defines.add_item({ name, date.to_string(), Define::Type::None });
}
bool DefineManager::load_defines_file(ast::NodeCPtr root) {
@@ -103,8 +106,6 @@ bool DefineManager::load_defines_file(ast::NodeCPtr root) {
return ret;
})(value);
} else if (key == "start_date" || key == "end_date") {
- using namespace std::placeholders;
-
return expect_identifier_or_string(expect_date_str([this, &key](Date date) -> bool {
return add_date_define(key, date);
}))(value);
diff --git a/src/openvic-simulation/misc/Define.hpp b/src/openvic-simulation/misc/Define.hpp
index be71f9d..46e4836 100644
--- a/src/openvic-simulation/misc/Define.hpp
+++ b/src/openvic-simulation/misc/Define.hpp
@@ -1,7 +1,7 @@
#pragma once
#include <concepts>
-#include <memory>
+#include <optional>
#include "openvic-simulation/types/IdentifierRegistry.hpp"
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
@@ -32,8 +32,8 @@ namespace OpenVic {
private:
IdentifierRegistry<Define> defines;
- std::unique_ptr<Date> start_date = nullptr;
- std::unique_ptr<Date> end_date = nullptr;
+ std::optional<Date> start_date;
+ std::optional<Date> end_date;
public:
DefineManager();
@@ -44,6 +44,7 @@ namespace OpenVic {
Date get_start_date() const;
Date get_end_date() const;
+ bool in_game_period(Date date) const;
bool load_defines_file(ast::NodeCPtr root);
};