Skip to content

Commit 2fcc7cc

Browse files
authored
Merge pull request #129 from OpenVicProject/registry-template-fun
Reworked `UniqueKeyRegistry` template params
2 parents 046e561 + 7094857 commit 2fcc7cc

File tree

5 files changed

+243
-149
lines changed

5 files changed

+243
-149
lines changed

src/openvic-simulation/dataloader/Dataloader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,10 @@ bool Dataloader::load_defines(GameManager& game_manager) {
836836
ret = false;
837837
}
838838
if (!_load_technologies(game_manager)) {
839+
Logger::error("Failed to load technologies!");
839840
ret = false;
840841
}
842+
game_manager.get_modifier_manager().lock_modifier_effects();
841843
if (!game_manager.get_politics_manager().get_rule_manager().setup_rules(
842844
game_manager.get_economy_manager().get_building_type_manager()
843845
)) {

src/openvic-simulation/interface/LoadBase.hpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@
55

66
namespace OpenVic {
77

8-
template<typename... _Context>
8+
template<typename... Context>
99
class LoadBase {
1010
protected:
1111
LoadBase() = default;
1212

13-
virtual bool _fill_key_map(NodeTools::key_map_t&, _Context...) = 0;
13+
virtual bool _fill_key_map(NodeTools::key_map_t&, Context...) = 0;
1414

1515
public:
1616
LoadBase(LoadBase&&) = default;
1717
virtual ~LoadBase() = default;
1818

19-
bool load(ast::NodeCPtr node, _Context... context) {
19+
bool load(ast::NodeCPtr node, Context... context) {
2020
NodeTools::key_map_t key_map;
2121
bool ret = _fill_key_map(key_map, context...);
2222
ret &= NodeTools::expect_dictionary_key_map(std::move(key_map))(node);
2323
return ret;
2424
}
2525

26-
template<std::derived_from<LoadBase<_Context...>> T, std::derived_from<T> U>
26+
template<std::derived_from<LoadBase<Context...>> T, std::derived_from<T> U>
2727
static NodeTools::node_callback_t _expect_instance(
28-
NodeTools::callback_t<std::unique_ptr<T>&&> callback, _Context... context
28+
NodeTools::callback_t<std::unique_ptr<T>&&> callback, Context... context
2929
) {
3030
return [callback, &context...](ast::NodeCPtr node) -> bool {
3131
std::unique_ptr<T> instance { std::make_unique<U>() };
@@ -38,14 +38,14 @@ namespace OpenVic {
3838
OV_DETAIL_GET_TYPE_BASE_CLASS(LoadBase)
3939
};
4040

41-
template<typename... _Context>
42-
class Named : public LoadBase<_Context...> {
41+
template<typename... Context>
42+
class Named : public LoadBase<Context...> {
4343
std::string PROPERTY(name);
4444

4545
protected:
4646
Named() = default;
4747

48-
virtual bool _fill_key_map(NodeTools::key_map_t& key_map, _Context...) override {
48+
virtual bool _fill_key_map(NodeTools::key_map_t& key_map, Context...) override {
4949
using namespace OpenVic::NodeTools;
5050
return add_key_map_entries(key_map, "name", ONE_EXACTLY, expect_string(assign_variable_callback_string(name)));
5151
}
@@ -64,19 +64,21 @@ namespace OpenVic {
6464
OV_DETAIL_GET_TYPE
6565
};
6666

67-
template<typename T, typename... _Context>
68-
requires std::derived_from<T, Named<_Context...>>
69-
struct _get_name {
70-
constexpr std::string_view operator()(T const* item) const {
71-
return item->get_name();
67+
template<typename Value, typename... Context>
68+
requires std::derived_from<Value, Named<Context...>>
69+
struct RegistryValueInfoNamed {
70+
using value_type = Value;
71+
72+
static constexpr std::string_view get_identifier(value_type const& item) {
73+
return item.get_name();
7274
}
7375
};
7476

75-
template<typename T, typename... _Context>
76-
requires std::derived_from<T, Named<_Context...>>
77-
using NamedRegistry = ValueRegistry<T, _get_name<T, _Context...>>;
77+
template<typename Value, typename... Context>
78+
requires std::derived_from<Value, Named<Context...>>
79+
using NamedRegistry = ValueRegistry<RegistryValueInfoNamed<Value, Context...>>;
7880

79-
template<typename T, typename... _Context>
80-
requires std::derived_from<T, Named<_Context...>>
81-
using NamedInstanceRegistry = InstanceRegistry<T, _get_name<T, _Context...>>;
81+
template<typename Value, typename... Context>
82+
requires std::derived_from<Value, Named<Context...>>
83+
using NamedInstanceRegistry = InstanceRegistry<RegistryValueInfoNamed<Value, Context...>>;
8284
}

src/openvic-simulation/misc/Modifier.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ bool ModifierManager::add_modifier_effect(std::string_view identifier, bool posi
9898
Logger::error("Invalid modifier effect identifier - empty!");
9999
return false;
100100
}
101-
return modifier_effects.add_item(
102-
std::make_unique<ModifierEffect>(std::move(identifier), std::move(positive_good), std::move(format))
103-
);
101+
return modifier_effects.add_item({ std::move(identifier), positive_good, format });
104102
}
105103

106104
bool ModifierManager::setup_modifier_effects() {

src/openvic-simulation/misc/Modifier.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@
33
#include "openvic-simulation/scripts/ConditionScript.hpp"
44
#include "openvic-simulation/types/IdentifierRegistry.hpp"
55

6-
#include "dataloader/NodeTools.hpp"
7-
8-
96
namespace OpenVic {
107
struct ModifierManager;
118

129
struct ModifierEffect : HasIdentifier {
10+
friend struct ModifierManager;
11+
1312
enum class format_t {
1413
PROPORTION_DECIMAL, /* An unscaled fraction/ratio, with 1 being "full"/"whole" */
1514
PERCENTAGE_DECIMAL, /* A fraction/ratio scaled so that 100 is "full"/"whole" */
1615
RAW_DECIMAL, /* A continuous quantity, e.g. attack strength */
1716
INT /* A discrete quantity, e.g. building count limit */
1817
};
1918

20-
friend std::unique_ptr<ModifierEffect> std::make_unique<ModifierEffect>(std::string_view&&, bool&&, format_t&&);
21-
2219
private:
2320
/* If true, positive values will be green and negative values will be red.
2421
* If false, the colours will be switced.
@@ -115,10 +112,10 @@ namespace OpenVic {
115112
struct ModifierManager {
116113
/* Some ModifierEffects are generated mid-load, such as max/min count modifiers for each building, so
117114
* we can't lock it until loading is over. This means we can't rely on locking for pointer stability,
118-
* so instead we use an IdentifierInstanceRegistry (using std::unique_ptr's under the hood).
115+
* so instead we store the effects in a deque which doesn't invalidate pointers on insert.
119116
*/
120117
private:
121-
CaseInsensitiveIdentifierInstanceRegistry<ModifierEffect> IDENTIFIER_REGISTRY(modifier_effect);
118+
CaseInsensitiveIdentifierRegistry<ModifierEffect, RegistryStorageInfoDeque> IDENTIFIER_REGISTRY(modifier_effect);
122119
case_insensitive_string_set_t complex_modifiers;
123120

124121
IdentifierRegistry<Modifier> IDENTIFIER_REGISTRY(event_modifier);

0 commit comments

Comments
 (0)