From 6ca43f056d7ba8ea816670478a34ec5488c231a6 Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Tue, 4 Oct 2022 19:08:39 +0000 Subject: [PATCH] Adds PS::span and std::string_view to CmdLineArgs. Patch By: phosit Differential Revision: https://code.wildfiregames.com/D4677 This was SVN commit r27106. --- source/main.cpp | 11 +++-- source/ps/CStr.h | 9 ++-- source/ps/GameSetup/CmdLineArgs.cpp | 48 ++++++++++---------- source/ps/GameSetup/CmdLineArgs.h | 11 +++-- source/ps/GameSetup/tests/test_CmdLineArgs.h | 43 +++++++++++------- source/ps/containers/Span.h | 11 ++++- 6 files changed, 76 insertions(+), 57 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index cdd23129da..ccde1a24c7 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -108,8 +108,8 @@ typedef unsigned long DWORD; // 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 @@ static void StartRLInterface(CmdLineArgs args) // 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 @@ extern "C" int main(int argc, char* argv[]) 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(); diff --git a/source/ps/CStr.h b/source/ps/CStr.h index 0f475e2bd6..c451526596 100644 --- a/source/ps/CStr.h +++ b/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 @@ public: 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. diff --git a/source/ps/GameSetup/CmdLineArgs.cpp b/source/ps/GameSetup/CmdLineArgs.cpp index 5d6a17f777..ad20a16187 100644 --- a/source/ps/GameSetup/CmdLineArgs.cpp +++ b/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 @@ private: } // 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; - for (int i = 1; i < argc; ++i) + 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; + + // 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(), '='); - m_Args.emplace_back(std::move(name), std::move(value)); + CStr value = (eq != afterDashes.end()) ? CStr{eq + 1, afterDashes.end()} : CStr{}; + + m_Args.emplace_back(CStr(afterDashes.begin(), eq), std::move(value)); } } diff --git a/source/ps/GameSetup/CmdLineArgs.h b/source/ps/GameSetup/CmdLineArgs.h index 028c3fa4db..85c430e2a9 100644 --- a/source/ps/GameSetup/CmdLineArgs.h +++ b/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 @@ public: * 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 diff --git a/source/ps/GameSetup/tests/test_CmdLineArgs.h b/source/ps/GameSetup/tests/test_CmdLineArgs.h index d0ed81827c..6fbfb8338f 100644 --- a/source/ps/GameSetup/tests/test_CmdLineArgs.h +++ b/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 @@ public: 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 @@ public: 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 @@ public: 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 @@ public: 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 @@ public: 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" }; diff --git a/source/ps/containers/Span.h b/source/ps/containers/Span.h index 50d66161bd..145fdbf910 100644 --- a/source/ps/containers/Span.h +++ b/source/ps/containers/Span.h @@ -38,7 +38,7 @@ class span 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 @@ public: 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 @@ public: 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