Skip to content

Commit 47389bd

Browse files
authored
Merge pull request #353 from OpenVicProject/add/function2
Add Naios/function2@43fc0ca
2 parents d055c54 + ebe05d1 commit 47389bd

File tree

20 files changed

+95
-65
lines changed

20 files changed

+95
-65
lines changed

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@
1717
path = tests/deps/snitch
1818
url = https://github.com/snitch-org/snitch
1919
ignore = dirty
20+
[submodule "deps/function2"]
21+
path = deps/function2
22+
url = https://github.com/Naios/function2
23+
[submodule "deps/std_function/func"]
24+
path = deps/std_function/func
25+
url = https://github.com/skarupke/std_function

deps/SCsub

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ def build_colony(env):
3131
env.Append(CPPPATH=env.colony["INCPATH"])
3232
env.exposed_includes += env.colony["INCPATH"]
3333

34+
def build_function2(env):
35+
include_path = "function2/include"
36+
env.function2 = {}
37+
env.function2["INCPATH"] = [env.Dir(include_path)]
38+
env.Append(CPPPATH=env.function2["INCPATH"])
39+
env.exposed_includes += env.function2["INCPATH"]
40+
41+
def build_std_function(env):
42+
include_path = "std_function"
43+
env.std_function = {}
44+
env.std_function["INCPATH"] = [env.Dir(include_path)]
45+
env.Append(CPPPATH=env.std_function["INCPATH"])
46+
env.exposed_includes += env.std_function["INCPATH"]
47+
3448
def link_tbb(env):
3549
import sys
3650
if not env.get("is_msvc", False) and not env.get("use_mingw", False) and sys.platform != "darwin":
@@ -40,4 +54,6 @@ build_openvic_dataloader(env)
4054
build_lexy_vdf(env)
4155
build_ordered_map(env)
4256
build_colony(env)
57+
build_function2(env)
58+
build_std_function(env)
4359
link_tbb(env)

deps/function2

Submodule function2 added at 43fc0ca

deps/std_function/func

Submodule func added at dbf2df9

src/openvic-simulation/InstanceManager.hpp

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

3-
#include <functional>
4-
53
#include "openvic-simulation/console/ConsoleInstance.hpp"
64
#include "openvic-simulation/country/CountryInstance.hpp"
75
#include "openvic-simulation/diplomacy/CountryRelation.hpp"
@@ -17,6 +15,8 @@
1715
#include "openvic-simulation/types/FlagStrings.hpp"
1816
#include "openvic-simulation/utility/ThreadPool.hpp"
1917

