Skip to content

Commit 1edf08d

Browse files
committed
use std::string_view. eliminate C-varargs when building state_change_event table.
1 parent a52f36b commit 1edf08d

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

library/Core.cpp

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -316,46 +316,46 @@ static std::string dfhack_version_desc()
316316
return s.str();
317317
}
318318

319-
static bool init_run_script(color_ostream &out, lua_State *state, const std::string& pcmd, const std::span<const std::string> pargs)
319+
static bool init_run_script(color_ostream &out, lua_State *state, const std::string_view pcmd, const std::span<const std::string> pargs)
320320
{
321321
if (!lua_checkstack(state, pargs.size()+10))
322322
return false;
323323
Lua::PushDFHack(state);
324324
lua_getfield(state, -1, "run_script");
325325
lua_remove(state, -2);
326-
lua_pushstring(state, pcmd.c_str());
326+
lua_pushlstring(state, pcmd.data(), pcmd.size());
327327
for (const auto& arg : pargs)
328328
lua_pushstring(state, arg.c_str());
329329
return true;
330330
}
331331

332-
static command_result runLuaScript(color_ostream &out, std::string name, const std::span<const std::string> args)
332+
static command_result runLuaScript(color_ostream &out, const std::string_view name, const std::span<const std::string> args)
333333
{
334-
auto init_fn = [n = std::move(name), args](color_ostream& out, lua_State* state) -> bool {
335-
return init_run_script(out, state, n, args);
334+
auto init_fn = [name, args](color_ostream& out, lua_State* state) -> bool {
335+
return init_run_script(out, state, name, args);
336336
};
337337

338338
bool ok = Lua::RunCoreQueryLoop(out, DFHack::Core::getInstance().getLuaState(true), init_fn);
339339

340340
return ok ? CR_OK : CR_FAILURE;
341341
}
342342

343-
static bool init_enable_script(color_ostream &out, lua_State *state, const std::string& name, bool enable)
343+
static bool init_enable_script(color_ostream &out, lua_State *state, const std::string_view name, bool enable)
344344
{
345345
if (!lua_checkstack(state, 4))
346346
return false;
347347
Lua::PushDFHack(state);
348348
lua_getfield(state, -1, "enable_script");
349349
lua_remove(state, -2);
350-
lua_pushstring(state, name.c_str());
350+
lua_pushlstring(state, name.data(), name.size());
351351
lua_pushboolean(state, enable);
352352
return true;
353353
}
354354

355-
static command_result enableLuaScript(color_ostream &out, std::string name, bool enabled)
355+
static command_result enableLuaScript(color_ostream &out, const std::string_view name, bool enabled)
356356
{
357-
auto init_fn = [n = std::move(name), enabled](color_ostream& out, lua_State* state) -> bool {
358-
return init_enable_script(out, state, n, enabled);
357+
auto init_fn = [name, enabled](color_ostream& out, lua_State* state) -> bool {
358+
return init_enable_script(out, state, name, enabled);
359359
};
360360

361361
bool ok = Lua::RunCoreQueryLoop(out, DFHack::Core::getInstance().getLuaState(), init_fn);
@@ -677,7 +677,7 @@ static void ls_helper(color_ostream &con, const std::span<const std::string> par
677677
std::vector<std::string> filter;
678678
bool skip_tags = false;
679679
bool show_dev_commands = false;
680-
std::string exclude_strs;
680+
std::string_view exclude_strs;
681681

682682
bool in_exclude = false;
683683
for (const auto& str : params) {
@@ -2188,24 +2188,31 @@ namespace DFHack {
21882188
using EntryVector = std::vector<Entry>;
21892189
using InitVariationTable = std::map<Key,Val>;
21902190

2191-
static EntryVector computeInitVariationTable(void* none, ...) {
2192-
va_list list;
2193-
va_start(list,none);
2191+
template <typename T>
2192+
concept VariationTableTypes = std::same_as<T, Key> || std::convertible_to<T, std::string_view>;
2193+
2194+
template <VariationTableTypes... Ts>
2195+
static EntryVector computeInitVariationTable(Ts&&... ts) {
2196+
using Arg = std::variant<Key, std::string_view>;
2197+
std::vector<Arg> args{ Arg{std::forward<Ts>(ts)}... };
2198+
2199+
Key current_key = SC_UNKNOWN;
21942200
EntryVector result;
2195-
while(true) {
2196-
Key key = (Key)va_arg(list,int);
2197-
if ( key == SC_UNKNOWN )
2198-
break;
2199-
Val val;
2200-
while (true) {
2201-
const char *v = va_arg(list, const char *);
2202-
if (!v || !v[0])
2203-
break;
2204-
val.emplace_back(v);
2201+
Val val;
2202+
for (auto& arg : args) {
2203+
if (std::holds_alternative<Key>(arg)) {
2204+
if (current_key != SC_UNKNOWN && !val.empty()) {
2205+
result.emplace_back(current_key, val);
2206+
val.clear();
2207+
}
2208+
current_key = std::get<Key>(arg);
2209+
} else if (std::holds_alternative<std::string_view>(arg)) {
2210+
auto str = std::get<std::string_view>(arg);
2211+
if (!str.empty())
2212+
val.emplace_back(str);
22052213
}
2206-
result.push_back(Entry(key,val));
22072214
}
2208-
va_end(list);
2215+
22092216
return result;
22102217
}
22112218

@@ -2216,12 +2223,12 @@ namespace DFHack {
22162223
}
22172224

22182225
void Core::handleLoadAndUnloadScripts(color_ostream& out, state_change_event event) {
2219-
static const X::InitVariationTable table = X::getTable(X::computeInitVariationTable(nullptr,
2220-
(int)SC_WORLD_LOADED, "onLoad", "onLoadWorld", "onWorldLoaded", "",
2221-
(int)SC_WORLD_UNLOADED, "onUnload", "onUnloadWorld", "onWorldUnloaded", "",
2222-
(int)SC_MAP_LOADED, "onMapLoad", "onLoadMap", "",
2223-
(int)SC_MAP_UNLOADED, "onMapUnload", "onUnloadMap", "",
2224-
(int)SC_UNKNOWN
2226+
static const X::InitVariationTable table = X::getTable(X::computeInitVariationTable(
2227+
SC_WORLD_LOADED, "onLoad", "onLoadWorld", "onWorldLoaded",
2228+
SC_WORLD_UNLOADED, "onUnload", "onUnloadWorld", "onWorldUnloaded",
2229+
SC_MAP_LOADED, "onMapLoad", "onLoadMap",
2230+
SC_MAP_UNLOADED, "onMapUnload", "onUnloadMap",
2231+
SC_UNKNOWN
22252232
));
22262233

22272234
if (!df::global::world)

0 commit comments

Comments
 (0)