Skip to content

Commit

Permalink
Merge pull request #13710 from skdltmxn/refactor/string_view
Browse files Browse the repository at this point in the history
Refactor/string view
  • Loading branch information
Gymnasiast committed Jan 10, 2021
2 parents 011588b + e1cfa86 commit 1da6ae6
Show file tree
Hide file tree
Showing 60 changed files with 198 additions and 204 deletions.
2 changes: 1 addition & 1 deletion src/openrct2-ui/WindowManager.cpp
Expand Up @@ -211,7 +211,7 @@ class WindowManager final : public IWindowManager
return window_error_open(title, message, args);
}

rct_window* ShowError(const std::string_view& title, const std::string_view& message) override
rct_window* ShowError(std::string_view title, std::string_view message) override
{
return window_error_open(title, message);
}
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2-ui/scripting/CustomListView.h
Expand Up @@ -56,7 +56,7 @@ namespace OpenRCT2::Ui::Windows
std::vector<std::string> Cells;

ListViewItem() = default;
explicit ListViewItem(const std::string_view& text)
explicit ListViewItem(std::string_view text)
{
Cells.emplace_back(text);
}
Expand Down
10 changes: 5 additions & 5 deletions src/openrct2-ui/scripting/CustomWindow.cpp
Expand Up @@ -1088,7 +1088,7 @@ namespace OpenRCT2::Ui::Windows
return {};
}

void UpdateWindowTitle(rct_window* w, const std::string_view& value)
void UpdateWindowTitle(rct_window* w, std::string_view value)
{
if (w->custom_info != nullptr)
{
Expand All @@ -1097,7 +1097,7 @@ namespace OpenRCT2::Ui::Windows
}
}

