Index: ps/trunk/source/main.cpp =================================================================== --- ps/trunk/source/main.cpp +++ ps/trunk/source/main.cpp @@ -108,8 +108,8 @@ // Request the high performance GPU on Windows by default if no system override is specified. // See: // - https://github.com/supertuxkart/stk-code/pull/4693/commits/0a99c667ef513b2ce0f5755729a6e05df8aac48a -// - https://docs.nvidia.com/gameworks/content/technologies/desktop/optimus.htm -// - https://gpuopen.com/learn/amdpowerxpressrequesthighperformance/ +// - https://docs.nvidia.com/gameworks/content/technologies/desktop/optimus.htm +// - https://gpuopen.com/learn/amdpowerxpressrequesthighperformance/ extern "C" { __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; @@ -496,9 +496,9 @@ // moved into a helper function to ensure args is destroyed before // exit(), which may result in a memory leak. -static void RunGameOrAtlas(int argc, const char* argv[]) +static void RunGameOrAtlas(const PS::span argv) { - CmdLineArgs args(argc, argv); + const CmdLineArgs args(argv); g_CmdLineArgs = args; @@ -736,7 +736,8 @@ EarlyInit(); // must come at beginning of main - RunGameOrAtlas(argc, const_cast(argv)); + // static_cast is ok, argc is never negative. + RunGameOrAtlas({argv, static_cast(argc)}); // Shut down profiler initialised by EarlyInit g_Profiler2.Shutdown(); Index: ps/trunk/source/ps/CStr.h =================================================================== --- ps/trunk/source/ps/CStr.h +++ ps/trunk/source/ps/CStr.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -63,12 +63,9 @@ using StrBase = std::tstring; using Char = typename std::tstring::value_type; - CStr() {} - CStr(const Char* str) : StrBase(str) {} - CStr(const Char* str, size_t len) : StrBase(str, len) {} CStr(const StrBase& str) : StrBase(str) {} - template - CStr (InputIterator first, InputIterator last) : StrBase(first, last) {} + + using StrBase::StrBase; /** * Repeat: Named constructor, to avoid overload overload. Index: ps/trunk/source/ps/GameSetup/CmdLineArgs.h =================================================================== --- ps/trunk/source/ps/GameSetup/CmdLineArgs.h +++ ps/trunk/source/ps/GameSetup/CmdLineArgs.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -18,9 +18,11 @@ #ifndef INCLUDED_CMDLINEARGS #define INCLUDED_CMDLINEARGS -#include "ps/CStr.h" #include "lib/os_path.h" +#include "ps/containers/Span.h" +#include "ps/CStr.h" +#include #include class CmdLineArgs @@ -33,10 +35,9 @@ * All arguments are required to be of the form -name or * -name=value - anything else is ignored. * - * @param argc size of argv array - * @param argv array of arguments; argv[0] should be the program's name + * @param argv span of arguments; argv[0] should be the program's name */ - CmdLineArgs(int argc, const char* argv[]); + CmdLineArgs(const PS::span argv); /** * Test whether the given name was specified, as either -name or Index: ps/trunk/source/ps/GameSetup/CmdLineArgs.cpp =================================================================== --- ps/trunk/source/ps/GameSetup/CmdLineArgs.cpp +++ ps/trunk/source/ps/GameSetup/CmdLineArgs.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -20,6 +20,10 @@ #include "lib/sysdep/sysdep.h" +#include +#include +#include + CmdLineArgs g_CmdLineArgs; namespace @@ -42,41 +46,37 @@ } // namespace -CmdLineArgs::CmdLineArgs(int argc, const char* argv[]) +CmdLineArgs::CmdLineArgs(const PS::span argv) { - if (argc >= 1) - { - std::string arg0(argv[0]); - // avoid OsPath complaining about mixing both types of separators, - // which happens when running in the VC2010 debugger - std::replace(arg0.begin(), arg0.end(), '/', SYS_DIR_SEP); - m_Arg0 = arg0; - } + if (argv.empty()) + return; + + std::string arg0(argv[0]); + // avoid OsPath complaining about mixing both types of separators, + // which happens when running in the VC2010 debugger + std::replace(arg0.begin(), arg0.end(), '/', SYS_DIR_SEP); + m_Arg0 = arg0; - for (int i = 1; i < argc; ++i) + // Implicit conversion from const char* to std::string_view. + for (const std::string_view arg : argv.subspan(1)) { // Only accept arguments that start with '-' - if (argv[i][0] != '-') + if (arg[0] != '-') { - m_ArgsWithoutName.emplace_back(argv[i]); + m_ArgsWithoutName.emplace_back(arg); continue; } // Allow -arg and --arg - char offset = argv[i][1] == '-' ? 2 : 1; - CStr name, value; + const std::string_view afterDashes = arg.substr(arg[1] == '-' ? 2 : 1); // Check for "-arg=value" - const char* eq = strchr(argv[i], '='); - if (eq) - { - name = CStr(argv[i]+offset, eq-argv[i]-offset); - value = CStr(eq+1); - } - else - name = CStr(argv[i]+offset); + const std::string_view::iterator eq = + std::find(afterDashes.begin(), afterDashes.end(), '='); + + CStr value = (eq != afterDashes.end()) ? CStr{eq + 1, afterDashes.end()} : CStr{}; - m_Args.emplace_back(std::move(name), std::move(value)); + m_Args.emplace_back(CStr(afterDashes.begin(), eq), std::move(value)); } } Index: ps/trunk/source/ps/GameSetup/tests/test_CmdLineArgs.h =================================================================== --- ps/trunk/source/ps/GameSetup/tests/test_CmdLineArgs.h +++ ps/trunk/source/ps/GameSetup/tests/test_CmdLineArgs.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2018 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -19,13 +19,15 @@ #include "ps/GameSetup/CmdLineArgs.h" +#include + class TestCmdLineArgs : public CxxTest::TestSuite { public: void test_has() { - const char* argv[] = { "program", "-test2" }; - CmdLineArgs c(ARRAY_SIZE(argv), argv); + constexpr std::array argv = { "program", "-test2" }; + CmdLineArgs c(argv); TS_ASSERT(!c.Has("test1")); TS_ASSERT(c.Has("test2")); TS_ASSERT(!c.Has("test3")); @@ -34,8 +36,11 @@ void test_get() { - const char* argv[] = { "program", "-test1=", "--test2=x", "-test3=-y=y-", "-=z" }; - CmdLineArgs c(ARRAY_SIZE(argv), argv); + constexpr std::array argv = + { + "program", "-test1=", "--test2=x", "-test3=-y=y-", "-=z" + }; + CmdLineArgs c(argv); TS_ASSERT(!c.Has("program")); TS_ASSERT_STR_EQUALS(c.Get("test0"), ""); TS_ASSERT_STR_EQUALS(c.Get("test1"), ""); @@ -46,8 +51,11 @@ void test_multiple() { - const char* argv[] = { "program", "-test1=one", "--test1=two", "-test2=none", "-test1=three" }; - CmdLineArgs c(ARRAY_SIZE(argv), argv); + constexpr std::array argv = + { + "program", "-test1=one", "--test1=two", "-test2=none", "-test1=three" + }; + CmdLineArgs c(argv); TS_ASSERT_STR_EQUALS(c.Get("test1"), "one"); TS_ASSERT_STR_EQUALS(c.Get("test2"), "none"); @@ -66,10 +74,10 @@ void test_get_invalid() { - const char* argv[] = { + constexpr std::array argv = { "-test1", "--test2", "test3-", " -test4", "--", "-==" }; - CmdLineArgs c(ARRAY_SIZE(argv), argv); + CmdLineArgs c(argv); TS_ASSERT(!c.Has("test1")); TS_ASSERT(c.Has("test2")); @@ -79,15 +87,15 @@ void test_arg0() { - const char* argv[] = { "program" }; - CmdLineArgs c(ARRAY_SIZE(argv), argv); + constexpr std::array argv = { "program" }; + CmdLineArgs c(argv); TS_ASSERT_WSTR_EQUALS(c.GetArg0().string(), L"program"); - CmdLineArgs c2(0, NULL); + CmdLineArgs c2(PS::span{}); TS_ASSERT_WSTR_EQUALS(c2.GetArg0().string(), L""); - const char* argv3[] = { "ab/cd/ef/gh/../ij" }; - CmdLineArgs c3(ARRAY_SIZE(argv3), argv3); + const std::array argv3 = { "ab/cd/ef/gh/../ij" }; + CmdLineArgs c3(argv3); #if OS_WIN TS_ASSERT_WSTR_EQUALS(c3.GetArg0().string(), L"ab\\cd\\ef\\gh\\..\\ij"); #else @@ -97,8 +105,11 @@ void test_get_without_names() { - const char* argv[] = { "program", "test0", "-test1", "test2", "test3", "--test4=test5" }; - CmdLineArgs c(ARRAY_SIZE(argv), argv); + constexpr std::array argv = + { + "program", "test0", "-test1", "test2", "test3", "--test4=test5" + }; + CmdLineArgs c(argv); TS_ASSERT(c.Has("test1")); TS_ASSERT_STR_EQUALS(c.Get("test4"), "test5"); CStr expected_args[] = { "test0", "test2", "test3" }; Index: ps/trunk/source/ps/containers/Span.h =================================================================== --- ps/trunk/source/ps/containers/Span.h +++ ps/trunk/source/ps/containers/Span.h @@ -38,7 +38,7 @@ public: using element_type = T; using value_type = std::remove_cv_t; - using size_type = std::size_t; + using size_type = size_t; using pointer = T*; using reference = T&; using iterator = pointer; @@ -52,6 +52,10 @@ constexpr span(iterator first, iterator last) : m_Pointer(first), m_Extent(static_cast(last - first)) {} + template + constexpr span(const std::array& arr) + : m_Pointer(arr.data()), m_Extent(arr.size()) {} + constexpr span(const span& other) = default; constexpr span& operator=(const span& other) = default; @@ -66,11 +70,16 @@ constexpr iterator begin() const { return m_Pointer; } constexpr iterator end() const { return m_Pointer + m_Extent; } + constexpr span subspan(size_type offset) const { return {m_Pointer + offset, m_Extent - offset}; } + private: pointer m_Pointer; size_type m_Extent; }; +template +span(const std::array&) -> span; + } // namespace PS #endif // INCLUDED_PS_SPAN