1
0
Fork 0

Codechange: Pass span of strings to drivers instead of StringList.

StringList is a `std::vector<std::string>`, and drivers do not need to modify the parameters.

This also renames parameter to be the same for all implementations.
pull/13139/head
Peter Nelson 2024-12-01 16:54:42 +00:00
parent 3a310f1802
commit 37934f0a3c
No known key found for this signature in database
GPG Key ID: 8EF8F0A467DF75ED
47 changed files with 64 additions and 64 deletions

View File

@ -41,7 +41,7 @@ static const std::string HWACCELERATION_TEST_FILE = "hwaccel.dat"; ///< Filename
* @param name The parameter name we're looking for.
* @return The parameter value.
*/
const char *GetDriverParam(const StringList &parm, const char *name)
const char *GetDriverParam(std::span<const std::string> parm, const char *name)
{
if (parm.empty()) return nullptr;
@ -61,7 +61,7 @@ const char *GetDriverParam(const StringList &parm, const char *name)
* @param name The parameter name we're looking for.
* @return The parameter value.
*/
bool GetDriverParamBool(const StringList &parm, const char *name)
bool GetDriverParamBool(std::span<const std::string> parm, const char *name)
{
return GetDriverParam(parm, name) != nullptr;
}
@ -73,7 +73,7 @@ bool GetDriverParamBool(const StringList &parm, const char *name)
* @param def The default value if the parameter doesn't exist.
* @return The parameter value.
*/
int GetDriverParamInt(const StringList &parm, const char *name, int def)
int GetDriverParamInt(std::span<const std::string> parm, const char *name, int def)
{
const char *p = GetDriverParam(parm, name);
return p != nullptr ? atoi(p) : def;

View File

@ -11,11 +11,10 @@
#define DRIVER_H
#include "core/enum_type.hpp"
#include "string_type.h"
const char *GetDriverParam(const StringList &parm, const char *name);
bool GetDriverParamBool(const StringList &parm, const char *name);
int GetDriverParamInt(const StringList &parm, const char *name, int def);
const char *GetDriverParam(std::span<const std::string> parm, const char *name);
bool GetDriverParamBool(std::span<const std::string> parm, const char *name);
int GetDriverParamInt(std::span<const std::string> parm, const char *name, int def);
/** A driver for communicating with the user. */
class Driver {
@ -25,7 +24,7 @@ public:
* @param parm Parameters passed to the driver.
* @return std::nullopt if everything went okay, otherwise an error message.
*/
virtual std::optional<std::string_view> Start(const StringList &parm) = 0;
virtual std::optional<std::string_view> Start(std::span<const std::string> parm) = 0;
/**
* Stop this driver.

View File

@ -26,7 +26,7 @@ static MIDI *_midi = nullptr;
*/
extern int _allegro_instance_count;
std::optional<std::string_view> MusicDriver_Allegro::Start(const StringList &)
std::optional<std::string_view> MusicDriver_Allegro::Start(std::span<const std::string> )
{
if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, nullptr)) {
Debug(driver, 0, "allegro: install_allegro failed '{}'", allegro_error);

View File

@ -15,7 +15,7 @@
/** Allegro's music player. */
class MusicDriver_Allegro : public MusicDriver {
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;

View File

@ -18,7 +18,7 @@
/** Factory for BeOS' midi player. */
static FMusicDriver_BeMidi iFMusicDriver_BeMidi;
std::optional<std::string_view> MusicDriver_BeMidi::Start(const StringList &parm)
std::optional<std::string_view> MusicDriver_BeMidi::Start(std::span<const std::string>)
{
return std::nullopt;
}

View File

@ -18,7 +18,8 @@
/** The midi player for BeOS. */
class MusicDriver_BeMidi : public MusicDriver {
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;

View File

@ -79,7 +79,7 @@ static void DoSetVolume()
/**
* Initialized the MIDI player, including QuickTime initialization.
*/
std::optional<std::string_view> MusicDriver_Cocoa::Start(const StringList &)
std::optional<std::string_view> MusicDriver_Cocoa::Start(std::span<const std::string>)
{
if (NewMusicPlayer(&_player) != noErr) return "failed to create music player";

View File

@ -14,7 +14,7 @@
class MusicDriver_Cocoa : public MusicDriver {
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;

View File

@ -1063,7 +1063,7 @@ static const char *LoadDefaultDLSFile(const char *user_dls)
}
std::optional<std::string_view> MusicDriver_DMusic::Start(const StringList &parm)
std::optional<std::string_view> MusicDriver_DMusic::Start(std::span<const std::string> parm)
{
/* Initialize COM */
if (FAILED(CoInitializeEx(nullptr, COINIT_MULTITHREADED))) return "COM initialization failed";

View File

@ -17,7 +17,7 @@ class MusicDriver_DMusic : public MusicDriver {
public:
virtual ~MusicDriver_DMusic();
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;

View File

@ -35,7 +35,7 @@
/** Factory for the midi player that uses external players. */
static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi;
std::optional<std::string_view> MusicDriver_ExtMidi::Start(const StringList &parm)
std::optional<std::string_view> MusicDriver_ExtMidi::Start(std::span<const std::string> parm)
{
if (VideoDriver::GetInstance()->GetName() == "allegro" ||
SoundDriver::GetInstance()->GetName() == "allegro") {

View File

@ -22,7 +22,7 @@ private:
void DoStop();
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;

View File

@ -58,11 +58,11 @@ static void RenderMusicStream(int16_t *buffer, size_t samples)
fluid_synth_write_s16(_midi.synth, samples, buffer, 0, 2, buffer, 1, 2);
}
std::optional<std::string_view> MusicDriver_FluidSynth::Start(const StringList &param)
std::optional<std::string_view> MusicDriver_FluidSynth::Start(std::span<const std::string> parm)
{
std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
const char *sfont_name = GetDriverParam(param, "soundfont");
const char *sfont_name = GetDriverParam(parm, "soundfont");
int sfont_id;
Debug(driver, 1, "Fluidsynth: sf {}", sfont_name != nullptr ? sfont_name : "(null)");

View File

@ -15,7 +15,7 @@
/** Music driver making use of FluidSynth. */
class MusicDriver_FluidSynth : public MusicDriver {
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;

View File

@ -15,7 +15,7 @@
/** The music player that does nothing. */
class MusicDriver_Null : public MusicDriver {
public:
std::optional<std::string_view> Start(const StringList &) override { return std::nullopt; }
std::optional<std::string_view> Start(std::span<const std::string>) override { return std::nullopt; }
void Stop() override { }

View File

@ -367,7 +367,7 @@ void MusicDriver_Win32::SetVolume(uint8_t vol)
_midi.new_volume = vol;
}
std::optional<std::string_view> MusicDriver_Win32::Start(const StringList &parm)
std::optional<std::string_view> MusicDriver_Win32::Start(std::span<const std::string> parm)
{
Debug(driver, 2, "Win32-MIDI: Start: initializing");

View File

@ -15,7 +15,7 @@
/** The Windows music player. */
class MusicDriver_Win32 : public MusicDriver {
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;

View File

@ -50,7 +50,7 @@ void SoundDriver_Allegro::MainLoop()
*/
extern int _allegro_instance_count;
std::optional<std::string_view> SoundDriver_Allegro::Start(const StringList &parm)
std::optional<std::string_view> SoundDriver_Allegro::Start(std::span<const std::string> parm)
{
if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, nullptr)) {
Debug(driver, 0, "allegro: install_allegro failed '{}'", allegro_error);

View File

@ -15,7 +15,7 @@
/** Implementation of the allegro sound driver. */
class SoundDriver_Allegro : public SoundDriver {
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;

View File

@ -43,7 +43,7 @@ static OSStatus audioCallback(void *, AudioUnitRenderActionFlags *, const AudioT
}
std::optional<std::string_view> SoundDriver_Cocoa::Start(const StringList &parm)
std::optional<std::string_view> SoundDriver_Cocoa::Start(std::span<const std::string> parm)
{
struct AURenderCallbackStruct callback;
AudioStreamBasicDescription requestedDesc;

View File

@ -14,7 +14,7 @@
class SoundDriver_Cocoa : public SoundDriver {
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;
std::string_view GetName() const override { return "cocoa"; }

View File

@ -15,7 +15,7 @@
/** Implementation of the null sound driver. */
class SoundDriver_Null : public SoundDriver {
public:
std::optional<std::string_view> Start(const StringList &) override { return std::nullopt; }
std::optional<std::string_view> Start(std::span<const std::string>) override { return std::nullopt; }
void Stop() override { }
std::string_view GetName() const override { return "null"; }

View File

@ -30,7 +30,7 @@ static void CDECL fill_sound_buffer(void *, Uint8 *stream, int len)
MxMixSamples(stream, len / 4);
}
std::optional<std::string_view> SoundDriver_SDL::Start(const StringList &parm)
std::optional<std::string_view> SoundDriver_SDL::Start(std::span<const std::string> parm)
{
SDL_AudioSpec spec;
SDL_AudioSpec spec_actual;

View File

@ -30,7 +30,7 @@ static void CDECL fill_sound_buffer(void *, Uint8 *stream, int len)
MxMixSamples(stream, len / 4);
}
std::optional<std::string_view> SoundDriver_SDL::Start(const StringList &parm)
std::optional<std::string_view> SoundDriver_SDL::Start(std::span<const std::string> parm)
{
SDL_AudioSpec spec;

View File

@ -15,7 +15,7 @@
/** Implementation of the SDL sound driver. */
class SoundDriver_SDL : public SoundDriver {
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;
std::string_view GetName() const override { return "sdl"; }

View File

@ -59,7 +59,7 @@ static DWORD WINAPI SoundThread(LPVOID)
return 0;
}
std::optional<std::string_view> SoundDriver_Win32::Start(const StringList &parm)
std::optional<std::string_view> SoundDriver_Win32::Start(std::span<const std::string> parm)
{
WAVEFORMATEX wfex;
wfex.wFormatTag = WAVE_FORMAT_PCM;

View File

@ -15,7 +15,7 @@
/** Implementation of the sound driver for Windows. */
class SoundDriver_Win32 : public SoundDriver {
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;
std::string_view GetName() const override { return "win32"; }

View File

@ -138,7 +138,7 @@ static HRESULT CreateXAudio(API_XAudio2Create xAudio2Create)
* @return An error message if unsuccessful, or std::nullopt otherwise.
*
*/
std::optional<std::string_view> SoundDriver_XAudio2::Start(const StringList &parm)
std::optional<std::string_view> SoundDriver_XAudio2::Start(std::span<const std::string> parm)
{
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);

View File

@ -15,7 +15,7 @@
/** Implementation of the XAudio2 sound driver. */
class SoundDriver_XAudio2 : public SoundDriver {
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;
std::string_view GetName() const override { return "xaudio2"; }

View File

@ -420,7 +420,7 @@ bool VideoDriver_Allegro::PollEvent()
*/
int _allegro_instance_count = 0;
std::optional<std::string_view> VideoDriver_Allegro::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_Allegro::Start(std::span<const std::string> parm)
{
if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, nullptr)) {
Debug(driver, 0, "allegro: install_allegro failed '{}'", allegro_error);
@ -448,7 +448,7 @@ std::optional<std::string_view> VideoDriver_Allegro::Start(const StringList &par
MarkWholeScreenDirty();
set_close_button_callback(HandleExitGameRequest);
this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
this->is_game_threaded = !GetDriverParamBool(parm, "no_threads") && !GetDriverParamBool(parm, "no_thread");
return std::nullopt;
}

View File

@ -15,7 +15,7 @@
/** The allegro video driver. */
class VideoDriver_Allegro : public VideoDriver {
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;

View File

@ -25,7 +25,7 @@ class VideoDriver_CocoaOpenGL : public VideoDriver_Cocoa {
public:
VideoDriver_CocoaOpenGL() : VideoDriver_Cocoa(true), gl_context(nullptr), anim_buffer(nullptr), driver_info(this->GetName()) {}
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;
bool HasEfficient8Bpp() const override { return true; }

View File

@ -185,7 +185,7 @@ static bool _allowSoftware;
static FVideoDriver_CocoaOpenGL iFVideoDriver_CocoaOpenGL;
std::optional<std::string_view> VideoDriver_CocoaOpenGL::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_CocoaOpenGL::Start(std::span<const std::string> parm)
{
auto err = this->Initialize();
if (err) return err;

View File

@ -109,7 +109,7 @@ public:
VideoDriver_CocoaQuartz();
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;
/** Return driver name */

View File

@ -589,7 +589,7 @@ VideoDriver_CocoaQuartz::VideoDriver_CocoaQuartz()
this->cgcontext = nullptr;
}
std::optional<std::string_view> VideoDriver_CocoaQuartz::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_CocoaQuartz::Start(std::span<const std::string> parm)
{
auto err = this->Initialize();
if (err) return err;
@ -613,7 +613,7 @@ std::optional<std::string_view> VideoDriver_CocoaQuartz::Start(const StringList
this->GameSizeChanged();
this->UpdateVideoModes();
this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
this->is_game_threaded = !GetDriverParamBool(parm, "no_threads") && !GetDriverParamBool(parm, "no_thread");
return std::nullopt;

View File

@ -103,7 +103,7 @@ extern bool SafeLoad(const std::string &filename, SaveLoadOperation fop, Detaile
static FVideoDriver_Dedicated iFVideoDriver_Dedicated;
std::optional<std::string_view> VideoDriver_Dedicated::Start(const StringList &)
std::optional<std::string_view> VideoDriver_Dedicated::Start(std::span<const std::string>)
{
this->UpdateAutoResolution();

View File

@ -15,7 +15,7 @@
/** The dedicated server video driver. */
class VideoDriver_Dedicated : public VideoDriver {
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;

View File

@ -19,7 +19,7 @@
/** Factory for the null video driver. */
static FVideoDriver_Null iFVideoDriver_Null;
std::optional<std::string_view> VideoDriver_Null::Start(const StringList &parm)
std::optional<std::string_view> VideoDriver_Null::Start(std::span<const std::string> parm)
{
#ifdef _MSC_VER
/* Disable the MSVC assertion message box. */

View File

@ -18,7 +18,7 @@ private:
uint ticks; ///< Amount of ticks to run.
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;

View File

@ -53,9 +53,9 @@ bool VideoDriver_SDL_OpenGL::CreateMainWindow(uint w, uint h, uint flags)
return this->VideoDriver_SDL_Base::CreateMainWindow(w, h, flags | SDL_WINDOW_OPENGL);
}
std::optional<std::string_view> VideoDriver_SDL_OpenGL::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_SDL_OpenGL::Start(std::span<const std::string> parm)
{
auto error = VideoDriver_SDL_Base::Start(param);
auto error = VideoDriver_SDL_Base::Start(parm);
if (error) return error;
error = this->AllocateContext();

View File

@ -14,7 +14,7 @@ class VideoDriver_SDL_OpenGL : public VideoDriver_SDL_Base {
public:
VideoDriver_SDL_OpenGL() : VideoDriver_SDL_Base(true), gl_context(nullptr), anim_buffer(nullptr) {}
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;

View File

@ -540,7 +540,7 @@ std::optional<std::string_view> VideoDriver_SDL_Base::Initialize()
return std::nullopt;
}
std::optional<std::string_view> VideoDriver_SDL_Base::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_SDL_Base::Start(std::span<const std::string> parm)
{
if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
@ -548,7 +548,7 @@ std::optional<std::string_view> VideoDriver_SDL_Base::Start(const StringList &pa
if (error) return error;
#ifdef SDL_HINT_MOUSE_AUTO_CAPTURE
if (GetDriverParamBool(param, "no_mouse_capture")) {
if (GetDriverParamBool(parm, "no_mouse_capture")) {
/* By default SDL captures the mouse, while a button is pressed.
* This is annoying during debugging, when OpenTTD is suspended while the button was pressed.
*/
@ -560,7 +560,7 @@ std::optional<std::string_view> VideoDriver_SDL_Base::Start(const StringList &pa
SDL_SetHint(SDL_HINT_APP_NAME, "OpenTTD");
#endif
this->startup_display = FindStartupDisplay(GetDriverParamInt(param, "display", -1));
this->startup_display = FindStartupDisplay(GetDriverParamInt(parm, "display", -1));
if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height, false)) {
return SDL_GetError();
@ -582,7 +582,7 @@ std::optional<std::string_view> VideoDriver_SDL_Base::Start(const StringList &pa
#ifdef __EMSCRIPTEN__
this->is_game_threaded = false;
#else
this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
this->is_game_threaded = !GetDriverParamBool(parm, "no_threads") && !GetDriverParamBool(parm, "no_thread");
#endif
return std::nullopt;

View File

@ -19,7 +19,7 @@ class VideoDriver_SDL_Base : public VideoDriver {
public:
VideoDriver_SDL_Base(bool uses_hardware_acceleration = false) : VideoDriver(uses_hardware_acceleration), sdl_window(nullptr), buffer_locked(false) {}
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;

View File

@ -582,7 +582,7 @@ bool VideoDriver_SDL::PollEvent()
return true;
}
std::optional<std::string_view> VideoDriver_SDL::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_SDL::Start(std::span<const std::string> parm)
{
char buf[30];
_use_hwpalette = GetDriverParamInt(param, "hw_palette", 2);

View File

@ -15,7 +15,7 @@
/** The SDL video driver. */
class VideoDriver_SDL : public VideoDriver {
public:
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;

View File

@ -1030,7 +1030,7 @@ void VideoDriver_Win32Base::UnlockVideoBuffer()
static FVideoDriver_Win32GDI iFVideoDriver_Win32GDI;
std::optional<std::string_view> VideoDriver_Win32GDI::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_Win32GDI::Start(std::span<const std::string> parm)
{
if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
@ -1042,7 +1042,7 @@ std::optional<std::string_view> VideoDriver_Win32GDI::Start(const StringList &pa
MarkWholeScreenDirty();
this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
this->is_game_threaded = !GetDriverParamBool(parm, "no_threads") && !GetDriverParamBool(parm, "no_thread");
return std::nullopt;
}
@ -1320,7 +1320,7 @@ static void LoadWGLExtensions()
static FVideoDriver_Win32OpenGL iFVideoDriver_Win32OpenGL;
std::optional<std::string_view> VideoDriver_Win32OpenGL::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_Win32OpenGL::Start(std::span<const std::string> parm)
{
if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
@ -1356,7 +1356,7 @@ std::optional<std::string_view> VideoDriver_Win32OpenGL::Start(const StringList
MarkWholeScreenDirty();
this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
this->is_game_threaded = !GetDriverParamBool(parm, "no_threads") && !GetDriverParamBool(parm, "no_thread");
return std::nullopt;
}

View File

@ -79,7 +79,7 @@ class VideoDriver_Win32GDI : public VideoDriver_Win32Base {
public:
VideoDriver_Win32GDI() : dib_sect(nullptr), gdi_palette(nullptr), buffer_bits(nullptr) {}
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;
@ -120,7 +120,7 @@ class VideoDriver_Win32OpenGL : public VideoDriver_Win32Base {
public:
VideoDriver_Win32OpenGL() : VideoDriver_Win32Base(true), dc(nullptr), gl_rc(nullptr), anim_buffer(nullptr), driver_info(this->GetName()) {}
std::optional<std::string_view> Start(const StringList &param) override;
std::optional<std::string_view> Start(std::span<const std::string> parm) override;
void Stop() override;