Skip to content

Commit ef2f979

Browse files
wvpmSpartan322
authored andcommitted
Make definition index types strongly typed
1 parent c9f268c commit ef2f979

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+298
-132
lines changed

src/openvic-simulation/country/CountryDefinition.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ bool CountryDefinitionManager::add_country(
6464
static constexpr colour_t default_colour = colour_t::fill_as(colour_t::max_value);
6565

6666
return country_definitions.emplace_item(
67-
identifier,
68-
identifier, colour, get_country_definition_count(), *graphical_culture, std::move(parties), std::move(unit_names),
69-
dynamic_tag, std::move(alternative_colours),
67+
identifier, //
68+
identifier, colour, CountryDefinition::index_t { get_country_definition_count() }, *graphical_culture,
69+
std::move(parties), std::move(unit_names), dynamic_tag, std::move(alternative_colours),
7070
/* Default to country colour for the chest and grey for the others. Update later if necessary. */
7171
colour, default_colour, default_colour
7272
);

src/openvic-simulation/country/CountryDefinition.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "openvic-simulation/types/HasIndex.hpp"
1010
#include "openvic-simulation/types/IdentifierRegistry.hpp"
1111
#include "openvic-simulation/types/OrderedContainers.hpp"
12+
#include "openvic-simulation/types/TypedIndices.hpp"
1213

1314
namespace OpenVic {
1415
struct CountryDefinitionManager;
@@ -20,7 +21,7 @@ namespace OpenVic {
2021
struct UnitType;
2122

2223
/* Generic information about a TAG */
23-
struct CountryDefinition : HasIdentifierAndColour, HasIndex<CountryDefinition> {
24+
struct CountryDefinition : HasIdentifierAndColour, HasIndex<CountryDefinition, country_index_t> {
2425
friend struct CountryDefinitionManager;
2526

2627
using unit_names_map_t = ordered_map<UnitType const*, name_list_t>;

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <cstddef>
77
#include <cstdint>
88

9+
#include <type_safe/strong_typedef.hpp>
10+
911
#include "openvic-simulation/country/SharedCountryValues.hpp"
1012
#include "openvic-simulation/country/CountryDefinition.hpp"
1113
#include "openvic-simulation/defines/CountryDefines.hpp"
@@ -2065,7 +2067,7 @@ void CountryInstance::manage_national_stockpile(
20652067
fixed_point_t weights_sum = 0;
20662068

20672069
for (auto [good_instance, good_data] : goods_data) {
2068-
const size_t index = good_instance.index;
2070+
const size_t index = type_safe::get(good_instance.index);
20692071
if (good_data.is_automated || !good_data.is_selling) {
20702072
const fixed_point_t quantity_to_allocate_for = good_data.is_automated
20712073
? good_data.government_needs - good_data.stockpile_amount

src/openvic-simulation/country/CountryInstance.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "openvic-simulation/types/IndexedFlatMap.hpp"
1919
#include "openvic-simulation/types/OrderedContainers.hpp"
2020
#include "openvic-simulation/types/TechnologyUnlockLevel.hpp"
21+
#include "openvic-simulation/types/TypedIndices.hpp"
2122
#include "openvic-simulation/types/UnitBranchType.hpp"
2223
#include "openvic-simulation/types/UnitVariant.hpp"
2324
#include "openvic-simulation/types/ValueHistory.hpp"
@@ -78,7 +79,7 @@ namespace OpenVic {
7879

7980
/* Representation of a country's mutable attributes, with a CountryDefinition that is unique at any single time
8081
* but can be swapped with other CountryInstance's CountryDefinition when switching tags. */
81-
struct CountryInstance : FlagStrings, HasIndex<CountryInstance>, PopsAggregate {
82+
struct CountryInstance : FlagStrings, HasIndex<CountryInstance, country_index_t>, PopsAggregate {
8283
friend struct CountryInstanceManager;
8384

8485
/*

src/openvic-simulation/dataloader/Dataloader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ bool Dataloader::_load_map_dir(DefinitionManager& definition_manager) const {
784784

785785
bool ret = expect_dictionary_keys(
786786
"max_provinces", ONE_EXACTLY,
787-
expect_uint<ProvinceDefinition::index_t>(std::bind_front(&MapDefinition::set_max_provinces, &map_definition)),
787+
expect_index<ProvinceDefinition::index_t>(std::bind_front(&MapDefinition::set_max_provinces, &map_definition)),
788788
"sea_starts", ONE_EXACTLY,
789789
expect_list_reserve_length(
790790
water_province_identifiers, expect_identifier(vector_callback(water_province_identifiers))

src/openvic-simulation/dataloader/NodeTools.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <concepts>
34
#include <cstdint>
45
#include <functional>
56
#include <optional>
@@ -13,6 +14,8 @@
1314

1415
#include <spdlog/common.h>
1516

17+
#include <type_safe/strong_typedef.hpp>
18+
1619
#include "openvic-simulation/types/Colour.hpp"
1720
#include "openvic-simulation/types/Date.hpp"
1821
#include "openvic-simulation/types/IndexedFlatMap.hpp"
@@ -195,6 +198,28 @@ using namespace std::string_view_literals;
195198
return expect_uint(callback, base);
196199
}
197200

201+
template<derived_from_specialization_of<type_safe::strong_typedef> T>
202+
requires std::unsigned_integral<type_safe::underlying_type<T>>
203+
NodeCallback auto expect_index(callback_t<T>& callback, int base = 10) {
204+
using underlying_type = type_safe::underlying_type<T>;
205+
206+
return expect_uint64([callback](uint64_t val) mutable -> bool {
207+
if (val <= static_cast<uint64_t>(std::numeric_limits<underlying_type>::max())) {
208+
return callback(T(val));
209+
}
210+
spdlog::error_s(
211+
"Invalid uint: {} (valid range: [0, {}])",
212+
val, static_cast<uint64_t>(std::numeric_limits<underlying_type>::max())
213+
);
214+
return false;
215+
}, base);
216+
}
217+
template<derived_from_specialization_of<type_safe::strong_typedef> T>
218+
requires std::unsigned_integral<type_safe::underlying_type<T>>
219+
NodeCallback auto expect_index(callback_t<T>&& callback, int base = 10) {
220+
return expect_index(callback, base);
221+
}
222+
198223
callback_t<std::string_view> expect_fixed_point_str(callback_t<fixed_point_t> callback);
199224
node_callback_t expect_fixed_point(callback_t<fixed_point_t> callback);
200225
/* Expect a list of 3 base 10 values, each either in the range [0, 1] or (1, 255], representing RGB components. */

src/openvic-simulation/economy/BuildingType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ BuildingType::BuildingType(
1212
index_t new_index,
1313
std::string_view identifier,
1414
building_type_args_t& building_type_args
15-
) : HasIndex<BuildingType> { new_index },
15+
) : HasIndex { new_index },
1616
Modifier { identifier, std::move(building_type_args.modifier), modifier_type_t::BUILDING },
1717
type { building_type_args.type },
1818
on_completion { building_type_args.on_completion },
@@ -52,7 +52,7 @@ bool BuildingTypeManager::add_building_type(
5252

5353
const bool ret = building_types.emplace_item(
5454
identifier,
55-
get_building_type_count(), identifier, building_type_args
55+
BuildingType::index_t { get_building_type_count() }, identifier, building_type_args
5656
);
5757

5858
if (ret) {

src/openvic-simulation/economy/BuildingType.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "openvic-simulation/types/HasIndex.hpp"
77
#include "openvic-simulation/types/IdentifierRegistry.hpp"
88
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
9+
#include "openvic-simulation/types/TypedIndices.hpp"
910
#include "openvic-simulation/utility/Containers.hpp"
1011

1112
namespace OpenVic {
@@ -19,7 +20,7 @@ namespace OpenVic {
1920
* MAP-12, MAP-75, MAP-76
2021
* MAP-13, MAP-78, MAP-79
2122
*/
22-
struct BuildingType : HasIndex<BuildingType>, Modifier {
23+
struct BuildingType : HasIndex<BuildingType, building_type_index_t>, Modifier {
2324
using naval_capacity_t = uint64_t;
2425

2526
struct building_type_args_t {

src/openvic-simulation/economy/GoodDefinition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ bool GoodDefinitionManager::add_good_definition(
6363

6464
if (good_definitions.emplace_item(
6565
identifier,
66-
identifier, colour, get_good_definition_count(), category, base_price, is_available_from_start,
66+
identifier, colour, GoodDefinition::index_t { get_good_definition_count() }, category, base_price, is_available_from_start,
6767
is_tradeable, is_money, has_overseas_penalty
6868
)) {
6969
category.good_definitions.push_back(&get_back_good_definition());

src/openvic-simulation/economy/GoodDefinition.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "openvic-simulation/types/HasIdentifier.hpp"
44
#include "openvic-simulation/types/HasIndex.hpp"
55
#include "openvic-simulation/types/IdentifierRegistry.hpp"
6+
#include "openvic-simulation/types/TypedIndices.hpp"
67
#include "openvic-simulation/utility/Containers.hpp"
78

89
namespace OpenVic {
@@ -31,7 +32,7 @@ namespace OpenVic {
3132
* ECON-238, ECON-239, ECON-240, ECON-241, ECON-242, ECON-243, ECON-244, ECON-245, ECON-246, ECON-247, ECON-248, ECON-249,
3233
* ECON-250, ECON-251, ECON-252, ECON-253, ECON-254, ECON-255, ECON-256, ECON-257, ECON-258, ECON-259, ECON-260, ECON-261
3334
*/
34-
struct GoodDefinition : HasIdentifierAndColour, HasIndex<GoodDefinition> {
35+
struct GoodDefinition : HasIdentifierAndColour, HasIndex<GoodDefinition, good_index_t> {
3536

3637
public:
3738
GoodCategory const& category;

0 commit comments

Comments
 (0)