1+ #include < variant>
2+
13#include " Debug.h"
24#include " LuaTools.h"
35#include " PluginManager.h"
@@ -25,7 +27,7 @@ namespace DFHack {
2527 DBG_DECLARE (sort, log, DebugCategory::LINFO);
2628}
2729
28- using item_or_unit = std::pair< void *, bool >;
30+ using item_or_unit = std::variant<df::unit *, df::item* >;
2931using filter_vec_type = std::vector<std::function<bool (item_or_unit)>>;
3032
3133// recreated here since our autogenerated df::sort_entry lacks template params
@@ -44,8 +46,8 @@ static bool probing = false;
4446static bool probe_result = false ;
4547
4648static bool do_filter (const char *module_name, const char *fn_name, const item_or_unit &elem) {
47- if (elem. second ) return true ;
48- auto unit = ( df::unit *) elem. first ;
49+ if (std::holds_alternative<df::item*>( elem) ) return true ;
50+ auto unit = std::get< df::unit*>( elem) ;
4951
5052 if (probing) {
5153 TRACE (log).print (" probe successful\n " );
@@ -78,11 +80,11 @@ static bool do_work_animal_assignment_filter(item_or_unit elem) {
7880
7981static int32_t our_filter_idx (df::widget_unit_list *unitlist) {
8082 if (world->units .active .empty ())
81- return true ;
83+ return - 1 ;
8284
8385 df::unit *u = world->units .active [0 ]; // any unit will do; we just need a sentinel
8486 if (!u)
85- return true ;
87+ return - 1 ;
8688
8789 probing = true ;
8890 probe_result = false ;
@@ -91,8 +93,8 @@ static int32_t our_filter_idx(df::widget_unit_list *unitlist) {
9193 filter_vec_type *filter_vec = reinterpret_cast <filter_vec_type *>(&unitlist->filter_func );
9294
9395 TRACE (log).print (" probing for our filter function\n " );
94- for (auto fn : *filter_vec) {
95- fn (std::make_pair (u, false ));
96+ for (auto & fn : *filter_vec) {
97+ fn (item_or_unit (u ));
9698 if (probe_result) {
9799 TRACE (log).print (" found our filter function at idx %d\n " , idx);
98100 break ;
@@ -144,13 +146,13 @@ static df::widget_unit_list * get_work_animal_assignment_unit_list() {
144146//
145147
146148static bool sort_proxy (const item_or_unit &a, const item_or_unit &b) {
147- if (a. second || b. second )
149+ if (std::holds_alternative<df::item*>(a) || std::holds_alternative<df::item*>(b) )
148150 return true ;
149151
150152 bool ret = true ;
151153 color_ostream &out = Core::getInstance ().getConsole ();
152154 Lua::CallLuaModuleFunction (out, " plugins.sort" , " do_sort" ,
153- std::make_tuple (( df::unit *)a. first , ( df::unit *)b. first ),
155+ std::make_tuple (std::get< df::unit*>(a), std::get< df::unit*>(b) ),
154156 1 , [&](lua_State *L){
155157 ret = lua_toboolean (L, 1 );
156158 }
0 commit comments