18+
#include <function2/function2.hpp>
19+
2020
namespace OpenVic {
2121

2222
struct DefinitionManager;
@@ -25,7 +25,7 @@ namespace OpenVic {
2525
struct InstanceManager {
2626
friend GameActionManager;
2727

28-
using gamestate_updated_func_t = std::function<void()>;
28+
using gamestate_updated_func_t = fu2::function_base<true, true, fu2::capacity_can_hold<void*>, false, false, void()>;
2929

3030
private:
3131
ThreadPool thread_pool;

src/openvic-simulation/console/ConsoleInstance.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "openvic-simulation/types/OrderedContainers.hpp"
1515
#include "openvic-simulation/utility/Getters.hpp"
1616

17+
#include <function2/function2.hpp>
18+
1719
namespace OpenVic {
1820
struct ProvinceInstance;
1921
struct CountryInstance;
@@ -32,7 +34,7 @@ namespace OpenVic {
3234

3335
using execute_command_func_t = FunctionRef<bool(Argument&)>;
3436

35-
using write_func_t = std::function<void(OpenVic::colour_t, std::string&&)>;
37+
using write_func_t = fu2::function_view<void(OpenVic::colour_t, std::string&&)>;
3638

3739
ConsoleInstance(InstanceManager& instance_manager);
3840
ConsoleInstance(InstanceManager& instance_manager, write_func_t&& write_func);

src/openvic-simulation/dataloader/NodeTools.cpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ using namespace OpenVic::NodeTools;
2727

2828
template<typename T>
2929
static NodeCallback auto _expect_type(Callback<T const*> auto&& callback) {
30-
return [callback = FWD(callback)](ast::NodeCPtr node) -> bool {
30+
return [callback = FWD(callback)](ast::NodeCPtr node) mutable -> bool {
3131
if (node != nullptr) {
3232
T const* cast_node = dryad::node_try_cast<T>(node);
3333
if (cast_node != nullptr) {
@@ -45,7 +45,7 @@ using _NodeIterator = typename decltype(std::declval<ast::NodeCPtr>()->children(
4545
using _NodeStatementRange = dryad::node_range<_NodeIterator, ast::Statement>;
4646

4747
static NodeCallback auto _abstract_statement_node_callback(Callback<_NodeStatementRange> auto&& callback) {
48-
return [callback = FWD(callback)](ast::NodeCPtr node) -> bool {
48+
return [callback = FWD(callback)](ast::NodeCPtr node) mutable -> bool {
4949
if (node != nullptr) {
5050
if (auto const* file_tree = dryad::node_try_cast<ast::FileTree>(node)) {
5151
return callback(file_tree->statements());
@@ -68,7 +68,7 @@ static NodeCallback auto _abstract_statement_node_callback(Callback<_NodeStateme
6868

6969
template<std::derived_from<ast::FlatValue> T>
7070
static Callback<T const*> auto _abstract_symbol_node_callback(Callback<ovdl::symbol<char>> auto&& callback, bool allow_empty) {
71-
return [callback = FWD(callback), allow_empty](T const* node) -> bool {
71+
return [callback = FWD(callback), allow_empty](T const* node) mutable -> bool {
7272
if (allow_empty) {
7373
return callback(node->value());
7474
} else {
@@ -83,9 +83,9 @@ static Callback<T const*> auto _abstract_symbol_node_callback(Callback<ovdl::sym
8383
}
8484

8585
template<std::derived_from<ast::FlatValue> T>
86-
static Callback<T const*> auto _abstract_string_node_callback(Callback<std::string_view> auto callback, bool allow_empty) {
86+
static Callback<T const*> auto _abstract_string_node_callback(Callback<std::string_view> auto&& callback, bool allow_empty) {
8787
return _abstract_symbol_node_callback<T>(
88-
[callback](ovdl::symbol<char> symbol) -> bool {
88+
[callback = FWD(callback)](ovdl::symbol<char> symbol) mutable -> bool {
8989
return callback(symbol.view());
9090
},
9191
allow_empty
@@ -96,24 +96,24 @@ node_callback_t NodeTools::expect_identifier(callback_t<std::string_view> callba
9696
return _expect_type<ast::IdentifierValue>(_abstract_string_node_callback<ast::IdentifierValue>(callback, false));
9797
}
9898

99-
static NodeCallback auto _expect_identifier(Callback<ovdl::symbol<char>> auto callback) {
100-
return _expect_type<ast::IdentifierValue>(_abstract_symbol_node_callback<ast::IdentifierValue>(callback, false));
99+
static NodeCallback auto _expect_identifier(Callback<ovdl::symbol<char>> auto&& callback) {
100+
return _expect_type<ast::IdentifierValue>(_abstract_symbol_node_callback<ast::IdentifierValue>(FWD(callback), false));
101101
}
102102

103103
node_callback_t NodeTools::expect_string(callback_t<std::string_view> callback, bool allow_empty) {
104104
return _expect_type<ast::StringValue>(_abstract_string_node_callback<ast::StringValue>(callback, allow_empty));
105105
}
106106

107-
static NodeCallback auto _expect_string(Callback<ovdl::symbol<char>> auto callback, bool allow_empty) {
108-
return _expect_type<ast::StringValue>(_abstract_symbol_node_callback<ast::StringValue>(callback, allow_empty));
107+
static NodeCallback auto _expect_string(Callback<ovdl::symbol<char>> auto&& callback, bool allow_empty) {
108+
return _expect_type<ast::StringValue>(_abstract_symbol_node_callback<ast::StringValue>(FWD(callback), allow_empty));
109109
}
110110

111111
node_callback_t NodeTools::expect_identifier_or_string(callback_t<std::string_view> callback, bool allow_empty) {
112112
return [callback, allow_empty](ast::NodeCPtr node) -> bool {
113113
if (node != nullptr) {
114114
auto const* cast_node = dryad::node_try_cast<ast::FlatValue>(node);
115115
if (cast_node != nullptr) {
116-
return _abstract_string_node_callback<ast::FlatValue>(callback, allow_empty)(cast_node);
116+
return _abstract_string_node_callback<ast::FlatValue>(FWD(callback), allow_empty)(cast_node);
117117
}
118118
Logger::error(
119119
"Invalid node type ", ast::get_type_name(node->kind()), " when expecting ", utility::type_name<ast::IdentifierValue>(), " or ",
@@ -134,7 +134,7 @@ node_callback_t NodeTools::expect_bool(callback_t<bool> callback) {
134134
}
135135

136136
node_callback_t NodeTools::expect_int_bool(callback_t<bool> callback) {
137-
return expect_uint64([callback](uint64_t num) -> bool {
137+
return expect_uint64([callback](uint64_t num) mutable -> bool {
138138
if (num > 1) {
139139
Logger::warning("Found int bool with value >1: ", num);
140140
}
@@ -143,7 +143,7 @@ node_callback_t NodeTools::expect_int_bool(callback_t<bool> callback) {
143143
}
144144

145145
node_callback_t NodeTools::expect_int64(callback_t<int64_t> callback, int base) {
146-
return expect_identifier([callback, base](std::string_view identifier) -> bool {
146+
return expect_identifier([callback, base](std::string_view identifier) mutable -> bool {
147147
int64_t val;
148148
std::from_chars_result result = StringUtils::string_to_int64(identifier, val, base);
149149
if (result.ec == std::errc{}) {
@@ -155,7 +155,7 @@ node_callback_t NodeTools::expect_int64(callback_t<int64_t> callback, int base)
155155
}
156156

157157
node_callback_t NodeTools::expect_uint64(callback_t<uint64_t> callback, int base) {
158-
return expect_identifier([callback, base](std::string_view identifier) -> bool {
158+
return expect_identifier([callback, base](std::string_view identifier) mutable -> bool {
159159
uint64_t val;
160160
std::from_chars_result result = StringUtils::string_to_uint64(identifier, val, base);
161161
if (result.ec == std::errc{}) {
@@ -167,7 +167,7 @@ node_callback_t NodeTools::expect_uint64(callback_t<uint64_t> callback, int base
167167
}
168168

169169
callback_t<std::string_view> NodeTools::expect_fixed_point_str(callback_t<fixed_point_t> callback) {
170-
return [callback](std::string_view identifier) -> bool {
170+
return [callback](std::string_view identifier) mutable -> bool {
171171
bool successful = false;
172172
const fixed_point_t val = fixed_point_t::parse(identifier.data(), identifier.length(), &successful);
173173
if (successful) {
@@ -183,7 +183,7 @@ node_callback_t NodeTools::expect_fixed_point(callback_t<fixed_point_t> callback
183183
}
184184

185185
node_callback_t NodeTools::expect_colour(callback_t<colour_t> callback) {
186-
return [callback](ast::NodeCPtr node) -> bool {
186+
return [callback](ast::NodeCPtr node) mutable -> bool {
187187
colour_t col = colour_t::null();
188188
int32_t components = 0;
189189
bool ret = expect_list_of_length(3, expect_fixed_point(
@@ -208,13 +208,13 @@ node_callback_t NodeTools::expect_colour(callback_t<colour_t> callback) {
208208
}
209209

210210
node_callback_t NodeTools::expect_colour_hex(callback_t<colour_argb_t> callback) {
211-
return expect_uint<colour_argb_t::integer_type>([callback](colour_argb_t::integer_type val) -> bool {
211+
return expect_uint<colour_argb_t::integer_type>([callback](colour_argb_t::integer_type val) mutable -> bool {
212212
return callback(colour_argb_t::from_argb(val));
213213
}, 16);
214214
}
215215

216216
callback_t<std::string_view> NodeTools::expect_date_str(callback_t<Date> callback) {
217-
return [callback](std::string_view identifier) -> bool {
217+
return [callback](std::string_view identifier) mutable -> bool {
218218
Date::from_chars_result result;
219219
const Date date = Date::from_string_log(identifier, &result);
220220
if (result.ec == std::errc{}) {
@@ -238,26 +238,26 @@ node_callback_t NodeTools::expect_date_identifier_or_string(callback_t<Date> cal
238238
}
239239

240240
node_callback_t NodeTools::expect_years(callback_t<Timespan> callback) {
241-
return expect_int<Timespan::day_t>([callback](Timespan::day_t val) -> bool {
241+
return expect_int<Timespan::day_t>([callback](Timespan::day_t val) mutable -> bool {
242242
return callback(Timespan::from_years(val));
243243
});
244244
}
245245

246246
node_callback_t NodeTools::expect_months(callback_t<Timespan> callback) {
247-
return expect_int<Timespan::day_t>([callback](Timespan::day_t val) -> bool {
247+
return expect_int<Timespan::day_t>([callback](Timespan::day_t val) mutable -> bool {
248248
return callback(Timespan::from_months(val));
249249
});
250250
}
251251

252252
node_callback_t NodeTools::expect_days(callback_t<Timespan> callback) {
253-
return expect_int<Timespan::day_t>([callback](Timespan::day_t val) -> bool {
253+
return expect_int<Timespan::day_t>([callback](Timespan::day_t val) mutable -> bool {
254254
return callback(Timespan::from_days(val));
255255
});
256256
}
257257

258258
template<typename T, node_callback_t (*expect_func)(callback_t<T>)>
259259
NodeCallback auto _expect_vec2(Callback<vec2_t<T>> auto&& callback) {
260-
return [callback = FWD(callback)](ast::NodeCPtr node) -> bool {
260+
return [callback = FWD(callback)](ast::NodeCPtr node) mutable -> bool {
261261
vec2_t<T> vec;
262262
bool ret = expect_dictionary_keys(
263263
"x", ONE_EXACTLY, expect_func(assign_variable_callback(vec.x)),
@@ -297,7 +297,7 @@ node_callback_t NodeTools::expect_fvec2(callback_t<fvec2_t> callback) {
297297
// seen in some gfx files, these vectors don't have x,y,z,w labels, so are loaded similarly to colours.
298298

299299
node_callback_t NodeTools::expect_fvec3(callback_t<fvec3_t> callback) {
300-
return [callback](ast::NodeCPtr node) -> bool {
300+
return [callback](ast::NodeCPtr node) mutable -> bool {
301301
fvec3_t vec;
302302
int32_t components = 0;
303303
bool ret = expect_list_of_length(3, expect_fixed_point(
@@ -312,7 +312,7 @@ node_callback_t NodeTools::expect_fvec3(callback_t<fvec3_t> callback) {
312312
}
313313

314314
node_callback_t NodeTools::expect_fvec4(callback_t<fvec4_t> callback) {
315-
return [callback](ast::NodeCPtr node) -> bool {
315+
return [callback](ast::NodeCPtr node) mutable -> bool {
316316
fvec4_t vec;
317317
int32_t components = 0;
318318
bool ret = expect_list_of_length(4, expect_fixed_point(
@@ -327,7 +327,7 @@ node_callback_t NodeTools::expect_fvec4(callback_t<fvec4_t> callback) {
327327
}
328328

329329
node_callback_t NodeTools::expect_assign(key_value_callback_t callback) {
330-
return _expect_type<ast::AssignStatement>([callback](ast::AssignStatement const* assign_node) -> bool {
330+
return _expect_type<ast::AssignStatement>([callback](ast::AssignStatement const* assign_node) mutable -> bool {
331331
std::string_view left;
332332
bool ret = expect_identifier(assign_variable_callback(left))(assign_node->left());
333333
if (ret) {
@@ -343,7 +343,7 @@ node_callback_t NodeTools::expect_assign(key_value_callback_t callback) {
343343
}
344344

345345
node_callback_t NodeTools::expect_list_and_length(length_callback_t length_callback, node_callback_t callback) {
346-
return _abstract_statement_node_callback([length_callback, callback](_NodeStatementRange list) -> bool {
346+
return _abstract_statement_node_callback([length_callback, callback](_NodeStatementRange list) mutable -> bool {
347347
bool ret = true;
348348
auto dist = ranges::distance(list);
349349
size_t size = length_callback(dist);
@@ -392,10 +392,10 @@ node_callback_t NodeTools::expect_list(node_callback_t callback) {
392392
}
393393

394394
node_callback_t NodeTools::expect_length(callback_t<size_t> callback) {
395-
return [callback](ast::NodeCPtr node) -> bool {
395+
return [callback](ast::NodeCPtr node) mutable -> bool {
396396
bool ret = true;
397397
ret &= expect_list_and_length(
398-
[callback, &ret](size_t size) -> size_t {
398+
[callback, &ret](size_t size) mutable -> size_t {
399399
ret &= callback(size);
400400
return 0;
401401
},
@@ -406,7 +406,7 @@ node_callback_t NodeTools::expect_length(callback_t<size_t> callback) {
406406
}
407407

408408
template<typename Key>
409-
static node_callback_t _expect_key(Key key, NodeCallback auto callback, bool* key_found, bool allow_duplicates) {
409+
static node_callback_t _expect_key(Key key, NodeCallback auto&& callback, bool* key_found, bool allow_duplicates) {
410410
if constexpr (std::same_as<Key, ovdl::symbol<char>>) {
411411
if (!key) {
412412
if (key_found != nullptr) {
@@ -427,7 +427,7 @@ static node_callback_t _expect_key(Key key, NodeCallback auto callback, bool* ke
427427
}
428428
};
429429

430-
return _abstract_statement_node_callback([key, callback, key_found, allow_duplicates](_NodeStatementRange list) -> bool {
430+
return _abstract_statement_node_callback([key, callback = FWD(callback), key_found, allow_duplicates](_NodeStatementRange list) mutable -> bool {
431431
bool ret = true;
432432
size_t keys_found = 0;
433433
for (auto sub_node : list) {
@@ -493,7 +493,7 @@ node_callback_t NodeTools::expect_dictionary(key_value_callback_t callback) {
493493
}
494494

495495
node_callback_t NodeTools::name_list_callback(callback_t<name_list_t&&> callback) {
496-
return [callback](ast::NodeCPtr node) -> bool {
496+
return [callback](ast::NodeCPtr node) mutable -> bool {
497497
name_list_t list;
498498
bool ret = expect_list_reserve_length(
499499
list, expect_identifier_or_string(vector_callback<std::string_view>(list))

src/openvic-simulation/dataloader/NodeTools.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "openvic-simulation/utility/Getters.hpp"
2020
#include "openvic-simulation/utility/TslHelper.hpp"
2121

22+
#include <function2/function2.hpp>
23+
2224
#define MOV(...) static_cast<std::remove_reference_t<decltype(__VA_ARGS__)>&&>(__VA_ARGS__)
2325
#define FWD(...) static_cast<decltype(__VA_ARGS__)>(__VA_ARGS__)
2426

@@ -84,7 +86,7 @@ namespace OpenVic {
8486
concept LengthCallback = Functor<Fn, std::size_t, std::size_t>;
8587

8688
template<typename... Args>
87-
using callback_t = std::function<bool(Args...)>;
89+
using callback_t = fu2::function_base<true, true, fu2::capacity_default, false, false, bool(Args...)>;
8890

8991
using node_callback_t = callback_t<ast::NodeCPtr>;
9092
constexpr bool success_callback(ast::NodeCPtr) {
@@ -112,7 +114,7 @@ namespace OpenVic {
112114

113115
template<std::signed_integral T>
114116
NodeCallback auto expect_int(callback_t<T>& callback, int base = 10) {
115-
return expect_int64([callback](int64_t val) -> bool {
117+
return expect_int64([callback](int64_t val) mutable -> bool {
116118
if (static_cast<int64_t>(std::numeric_limits<T>::lowest()) <= val &&
117119
val <= static_cast<int64_t>(std::numeric_limits<T>::max())) {
118120
return callback(val);
@@ -131,7 +133,7 @@ namespace OpenVic {
131133

132134
template<std::integral T>
133135
NodeCallback auto expect_uint(callback_t<T>& callback, int base = 10) {
134-
return expect_uint64([callback](uint64_t val) -> bool {
136+
return expect_uint64([callback](uint64_t val) mutable -> bool {
135137
if (val <= static_cast<uint64_t>(std::numeric_limits<T>::max())) {
136138
return callback(val);
137139
}
@@ -169,7 +171,7 @@ namespace OpenVic {
169171
node_callback_t expect_fvec4(callback_t<fvec4_t> callback);
170172
node_callback_t expect_assign(key_value_callback_t callback);
171173

172-
using length_callback_t = std::function<size_t(size_t)>;
174+
using length_callback_t = fu2::function<size_t(size_t)>;
173175
constexpr size_t default_length_callback(size_t size) {
174176
return size;
175177
};
@@ -443,7 +445,7 @@ namespace OpenVic {
443445
Callback<std::string_view> auto expect_mapped_string(
444446
template_string_map_t<T, Case> const& map, Callback<T> auto&& callback, bool warn = false
445447
) {
446-
return [&map, callback = FWD(callback), warn](std::string_view string) -> bool {
448+
return [&map, callback = FWD(callback), warn](std::string_view string) mutable -> bool {
447449
const typename template_string_map_t<T, Case>::const_iterator it = map.find(string);
448450
if (it != map.end()) {
449451
return callback(it->second);

src/openvic-simulation/economy/production/ProductionType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ ProductionTypeManager::ProductionTypeManager() :
6060
node_callback_t ProductionTypeManager::_expect_job(
6161
GoodDefinitionManager const& good_definition_manager, PopManager const& pop_manager, callback_t<Job&&> callback
6262
) {
63-
return [this, &good_definition_manager, &pop_manager, callback](ast::NodeCPtr node) -> bool {
63+
return [this, &good_definition_manager, &pop_manager, callback](ast::NodeCPtr node) mutable -> bool {
6464
using enum Job::effect_t;
6565

6666
std::string_view pop_type {};
@@ -87,7 +87,7 @@ node_callback_t ProductionTypeManager::_expect_job_list(
8787
GoodDefinitionManager const& good_definition_manager, PopManager const& pop_manager,
8888
callback_t<std::vector<Job>&&> callback
8989
) {
90-
return [this, &good_definition_manager, &pop_manager, callback](ast::NodeCPtr node) -> bool {
90+
return [this, &good_definition_manager, &pop_manager, callback](ast::NodeCPtr node) mutable -> bool {
9191
std::vector<Job> jobs;
9292
bool ret = expect_list(_expect_job(good_definition_manager, pop_manager, vector_callback(jobs)))(node);
9393
ret &= callback(std::move(jobs));

0 commit comments

Comments
 (0)