mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-06-21 01:04:06 +00:00
Adds PS::span and std::string_view to CmdLineArgs.
Patch By: phosit Differential Revision: https://code.wildfiregames.com/D4677 This was SVN commit r27106.
This commit is contained in:
+6
-5
@@ -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<const char* const> 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<const char**>(argv));
|
||||
// static_cast is ok, argc is never negative.
|
||||
RunGameOrAtlas({argv, static_cast<std::size_t>(argc)});
|
||||
|
||||
// Shut down profiler initialised by EarlyInit
|
||||
g_Profiler2.Shutdown();
|
||||
|
||||
+3
-6
@@ -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<class InputIterator>
|
||||
CStr (InputIterator first, InputIterator last) : StrBase(first, last) {}
|
||||
|
||||
using StrBase::StrBase;
|
||||
|
||||
/**
|
||||
* Repeat: Named constructor, to avoid overload overload.
|
||||
|
||||
@@ -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 <algorithm>
|
||||
#include <iterator>
|
||||
#include <string_view>
|
||||
|
||||
CmdLineArgs g_CmdLineArgs;
|
||||
|
||||
namespace
|
||||
@@ -42,41 +46,37 @@ private:
|
||||
|
||||
} // namespace
|
||||
|
||||
CmdLineArgs::CmdLineArgs(int argc, const char* argv[])
|
||||
CmdLineArgs::CmdLineArgs(const PS::span<const char* const> 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 <utility>
|
||||
#include <vector>
|
||||
|
||||
class CmdLineArgs
|
||||
@@ -33,10 +35,9 @@ public:
|
||||
* All arguments are required to be of the form <tt>-name</tt> or
|
||||
* <tt>-name=value</tt> - 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<const char* const> argv);
|
||||
|
||||
/**
|
||||
* Test whether the given name was specified, as either <tt>-name</tt> or
|
||||
|
||||
@@ -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 <array>
|
||||
|
||||
class TestCmdLineArgs : public CxxTest::TestSuite
|
||||
{
|
||||
public:
|
||||
void test_has()
|
||||
{
|
||||
const char* argv[] = { "program", "-test2" };
|
||||
CmdLineArgs c(ARRAY_SIZE(argv), argv);
|
||||
constexpr std::array<const char*, 2> 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<const char*, 5> 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<const char*, 5> 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<const char*, 6> 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<const char*, 1> argv = { "program" };
|
||||
CmdLineArgs c(argv);
|
||||
TS_ASSERT_WSTR_EQUALS(c.GetArg0().string(), L"program");
|
||||
|
||||
CmdLineArgs c2(0, NULL);
|
||||
CmdLineArgs c2(PS::span<const char* const>{});
|
||||
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<const char*, 1> 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<const char*, 6> 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" };
|
||||
|
||||
@@ -38,7 +38,7 @@ class span
|
||||
public:
|
||||
using element_type = T;
|
||||
using value_type = std::remove_cv_t<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<size_type>(last - first)) {}
|
||||
|
||||
template<typename OtherT, size_t N>
|
||||
constexpr span(const std::array<OtherT, N>& 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<typename T, size_t N>
|
||||
span(const std::array<T, N>&) -> span<const T>;
|
||||
|
||||
} // namespace PS
|
||||
|
||||
#endif // INCLUDED_PS_SPAN
|
||||
|
||||
Reference in New Issue
Block a user