void UpdateWidgetText(rct_window* w, rct_widgetindex widgetIndex, const std::string_view& value)
void UpdateWidgetText(rct_window* w, rct_widgetindex widgetIndex, std::string_view value)
{
if (w->custom_info != nullptr)
{
Expand Down Expand Up @@ -1242,7 +1242,7 @@ namespace OpenRCT2::Ui::Windows
return -1;
}

rct_window* FindCustomWindowByClassification(const std::string_view& classification)
rct_window* FindCustomWindowByClassification(std::string_view classification)
{
for (auto w : g_window_list)
{
Expand All @@ -1258,7 +1258,7 @@ namespace OpenRCT2::Ui::Windows
return nullptr;
}

std::optional<rct_widgetindex> FindWidgetIndexByName(rct_window* w, const std::string_view& name)
std::optional<rct_widgetindex> FindWidgetIndexByName(rct_window* w, std::string_view name)
{
if (w->custom_info != nullptr)
{
Expand Down Expand Up @@ -1292,7 +1292,7 @@ namespace OpenRCT2::Ui::Windows
return {};
}

void SetWidgetName(rct_window* w, rct_widgetindex widgetIndex, const std::string_view& name)
void SetWidgetName(rct_window* w, rct_widgetindex widgetIndex, std::string_view name)
{
if (w->custom_info != nullptr)
{
Expand Down
10 changes: 5 additions & 5 deletions src/openrct2-ui/scripting/CustomWindow.h
Expand Up @@ -21,18 +21,18 @@ namespace OpenRCT2::Ui::Windows
class CustomListView;

std::string GetWindowTitle(rct_window* w);
void UpdateWindowTitle(rct_window* w, const std::string_view& value);
void UpdateWidgetText(rct_window* w, rct_widgetindex widget, const std::string_view& string_view);
void UpdateWindowTitle(rct_window* w, std::string_view value);
void UpdateWidgetText(rct_window* w, rct_widgetindex widget, std::string_view string_view);
void UpdateWidgetItems(rct_window* w, rct_widgetindex widgetIndex, const std::vector<std::string>& items);
void UpdateWidgetColour(rct_window* w, rct_widgetindex widgetIndex, colour_t colour);
void UpdateWidgetSelectedIndex(rct_window* w, rct_widgetindex widgetIndex, int32_t selectedIndex);
std::vector<std::string> GetWidgetItems(rct_window* w, rct_widgetindex widgetIndex);
colour_t GetWidgetColour(rct_window* w, rct_widgetindex widgetIndex);
int32_t GetWidgetSelectedIndex(rct_window* w, rct_widgetindex widgetIndex);
rct_window* FindCustomWindowByClassification(const std::string_view& classification);
std::optional<rct_widgetindex> FindWidgetIndexByName(rct_window* w, const std::string_view& name);
rct_window* FindCustomWindowByClassification(std::string_view classification);
std::optional<rct_widgetindex> FindWidgetIndexByName(rct_window* w, std::string_view name);
std::string GetWidgetName(rct_window* w, rct_widgetindex widgetIndex);
void SetWidgetName(rct_window* w, rct_widgetindex widgetIndex, const std::string_view& name);
void SetWidgetName(rct_window* w, rct_widgetindex widgetIndex, std::string_view name);
CustomListView* GetCustomListView(rct_window* w, rct_widgetindex widgetIndex);
} // namespace OpenRCT2::Ui::Windows

Expand Down
2 changes: 1 addition & 1 deletion src/openrct2-ui/scripting/ScUi.hpp
Expand Up @@ -198,7 +198,7 @@ namespace OpenRCT2::Scripting
auto callback = desc["callback"];
window_text_input_open(
title, description, initialValue, std::max(0, maxLength),
[this, plugin, callback](const std::string_view& value) {
[this, plugin, callback](std::string_view value) {
auto dukValue = ToDuk(_scriptEngine.GetContext(), value);
_scriptEngine.ExecutePluginCall(plugin, callback, { dukValue }, false);
},
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2-ui/windows/Error.cpp
Expand Up @@ -54,7 +54,7 @@ rct_window* window_error_open(rct_string_id title, rct_string_id message, const
return window_error_open(titlez, messagez);
}

rct_window* window_error_open(const std::string_view& title, const std::string_view& message)
rct_window* window_error_open(std::string_view title, std::string_view message)
{
int32_t numLines, fontHeight, width, height, maxY;
rct_window* w;
Expand Down
6 changes: 3 additions & 3 deletions src/openrct2-ui/windows/TextInput.cpp
Expand Up @@ -73,7 +73,7 @@ static int32_t _maxInputLength;

static std::string _title;
static std::string _description;
static std::function<void(const std::string_view&)> _callback;
static std::function<void(std::string_view)> _callback;
static std::function<void()> _cancelCallback;

void window_text_input_open(
Expand Down Expand Up @@ -157,8 +157,8 @@ void window_text_input_raw_open(
}

void window_text_input_open(
const std::string_view& title, const std::string_view& description, const std::string_view& initialValue, size_t maxLength,
std::function<void(const std::string_view&)> callback, std::function<void()> cancelCallback)
std::string_view title, std::string_view description, std::string_view initialValue, size_t maxLength,
std::function<void(std::string_view)> callback, std::function<void()> cancelCallback)
{
_title = title;
_description = description;
Expand Down
6 changes: 3 additions & 3 deletions src/openrct2-ui/windows/Window.h
Expand Up @@ -108,7 +108,7 @@ void window_title_command_editor_open(struct TitleSequence* sequence, int32_t co
rct_window* window_scenarioselect_open(scenarioselect_callback callback, bool titleEditor);

rct_window* window_error_open(rct_string_id title, rct_string_id message, const class Formatter& formatter);
rct_window* window_error_open(const std::string_view& title, const std::string_view& message);
rct_window* window_error_open(std::string_view title, std::string_view message);
struct TrackDesign;
rct_window* window_loadsave_open(int32_t type, const char* defaultName, loadsave_callback callback, TrackDesign* t6Exporter);
rct_window* window_track_place_open(const struct track_design_file_ref* tdFileRef);
Expand Down Expand Up @@ -161,8 +161,8 @@ void window_text_input_raw_open(
const_utf8string existing_text, int32_t maxLength);

void window_text_input_open(
const std::string_view& title, const std::string_view& description, const std::string_view& initialValue, size_t maxLength,
std::function<void(const std::string_view&)> okCallback, std::function<void()> cancelCallback);
std::string_view title, std::string_view description, std::string_view initialValue, size_t maxLength,
std::function<void(std::string_view)> okCallback, std::function<void()> cancelCallback);

rct_window* window_object_load_error_open(utf8* path, size_t numMissingObjects, const rct_object_entry* missingObjects);

Expand Down
10 changes: 5 additions & 5 deletions src/openrct2/actions/GameAction.h
Expand Up @@ -158,15 +158,15 @@ class GameActionParameterVisitor
public:
virtual ~GameActionParameterVisitor() = default;

virtual void Visit(const std::string_view& name, bool& param)
virtual void Visit(std::string_view name, bool& param)
{
}

virtual void Visit(const std::string_view& name, int32_t& param)
virtual void Visit(std::string_view name, int32_t& param)
{
}

virtual void Visit(const std::string_view& name, std::string& param)
virtual void Visit(std::string_view name, std::string& param)
{
}

Expand All @@ -191,15 +191,15 @@ class GameActionParameterVisitor
Visit("direction", param.direction);
}

template<typename T> void Visit(const std::string_view& name, T& param)
template<typename T> void Visit(std::string_view name, T& param)
{
static_assert(std::is_arithmetic_v<T> || std::is_enum_v<T>, "Not an arithmetic type");
auto value = static_cast<int32_t>(param);
Visit(name, value);
param = static_cast<T>(value);
}

template<typename T, size_t _TypeID> void Visit(const std::string_view& name, NetworkObjectId_t<T, _TypeID>& param)
template<typename T, size_t _TypeID> void Visit(std::string_view name, NetworkObjectId_t<T, _TypeID>& param)
{
Visit(name, param.id);
}
Expand Down
11 changes: 5 additions & 6 deletions src/openrct2/core/Crypt.CNG.cpp
Expand Up @@ -29,7 +29,7 @@ constexpr bool NT_SUCCESS(NTSTATUS status) {return status >= 0;}

using namespace Crypt;

static void CngThrowOnBadStatus(const std::string_view& name, NTSTATUS status)
static void CngThrowOnBadStatus(std::string_view name, NTSTATUS status)
{
if (!NT_SUCCESS(status))
{
Expand Down Expand Up @@ -409,7 +409,7 @@ class CngRsaKey final : public RsaKey
_hAlg = {};
}

void SetPrivate(const std::string_view& pem) override
void SetPrivate(std::string_view pem) override
{
auto der = ReadPEM(pem, SZ_PRIVATE_BEGIN_TOKEN, SZ_PRIVATE_END_TOKEN);
DerReader derReader(der);
Expand All @@ -427,7 +427,7 @@ class CngRsaKey final : public RsaKey
ImportKey(params);
}

void SetPublic(const std::string_view& pem) override
void SetPublic(std::string_view pem) override
{
auto der = ReadPEM(pem, SZ_PUBLIC_BEGIN_TOKEN, SZ_PUBLIC_END_TOKEN);
DerReader derReader(der);
Expand Down Expand Up @@ -531,8 +531,7 @@ class CngRsaKey final : public RsaKey
return RsaKeyParams::FromBlob(blob);
}

static std::vector<uint8_t> ReadPEM(
const std::string_view& pem, const std::string_view& beginToken, const std::string_view& endToken)
static std::vector<uint8_t> ReadPEM(std::string_view pem, std::string_view beginToken, std::string_view endToken)
{
auto beginPos = pem.find(beginToken);
auto endPos = pem.find(endToken);
Expand Down Expand Up @@ -586,7 +585,7 @@ class CngRsaKey final : public RsaKey
return result;
}

static std::vector<uint8_t> DecodeBase64(const std::string_view& input)
static std::vector<uint8_t> DecodeBase64(std::string_view input)
{
DWORD cbBinary{};
if (!CryptStringToBinaryA(
Expand Down
8 changes: 4 additions & 4 deletions src/openrct2/core/Crypt.OpenSSL.cpp
Expand Up @@ -19,7 +19,7 @@

using namespace Crypt;

static void OpenSSLThrowOnBadStatus(const std::string_view& name, int status)
static void OpenSSLThrowOnBadStatus(std::string_view name, int status)
{
if (status != 1)
{
Expand Down Expand Up @@ -156,12 +156,12 @@ class OpenSSLRsaKey final : public RsaKey
}
}

void SetPrivate(const std::string_view& pem) override
void SetPrivate(std::string_view pem) override
{
SetKey(pem, true);
}

void SetPublic(const std::string_view& pem) override
void SetPublic(std::string_view pem) override
{
SetKey(pem, false);
}
Expand All @@ -179,7 +179,7 @@ class OpenSSLRsaKey final : public RsaKey
private:
EVP_PKEY* _evpKey{};

void SetKey(const std::string_view& pem, bool isPrivate)
void SetKey(std::string_view pem, bool isPrivate)
{
// Read PEM data via BIO buffer
// HACK first parameter is not const on MINGW for some reason
Expand Down
4 changes: 2 additions & 2 deletions src/openrct2/core/Crypt.h
Expand Up @@ -32,8 +32,8 @@ namespace Crypt
public:
virtual ~RsaKey() = default;
virtual void Generate() = 0;
virtual void SetPrivate(const std::string_view& pem) = 0;
virtual void SetPublic(const std::string_view& pem) = 0;
virtual void SetPrivate(std::string_view pem) = 0;
virtual void SetPublic(std::string_view pem) = 0;
virtual std::string GetPrivate() = 0;
virtual std::string GetPublic() = 0;
};
Expand Down
27 changes: 13 additions & 14 deletions src/openrct2/core/File.cpp
Expand Up @@ -43,21 +43,20 @@ namespace File
return platform_file_move(srcPath.c_str(), dstPath.c_str());
}

std::vector<uint8_t> ReadAllBytes(const std::string_view& path)
std::vector<uint8_t> ReadAllBytes(std::string_view path)
{
std::vector<uint8_t> result;

#if defined(_WIN32) && !defined(__MINGW32__)
auto pathW = String::ToWideChar(std::string(path));
auto pathW = String::ToWideChar(path);
std::ifstream fs(pathW, std::ios::in | std::ios::binary);
#else
std::ifstream fs(std::string(path), std::ios::in | std::ios::binary);
#endif
if (!fs.is_open())
{
throw IOException("Unable to open " + std::string(path.data()));
throw IOException("Unable to open " + std::string(path));
}

std::vector<uint8_t> result;
fs.seekg(0, std::ios::end);
auto fsize = static_cast<size_t>(fs.tellg());
if (fsize > SIZE_MAX)
Expand All @@ -76,7 +75,7 @@ namespace File
return result;
}

std::string ReadAllText(const std::string_view& path)
std::string ReadAllText(std::string_view path)
{
auto bytes = ReadAllBytes(path);
// TODO skip BOM
Expand All @@ -85,13 +84,7 @@ namespace File
return result;
}

void WriteAllBytes(const std::string& path, const void* buffer, size_t length)
{
auto fs = OpenRCT2::FileStream(path, OpenRCT2::FILE_MODE_WRITE);
fs.Write(buffer, length);
}

std::vector<std::string> ReadAllLines(const std::string& path)
std::vector<std::string> ReadAllLines(std::string_view path)
{
std::vector<std::string> lines;
auto data = ReadAllBytes(path);
Expand Down Expand Up @@ -120,11 +113,17 @@ namespace File
return lines;
}

void WriteAllBytes(const std::string& path, const void* buffer, size_t length)
{
auto fs = OpenRCT2::FileStream(path, OpenRCT2::FILE_MODE_WRITE);
fs.Write(buffer, length);
}

uint64_t GetLastModified(const std::string& path)
{
uint64_t lastModified = 0;
#ifdef _WIN32
auto pathW = String::ToWideChar(path.c_str());
auto pathW = String::ToWideChar(path);
auto hFile = CreateFileW(pathW.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (hFile != INVALID_HANDLE_VALUE)
{
Expand Down
6 changes: 3 additions & 3 deletions src/openrct2/core/File.h
Expand Up @@ -21,9 +21,9 @@ namespace File
bool Copy(const std::string& srcPath, const std::string& dstPath, bool overwrite);
bool Delete(const std::string& path);
bool Move(const std::string& srcPath, const std::string& dstPath);
std::vector<uint8_t> ReadAllBytes(const std::string_view& path);
std::string ReadAllText(const std::string_view& path);
std::vector<uint8_t> ReadAllBytes(std::string_view path);
std::string ReadAllText(std::string_view path);
std::vector<std::string> ReadAllLines(std::string_view path);
void WriteAllBytes(const std::string& path, const void* buffer, size_t length);
std::vector<std::string> ReadAllLines(const std::string& path);
uint64_t GetLastModified(const std::string& path);
} // namespace File
10 changes: 5 additions & 5 deletions src/openrct2/core/Imaging.cpp
Expand Up @@ -251,7 +251,7 @@ namespace Imaging
}
}

IMAGE_FORMAT GetImageFormatFromPath(const std::string_view& path)
IMAGE_FORMAT GetImageFormatFromPath(std::string_view path)
{
if (String::EndsWith(path, ".png", true))
{
Expand Down Expand Up @@ -304,7 +304,7 @@ namespace Imaging
}
}

Image ReadFromFile(const std::string_view& path, IMAGE_FORMAT format)
Image ReadFromFile(std::string_view path, IMAGE_FORMAT format)
{
switch (format)
{
Expand All @@ -316,7 +316,7 @@ namespace Imaging
auto pathW = String::ToWideChar(path);
std::ifstream fs(pathW, std::ios::binary);
#else
std::ifstream fs(path.data(), std::ios::binary);
std::ifstream fs(std::string(path), std::ios::binary);
#endif
return ReadFromStream(fs, format);
}
Expand All @@ -329,7 +329,7 @@ namespace Imaging
return ReadFromStream(istream, format);
}

void WriteToFile(const std::string_view& path, const Image& image, IMAGE_FORMAT format)
void WriteToFile(std::string_view path, const Image& image, IMAGE_FORMAT format)
{
switch (format)
{
Expand All @@ -342,7 +342,7 @@ namespace Imaging
auto pathW = String::ToWideChar(path);
std::ofstream fs(pathW, std::ios::binary);
#else
std::ofstream fs(path.data(), std::ios::binary);
std::ofstream fs(std::string(path), std::ios::binary);
#endif
WritePng(fs, image);
break;
Expand Down

0 comments on commit 1da6ae6

Please sign in to comment.