aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/types
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic-simulation/types')
-rw-r--r--src/openvic-simulation/types/Date.cpp15
-rw-r--r--src/openvic-simulation/types/Date.hpp5
-rw-r--r--src/openvic-simulation/types/IdentifierRegistry.hpp86
3 files changed, 76 insertions, 30 deletions
diff --git a/src/openvic-simulation/types/Date.cpp b/src/openvic-simulation/types/Date.cpp
index 1b15e45..7356768 100644
--- a/src/openvic-simulation/types/Date.cpp
+++ b/src/openvic-simulation/types/Date.cpp
@@ -100,6 +100,12 @@ std::ostream& OpenVic::operator<<(std::ostream& out, Timespan const& timespan) {
return out << timespan.to_string();
}
+const std::string Date::MONTH_NAMES[MONTHS_IN_YEAR] = {
+ "January", "February", "March", "April", "May", "June",
+ "July", "August", "September", "October", "November",
+ "December"
+};
+
Timespan Date::_date_to_timespan(year_t year, month_t month, day_t day) {
month = std::clamp<month_t>(month, 1, MONTHS_IN_YEAR);
day = std::clamp<day_t>(day, 1, DAYS_IN_MONTH[month - 1]);
@@ -208,6 +214,15 @@ bool Date::in_range(Date start, Date end) const {
return start <= *this && *this <= end;
}
+std::string const& Date::get_month_name() const {
+ const month_t month = get_month();
+ if (1 <= month && month <= MONTHS_IN_YEAR) {
+ return MONTH_NAMES[month - 1];
+ }
+ static const std::string invalid_month_name = "Invalid Month";
+ return invalid_month_name;
+}
+
std::string Date::to_string() const {
std::stringstream ss;
ss << *this;
diff --git a/src/openvic-simulation/types/Date.hpp b/src/openvic-simulation/types/Date.hpp
index cce1308..a78b4e6 100644
--- a/src/openvic-simulation/types/Date.hpp
+++ b/src/openvic-simulation/types/Date.hpp
@@ -58,6 +58,7 @@ namespace OpenVic {
static month_t const* MONTH_FROM_DAY_IN_YEAR;
static constexpr char SEPARATOR_CHARACTER = '.';
+ static const std::string MONTH_NAMES[MONTHS_IN_YEAR];
private:
// Number of days since Jan 1st, Year 0
@@ -93,6 +94,10 @@ namespace OpenVic {
bool in_range(Date start, Date end) const;
+ /* This returns a std::string const&, rather than a std::string_view, as it needs to be converted to a
+ * godot::StringName in order to be localised, and std::string_view to a godot::StringName conversion requires an
+ * intermediary std::string. */
+ std::string const& get_month_name() const;
std::string to_string() const;
explicit operator std::string() const;
// Parsed from string of the form YYYY.MM.DD
diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp
index 072ab07..cfb4fed 100644
--- a/src/openvic-simulation/types/IdentifierRegistry.hpp
+++ b/src/openvic-simulation/types/IdentifierRegistry.hpp
@@ -197,7 +197,7 @@ namespace OpenVic {
return nullptr; \
} \
value_type CONST* get_item_by_index(size_t index) CONST { \
- return index < items.size() ? &items[index] : nullptr; \
+ return index < items.size() ? GetPointer(items[index]) : nullptr; \
} \
NodeTools::callback_t<std::string_view> expect_item_str( \
NodeTools::callback_t<value_type CONST&> callback, bool warn \
@@ -315,72 +315,98 @@ namespace OpenVic {
template<std::derived_from<HasIdentifier> _Type>
using IdentifierInstanceRegistry = InstanceRegistry<_Type, _get_identifier<_Type>>;
+/* Member functions for const access to a UniqueKeyRegistry member variable. */
+#define IDENTIFIER_REGISTRY_ACCESSORS(name) \
+ IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(name, name##s)
+
#define IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(singular, plural) \
+ IDENTIFIER_REGISTRY_ACCESSORS_FULL_CUSTOM(singular, plural, plural, 0)
+
+#define IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_INDEX_OFFSET(name, index_offset) \
+ IDENTIFIER_REGISTRY_ACCESSORS_FULL_CUSTOM(name, name##s, name##s, index_offset)
+
+#define IDENTIFIER_REGISTRY_ACCESSORS_FULL_CUSTOM(singular, plural, registry, index_offset) \
void lock_##plural() { \
- plural.lock(); \
+ registry.lock(); \
} \
bool plural##_are_locked() const { \
- return plural.is_locked(); \
+ return registry.is_locked(); \
} \
- decltype(plural)::value_type const* get_##singular##_by_identifier(std::string_view identifier) const { \
- return plural.get_item_by_identifier(identifier); \
+ decltype(registry)::value_type const* get_##singular##_by_identifier(std::string_view identifier) const { \
+ return registry.get_item_by_identifier(identifier); \
} \
bool has_##singular##_identifier(std::string_view identifier) const { \
- return plural.has_identifier(identifier); \
+ return registry.has_identifier(identifier); \
+ } \
+ decltype(registry)::value_type const* get_##singular##_by_index(size_t index) const { \
+ return index >= index_offset ? registry.get_item_by_index(index - index_offset) : nullptr; \
} \
size_t get_##singular##_count() const { \
- return plural.size(); \
+ return registry.size(); \
} \
bool plural##_empty() const { \
- return plural.empty(); \
+ return registry.empty(); \
} \
- std::vector<decltype(plural)::storage_type> const& get_##plural() const { \
- return plural.get_items(); \
+ std::vector<decltype(registry)::storage_type> const& get_##plural() const { \
+ return registry.get_items(); \
} \
std::vector<std::string_view> get_##singular##_identifiers() const { \
- return plural.get_item_identifiers(); \
+ return registry.get_item_identifiers(); \
} \
NodeTools::callback_t<std::string_view> expect_##singular##_str( \
- NodeTools::callback_t<decltype(plural)::value_type const&> callback, bool warn = false \
+ NodeTools::callback_t<decltype(registry)::value_type const&> callback, bool warn = false \
) const { \
- return plural.expect_item_str(callback, warn); \
+ return registry.expect_item_str(callback, warn); \
} \
NodeTools::node_callback_t expect_##singular##_identifier( \
- NodeTools::callback_t<decltype(plural)::value_type const&> callback, bool warn = false \
+ NodeTools::callback_t<decltype(registry)::value_type const&> callback, bool warn = false \
) const { \
- return plural.expect_item_identifier(callback, warn); \
+ return registry.expect_item_identifier(callback, warn); \
} \
NodeTools::node_callback_t expect_##singular##_dictionary( \
- NodeTools::callback_t<decltype(plural)::value_type const&, ast::NodeCPtr> callback \
+ NodeTools::callback_t<decltype(registry)::value_type const&, ast::NodeCPtr> callback \
) const { \
- return plural.expect_item_dictionary(callback); \
+ return registry.expect_item_dictionary(callback); \
} \
NodeTools::node_callback_t expect_##singular##_decimal_map( \
- NodeTools::callback_t<fixed_point_map_t<decltype(plural)::value_type const*>&&> callback \
+ NodeTools::callback_t<fixed_point_map_t<decltype(registry)::value_type const*>&&> callback \
) const { \
- return plural.expect_item_decimal_map(callback); \
+ return registry.expect_item_decimal_map(callback); \
}
+/* Member functions for non-const access to a UniqueKeyRegistry member variable. */
+#define IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS(name) \
+ IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_PLURAL(name, name##s)
+
#define IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_PLURAL(singular, plural) \
- decltype(plural)::value_type* get_##singular##_by_identifier(std::string_view identifier) { \
- return plural.get_item_by_identifier(identifier); \
+ IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_FULL_CUSTOM(single, plural, plural, 0)
+
+#define IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_INDEX_OFFSET(name, index_offset) \
+ IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_FULL_CUSTOM(name, name##s, name##s, index_offset)
+
+#define IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_FULL_CUSTOM(singular, plural, registry, index_offset) \
+ decltype(registry)::value_type* get_##singular##_by_identifier(std::string_view identifier) { \
+ return registry.get_item_by_identifier(identifier); \
+ } \
+ decltype(registry)::value_type* get_##singular##_by_index(size_t index) { \
+ return index >= index_offset ? registry.get_item_by_index(index - index_offset) : nullptr; \
+ } \
+ std::vector<decltype(registry)::storage_type>& get_##plural() { \
+ return registry.get_items(); \
} \
NodeTools::callback_t<std::string_view> expect_##singular##_str( \
- NodeTools::callback_t<decltype(plural)::value_type&> callback, bool warn = false \
+ NodeTools::callback_t<decltype(registry)::value_type&> callback, bool warn = false \
) { \
- return plural.expect_item_str(callback, warn); \
+ return registry.expect_item_str(callback, warn); \
} \
NodeTools::node_callback_t expect_##singular##_identifier( \
- NodeTools::callback_t<decltype(plural)::value_type&> callback, bool warn = false \
+ NodeTools::callback_t<decltype(registry)::value_type&> callback, bool warn = false \
) { \
- return plural.expect_item_identifier(callback, warn); \
+ return registry.expect_item_identifier(callback, warn); \
} \
NodeTools::node_callback_t expect_##singular##_dictionary( \
- NodeTools::callback_t<decltype(plural)::value_type&, ast::NodeCPtr> callback \
+ NodeTools::callback_t<decltype(registry)::value_type&, ast::NodeCPtr> callback \
) { \
- return plural.expect_item_dictionary(callback); \
+ return registry.expect_item_dictionary(callback); \
}
-
-#define IDENTIFIER_REGISTRY_ACCESSORS(name) IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(name, name##s)
-#define IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS(name) IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_PLURAL(name, name##s)
}