From f7a33bbeb0a5b62ed379a731ef5e00b9f6326704 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kapu=C5=9Bci=C5=84ski?= Date: Wed, 9 Aug 2023 17:58:48 +0200 Subject: [PATCH] Replaced boost::optional and boost::regex with STL implementations Changes based on https://github.com/osiox/colobot/commit/f0f6f61cabbc9269c83d74974bb24c2933b4bde4 --- src/common/config_file.cpp | 4 ++-- src/common/font_loader.cpp | 1 - src/common/regex_utils.cpp | 10 ++++---- src/common/regex_utils.h | 7 +++--- src/common/resources/resourcemanager.cpp | 4 ++-- src/common/restext.cpp | 1 - src/level/parser/parser.cpp | 8 +++---- src/object/auto/autofactory.cpp | 9 +++----- .../implementation/program_storage_impl.cpp | 23 ++++++++++--------- src/object/subclass/exchange_post.cpp | 4 ++-- src/object/subclass/exchange_post.h | 5 ++-- src/script/script.h | 7 +++--- src/script/scriptfunc.cpp | 2 +- 13 files changed, 39 insertions(+), 46 deletions(-) diff --git a/src/common/config_file.cpp b/src/common/config_file.cpp index 0376ad2e..891054b8 100644 --- a/src/common/config_file.cpp +++ b/src/common/config_file.cpp @@ -27,11 +27,11 @@ #include "common/system/system.h" +#include + #include #include #include -#include -#include namespace bp = boost::property_tree; diff --git a/src/common/font_loader.cpp b/src/common/font_loader.cpp index b2bc0cee..8c587b17 100644 --- a/src/common/font_loader.cpp +++ b/src/common/font_loader.cpp @@ -33,7 +33,6 @@ #include #include #include -#include namespace bp = boost::property_tree; diff --git a/src/common/regex_utils.cpp b/src/common/regex_utils.cpp index 7949657e..0c2d39f1 100644 --- a/src/common/regex_utils.cpp +++ b/src/common/regex_utils.cpp @@ -26,16 +26,16 @@ static std::string FormatAssertRegexMatchError(const std::string& text, } RegexUtils::CAssertRegexMatchError::CAssertRegexMatchError( - const std::string& text, const std::string& pattern) NOEXCEPT + const std::string& text, const std::string& pattern) noexcept : std::runtime_error(FormatAssertRegexMatchError(text, pattern)) { } -boost::smatch RegexUtils::AssertRegexMatch(const std::string& text, const std::string& pattern) +std::smatch RegexUtils::AssertRegexMatch(const std::string& text, const std::string& pattern) { - boost::regex regex(pattern); - boost::smatch matches; - bool ok = boost::regex_match(text, matches, regex); + std::regex regex(pattern); + std::smatch matches; + bool ok = std::regex_match(text, matches, regex); if (!ok) throw CAssertRegexMatchError(text, pattern); diff --git a/src/common/regex_utils.h b/src/common/regex_utils.h index a095eee6..6822ce9d 100644 --- a/src/common/regex_utils.h +++ b/src/common/regex_utils.h @@ -20,8 +20,7 @@ #pragma once #include - -#include +#include namespace RegexUtils { @@ -30,10 +29,10 @@ class CAssertRegexMatchError : public std::runtime_error { public: explicit CAssertRegexMatchError(const std::string& text, - const std::string& pattern) NOEXCEPT; + const std::string& pattern) noexcept; }; //! Match string with regex and return list of matches; throw exception on mismatch -boost::smatch AssertRegexMatch(const std::string& text, const std::string& pattern); +std::smatch AssertRegexMatch(const std::string& text, const std::string& pattern); } // namespace RegexUtils diff --git a/src/common/resources/resourcemanager.cpp b/src/common/resources/resourcemanager.cpp index ba46f96f..89708906 100644 --- a/src/common/resources/resourcemanager.cpp +++ b/src/common/resources/resourcemanager.cpp @@ -30,7 +30,7 @@ #include -#include +#include CResourceManager::CResourceManager(const char *argv0) @@ -57,7 +57,7 @@ CResourceManager::~CResourceManager() std::string CResourceManager::CleanPath(const std::filesystem::path& path) { - return boost::regex_replace(path.generic_u8string(), boost::regex("(.*)/\\.\\./"), ""); + return std::regex_replace(path.generic_u8string(), std::regex("(.*)/\\.\\./"), ""); } diff --git a/src/common/restext.cpp b/src/common/restext.cpp index c128fcae..92301b23 100644 --- a/src/common/restext.cpp +++ b/src/common/restext.cpp @@ -32,7 +32,6 @@ #include "object/object_type.h" #include -#include #include diff --git a/src/level/parser/parser.cpp b/src/level/parser/parser.cpp index fa7f445f..e6ed61c3 100644 --- a/src/level/parser/parser.cpp +++ b/src/level/parser/parser.cpp @@ -36,11 +36,11 @@ #include #include #include +#include #include #include #include -#include CLevelParser::CLevelParser() { @@ -174,9 +174,9 @@ void CLevelParser::Load() // ignore comments size_t pos = 0; std::string linesuffix = line; - boost::regex commentRegex{ R"(("[^"]*")|('[^']*')|(//.*$))" }; - boost::smatch matches; - while (boost::regex_search(linesuffix, matches, commentRegex)) + std::regex commentRegex{ R"(("[^"]*")|('[^']*')|(//.*$))" }; + std::smatch matches; + while (std::regex_search(linesuffix, matches, commentRegex)) { if (matches[3].matched) { diff --git a/src/object/auto/autofactory.cpp b/src/object/auto/autofactory.cpp index 3fc60d12..6f3f1443 100644 --- a/src/object/auto/autofactory.cpp +++ b/src/object/auto/autofactory.cpp @@ -47,10 +47,7 @@ #include "ui/controls/interface.h" #include "ui/controls/window.h" - -#include - - +#include // Object's constructor. @@ -410,12 +407,12 @@ bool CAutoFactory::EventProcess(const Event &event) { Program* program = dynamic_cast(*vehicle).AddProgram(); - if (boost::regex_match(m_program, boost::regex("[A-Za-z0-9_]+"))) // Public function name? + if (std::regex_match(m_program, std::regex("[A-Za-z0-9_]+"))) // Public function name? { std::string code = "extern void object::Start_"+m_program+"()\n{\n\t\n\t//Automatically generated by object.factory()\n\t"+m_program+"();\n\t\n}\n"; program->script->SendScript(code.c_str()); } - else if (boost::regex_match(m_program, boost::regex(".*\\.txt"))) // File name (with .txt extension)? + else if (std::regex_match(m_program, std::regex(".*\\.txt"))) // File name (with .txt extension)? { program->script->ReadScript(m_program.c_str()); } diff --git a/src/object/implementation/program_storage_impl.cpp b/src/object/implementation/program_storage_impl.cpp index 2fafd98e..c88647d2 100644 --- a/src/object/implementation/program_storage_impl.cpp +++ b/src/object/implementation/program_storage_impl.cpp @@ -46,10 +46,11 @@ #include "ui/controls/edit.h" +#include + #include #include -#include -#include +#include CProgramStorageObjectImpl::CProgramStorageObjectImpl(ObjectInterfaceTypes& types, CObject* object) : CProgramStorageObject(types), @@ -233,11 +234,11 @@ void CProgramStorageObjectImpl::SaveAllUserPrograms(const std::string& userSourc std::string dir = userSource.substr(0, userSource.find_last_of("/")); std::string file = userSource.substr(userSource.find_last_of("/")+1) + StrUtils::Format("%.3d([0-9]{3})\\.txt", m_programStorageIndex); - boost::regex regex(file); + std::regex regex(file); for (const std::string& filename : CResourceManager::ListFiles(dir)) { - boost::smatch matches; - if (boost::regex_match(filename, matches, regex)) + std::smatch matches; + if (std::regex_match(filename, matches, regex)) { unsigned int id = boost::lexical_cast(matches[1]); if (id >= m_program.size() || !m_program[id]->filename.empty()) @@ -296,11 +297,11 @@ void CProgramStorageObjectImpl::LoadAllProgramsForLevel(CLevelParserLine* levelS std::string dir = userSource.substr(0, userSource.find_last_of("/")); std::string file = userSource.substr(userSource.find_last_of("/")+1) + StrUtils::Format("%.3d([0-9]{3})\\.txt", m_programStorageIndex); - boost::regex regex(file); + std::regex regex(file); for (const std::string& filename : CResourceManager::ListFiles(dir)) { - boost::smatch matches; - if (boost::regex_match(filename, matches, regex)) + std::smatch matches; + if (std::regex_match(filename, matches, regex)) { unsigned int i = boost::lexical_cast(matches[1]); Program* program = GetOrAddProgram(i); @@ -341,11 +342,11 @@ void CProgramStorageObjectImpl::SaveAllProgramsForSavedScene(CLevelParserLine* l levelSourceLine->AddParam("scriptRunnable" + StrUtils::ToString(i+1), std::make_unique(m_program[i]->runnable)); } - boost::regex regex(StrUtils::Format("prog%.3d([0-9]{3})\\.txt", m_programStorageIndex)); + std::regex regex(StrUtils::Format("prog%.3d([0-9]{3})\\.txt", m_programStorageIndex)); for (const std::string& filename : CResourceManager::ListFiles(levelSource)) { - boost::smatch matches; - if (boost::regex_match(filename, matches, regex)) + std::smatch matches; + if (std::regex_match(filename, matches, regex)) { unsigned int id = boost::lexical_cast(matches[1]); if (id >= m_program.size() || !m_program[id]->filename.empty()) diff --git a/src/object/subclass/exchange_post.cpp b/src/object/subclass/exchange_post.cpp index 596ce4f5..ce981841 100644 --- a/src/object/subclass/exchange_post.cpp +++ b/src/object/subclass/exchange_post.cpp @@ -144,7 +144,7 @@ const std::vector& CExchangePost::GetInfoList() return m_infoList; } -boost::optional CExchangePost::GetInfoValue(const std::string& name) +std::optional CExchangePost::GetInfoValue(const std::string& name) { for (auto& info : m_infoList) { @@ -153,7 +153,7 @@ boost::optional CExchangePost::GetInfoValue(const std::string& name) return info.value; } } - return boost::none; + return std::nullopt; } bool CExchangePost::HasInfo(const std::string& name) diff --git a/src/object/subclass/exchange_post.h b/src/object/subclass/exchange_post.h index 7658d69f..6afb1b29 100644 --- a/src/object/subclass/exchange_post.h +++ b/src/object/subclass/exchange_post.h @@ -24,11 +24,10 @@ #include "object/auto/auto.h" +#include #include #include -#include - struct ExchangePostInfo { std::string name; //!< name of the information @@ -57,7 +56,7 @@ public: bool SetInfo(const std::string& name, float value); const std::vector& GetInfoList(); - boost::optional GetInfoValue(const std::string& name); + std::optional GetInfoValue(const std::string& name); bool HasInfo(const std::string& name); bool DeleteInfo(const std::string& name); diff --git a/src/script/script.h b/src/script/script.h index 59dffc53..1f78d4c3 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -26,11 +26,10 @@ #include "CBot/CBot.h" -#include #include +#include +#include #include -#include - class COldObject; class CTaskExecutorObject; @@ -128,5 +127,5 @@ protected: CBot::CBotError m_error = CBot::CBotNoErr; // error (0=ok) int m_cursor1 = 0; int m_cursor2 = 0; - boost::optional m_returnValue = boost::none; + std::optional m_returnValue = std::nullopt; }; diff --git a/src/script/scriptfunc.cpp b/src/script/scriptfunc.cpp index ca0e662b..4a46fdbc 100644 --- a/src/script/scriptfunc.cpp +++ b/src/script/scriptfunc.cpp @@ -2336,7 +2336,7 @@ bool CScriptFunctions::rReceive(CBotVar* var, CBotVar* result, int& exception, v } if ( !WaitForForegroundTask(script, result, exception) ) return false; // not finished - if ( script->m_returnValue == boost::none ) + if ( !script->m_returnValue.has_value() ) { result->SetValFloat(nanf("")); }