aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/military
diff options
context:
space:
mode:
author Spartan322 <Megacake1234@gmail.com>2024-07-06 16:40:57 +0200
committer Spartan322 <Megacake1234@gmail.com>2024-07-18 04:20:15 +0200
commitd236dcf30c001e540377184565f4d173ed56f76e (patch)
tree3f2a270b5b85f91a22289091084a30c105f10f4d /src/openvic-simulation/military
parent93d095ba30747c7158882668577c854be7ea39a1 (diff)
Optimize some string interning casesoptimize/string-interning
Diffstat (limited to 'src/openvic-simulation/military')
-rw-r--r--src/openvic-simulation/military/UnitType.cpp14
-rw-r--r--src/openvic-simulation/military/UnitType.hpp6
-rw-r--r--src/openvic-simulation/military/Wargoal.cpp21
-rw-r--r--src/openvic-simulation/military/Wargoal.hpp4
4 files changed, 32 insertions, 13 deletions
diff --git a/src/openvic-simulation/military/UnitType.cpp b/src/openvic-simulation/military/UnitType.cpp
index 7a582c8..fb65bf8 100644
--- a/src/openvic-simulation/military/UnitType.cpp
+++ b/src/openvic-simulation/military/UnitType.cpp
@@ -147,15 +147,21 @@ bool UnitTypeManager::add_ship_type(
bool UnitTypeManager::load_unit_type_file(
GoodDefinitionManager const& good_definition_manager, TerrainTypeManager const& terrain_type_manager,
- ModifierManager const& modifier_manager, ast::NodeCPtr root
+ ModifierManager const& modifier_manager, ovdl::v2script::Parser const& parser
) {
- return expect_dictionary([this, &good_definition_manager, &terrain_type_manager, &modifier_manager](
+ using namespace std::string_view_literals;
+ auto type_symbol = parser.find_intern("type"sv);
+ if(!type_symbol) {
+ Logger::error("type could not be interned.");
+ }
+
+ return expect_dictionary([this, &good_definition_manager, &terrain_type_manager, &modifier_manager, &type_symbol](
std::string_view key, ast::NodeCPtr value
) -> bool {
UnitType::branch_t branch = INVALID_BRANCH;
- bool ret = expect_key("type", expect_branch_identifier(assign_variable_callback(branch)))(value);
+ bool ret = expect_key(type_symbol, expect_branch_identifier(assign_variable_callback(branch)))(value);
/* We shouldn't just check ret as it can be false even if branch was successfully parsed,
* but more than one instance of the key was found. */
@@ -282,7 +288,7 @@ bool UnitTypeManager::load_unit_type_file(
Logger::error("Unknown branch for unit ", key, ": ", static_cast<int>(branch));
return false;
}
- })(root);
+ })(parser.get_file_node());
}
bool UnitTypeManager::generate_modifiers(ModifierManager& modifier_manager) const {
diff --git a/src/openvic-simulation/military/UnitType.hpp b/src/openvic-simulation/military/UnitType.hpp
index 095a759..1642f22 100644
--- a/src/openvic-simulation/military/UnitType.hpp
+++ b/src/openvic-simulation/military/UnitType.hpp
@@ -3,9 +3,11 @@
#include <cstdint>
#include <string_view>
-#include "openvic-simulation/misc/Modifier.hpp"
+#include <openvic-dataloader/v2script/Parser.hpp>
+
#include "openvic-simulation/dataloader/NodeTools.hpp"
#include "openvic-simulation/economy/GoodDefinition.hpp"
+#include "openvic-simulation/misc/Modifier.hpp"
#include "openvic-simulation/types/Date.hpp"
#include "openvic-simulation/types/IdentifierRegistry.hpp"
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
@@ -191,7 +193,7 @@ namespace OpenVic {
bool load_unit_type_file(
GoodDefinitionManager const& good_definition_manager, TerrainTypeManager const& terrain_type_manager,
- ModifierManager const& modifier_manager, ast::NodeCPtr root
+ ModifierManager const& modifier_manager, ovdl::v2script::Parser const& parser
);
bool generate_modifiers(ModifierManager& modifier_manager) const;
};
diff --git a/src/openvic-simulation/military/Wargoal.cpp b/src/openvic-simulation/military/Wargoal.cpp
index afb1a24..773e791 100644
--- a/src/openvic-simulation/military/Wargoal.cpp
+++ b/src/openvic-simulation/military/Wargoal.cpp
@@ -1,5 +1,7 @@
#include "Wargoal.hpp"
+#include <openvic-dataloader/v2script/Parser.hpp>
+
#include "openvic-simulation/dataloader/NodeTools.hpp"
using namespace OpenVic;
@@ -68,11 +70,18 @@ bool WargoalTypeManager::add_wargoal_type(
});
}
-bool WargoalTypeManager::load_wargoal_file(ast::NodeCPtr root) {
+bool WargoalTypeManager::load_wargoal_file(ovdl::v2script::Parser const& parser) {
+ using namespace std::string_view_literals;
+ ovdl::symbol<char> peace_order_symbol = parser.find_intern("peace_order"sv);
+ if (!peace_order_symbol) {
+ Logger::error("peace_order could not be interned.");
+ }
+
bool ret = expect_dictionary_reserve_length(
wargoal_types,
- [this](std::string_view identifier, ast::NodeCPtr value) -> bool {
- if (identifier == "peace_order") {
+ [this, &peace_order_symbol](std::string_view identifier, ast::NodeCPtr value) -> bool {
+ // If peace_order_symbol is false, we know there is no peace_order string in the parser
+ if (peace_order_symbol && identifier == peace_order_symbol.c_str()) {
return true;
}
@@ -187,11 +196,11 @@ bool WargoalTypeManager::load_wargoal_file(ast::NodeCPtr root) {
);
return ret;
}
- )(root);
+ )(parser.get_file_node());
/* load order in which CBs are prioritised by AI */
ret &= expect_key(
- "peace_order",
+ peace_order_symbol,
expect_list(
expect_wargoal_type_identifier(
[this](WargoalType const& wargoal) -> bool {
@@ -205,7 +214,7 @@ bool WargoalTypeManager::load_wargoal_file(ast::NodeCPtr root) {
true // warn instead of error
)
)
- )(root);
+ )(parser.get_file_node());
lock_wargoal_types();
return ret;
diff --git a/src/openvic-simulation/military/Wargoal.hpp b/src/openvic-simulation/military/Wargoal.hpp
index ecd8056..8387821 100644
--- a/src/openvic-simulation/military/Wargoal.hpp
+++ b/src/openvic-simulation/military/Wargoal.hpp
@@ -1,5 +1,7 @@
#pragma once
+#include <openvic-dataloader/v2script/Parser.hpp>
+
#include "openvic-simulation/scripts/ConditionScript.hpp"
#include "openvic-simulation/scripts/EffectScript.hpp"
#include "openvic-simulation/types/EnumBitfield.hpp"
@@ -111,7 +113,7 @@ namespace OpenVic {
ConditionScript&& allowed_countries, EffectScript&& on_add, EffectScript&& on_po_accepted
);
- bool load_wargoal_file(ast::NodeCPtr root);
+ bool load_wargoal_file(ovdl::v2script::Parser const& parser);
bool parse_scripts(DefinitionManager const& definition_manager);
};