Make use of C++17 features in LuaConsole and filesystem walking

This commit is contained in:
Alexander Bock
2017-07-15 14:37:00 -04:00
parent 00e684f7dc
commit ec6641038d
3 changed files with 18 additions and 21 deletions
+2 -4
View File
@@ -52,9 +52,7 @@ namespace {
// A high number is chosen since we didn't have a version number before
// any small number might also be equal to the console history length
// @CPP17
//const uint64_t CurrentVersion = 0xFEEE'FEEE'0000'0001;
const uint64_t CurrentVersion = 0xFEEEFEEE00000001;
const uint64_t CurrentVersion = 0xFEEE'FEEE'0000'0001;
const openspace::Key CommandInputButton = openspace::Key::GraveAccent;
@@ -750,7 +748,7 @@ void LuaConsole::render() {
res.y - HistoryFontSize * 1.5f + _fullHeight - _currentHeight
);
// @CPP17: Replace with array_view
// @CPP: Replace with array_view
std::vector<std::string> commandSubset;
if (_commandsHistory.size() < static_cast<size_t>(_historyLength)) {
commandSubset = _commandsHistory;
+15 -16
View File
@@ -30,16 +30,9 @@ namespace luascriptfunctions {
namespace {
using walkFunc = std::vector<std::string>(ghoul::filesystem::Directory::*)(
ghoul::filesystem::Directory::Recursive, ghoul::filesystem::Directory::Sort) const;
// Defining a common walk function that works off a pointer-to-member function (defined
// above) allows us to easily reuse this code
int walkCommon(lua_State* L, walkFunc func) {
// @CPP17 Replace with std::invoke
#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
using namespace ghoul::filesystem;
// Defining a common walk function that works off a pointer-to-member function
template <typename Func>
int walkCommon(lua_State* L, Func func) {
const int nArguments = lua_gettop(L);
if (nArguments < 1 || nArguments > 3) {
return luaL_error(L, "Expected %i-%i arguments, got %i", 1, 3, nArguments);
@@ -49,29 +42,35 @@ int walkCommon(lua_State* L, walkFunc func) {
if (nArguments == 1) {
// Only the path was passed
const std::string path = luaL_checkstring(L, -1);
result = CALL_MEMBER_FN(Directory(path), func)(
result = std::invoke(
func,
ghoul::filesystem::Directory(path),
ghoul::filesystem::Directory::Recursive::No,
ghoul::filesystem::Directory::Sort::No
);
);
}
else if (nArguments == 2) {
// The path and the recursive value were passed
const std::string path = luaL_checkstring(L, -2);
const bool recursive = lua_toboolean(L, -1) != 0;
result = CALL_MEMBER_FN(Directory(path), func)(
result = std::invoke(
func,
ghoul::filesystem::Directory(path),
ghoul::filesystem::Directory::Recursive(recursive),
ghoul::filesystem::Directory::Sort::No
);
);
}
else if (nArguments == 3) {
// All three arguments were passed
const std::string path = luaL_checkstring(L, -3);
const bool recursive = lua_toboolean(L, -2) != 0;
const bool sorted = lua_toboolean(L, -1) != 0;
result = CALL_MEMBER_FN(Directory(path), func)(
result = std::invoke(
func,
ghoul::filesystem::Directory(path),
ghoul::filesystem::Directory::Recursive(recursive),
ghoul::filesystem::Directory::Sort(sorted)
);
);
}
// Copy values into the lua_State