From 1b69589302c2ac92c6befd2880a03b4b07c7f820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kapu=C5=9Bci=C5=84ski?= Date: Wed, 9 Aug 2023 19:32:59 +0200 Subject: [PATCH] Replaced boost::lexical_cast with other implementations Changes based on https://github.com/osiox/colobot/commit/f0f6f61cabbc9269c83d74974bb24c2933b4bde4 --- src/app/input.cpp | 10 ++++----- src/app/modman.cpp | 7 ++++--- src/common/config_file.h | 16 ++++++++++++-- src/common/event.cpp | 7 ++++--- src/graphics/model/model_mod.cpp | 2 +- src/graphics/model/model_txt.cpp | 21 +++++++++---------- src/level/parser/parser.cpp | 5 ++--- src/level/parser/parserexceptions.cpp | 6 +++--- src/level/parser/parserparam.cpp | 20 ++++++++++-------- src/level/parser/parserparam.h | 4 ++-- src/level/robotmain.cpp | 8 +++---- src/level/scoreboard.cpp | 2 +- .../implementation/program_storage_impl.cpp | 8 +++---- src/object/object_create_exception.h | 7 ++++--- src/object/old_object.cpp | 3 +-- src/object/subclass/exchange_post.cpp | 11 +++++----- 16 files changed, 73 insertions(+), 64 deletions(-) diff --git a/src/app/input.cpp b/src/app/input.cpp index 9bc292f7..5e540829 100644 --- a/src/app/input.cpp +++ b/src/app/input.cpp @@ -22,6 +22,7 @@ #include "common/config_file.h" #include "common/logger.h" #include "common/restext.h" +#include "common/stringutils.h" #include "graphics/engine/engine.h" @@ -30,7 +31,6 @@ #include "math/func.h" #include -#include #include CInput::CInput() @@ -355,8 +355,8 @@ void CInput::SaveKeyBindings() { JoyAxisBinding b = GetJoyAxisBinding(static_cast(i)); - GetConfigFile().SetIntProperty("Setup", "JoystickAxisBinding"+boost::lexical_cast(i), b.axis); - GetConfigFile().SetIntProperty("Setup", "JoystickAxisInvert"+boost::lexical_cast(i), b.invert); + GetConfigFile().SetIntProperty("Setup", "JoystickAxisBinding" + StrUtils::ToString(i), b.axis); + GetConfigFile().SetIntProperty("Setup", "JoystickAxisInvert" + StrUtils::ToString(i), b.invert); } GetConfigFile().SetFloatProperty("Setup", "JoystickDeadzone", GetJoystickDeadzone()); } @@ -387,11 +387,11 @@ void CInput::LoadKeyBindings() { JoyAxisBinding b; - if (!GetConfigFile().GetIntProperty("Setup", "JoystickAxisBinding"+boost::lexical_cast(i), b.axis)) + if (!GetConfigFile().GetIntProperty("Setup", "JoystickAxisBinding" + std::to_string(i), b.axis)) continue; int x = 0; - GetConfigFile().GetIntProperty("Setup", "JoystickAxisInvert"+boost::lexical_cast(i), x); // If doesn't exist, use default (0) + GetConfigFile().GetIntProperty("Setup", "JoystickAxisInvert" + std::to_string(i), x); // If doesn't exist, use default (0) b.invert = (x != 0); SetJoyAxisBinding(static_cast(i), b); diff --git a/src/app/modman.cpp b/src/app/modman.cpp index 31b2d25f..0a6f601f 100644 --- a/src/app/modman.cpp +++ b/src/app/modman.cpp @@ -26,6 +26,7 @@ #include "common/config_file.h" #include "common/logger.h" +#include "common/stringutils.h" #include "common/resources/resourcemanager.h" @@ -275,9 +276,9 @@ void CModManager::LoadModData(Mod& mod) } else if (line->GetParam("major")->IsDefined() && line->GetParam("minor")->IsDefined() && line->GetParam("patch")->IsDefined()) { - auto major = boost::lexical_cast(line->GetParam("major")->AsInt()); - auto minor = boost::lexical_cast(line->GetParam("minor")->AsInt()); - auto patch = boost::lexical_cast(line->GetParam("patch")->AsInt()); + auto major = StrUtils::ToString(line->GetParam("major")->AsInt()); + auto minor = StrUtils::ToString(line->GetParam("minor")->AsInt()); + auto patch = StrUtils::ToString(line->GetParam("patch")->AsInt()); data.version = boost::algorithm::join(std::vector{ major, minor, patch }, "."); } } diff --git a/src/common/config_file.h b/src/common/config_file.h index e9d6342f..ec8ed70c 100644 --- a/src/common/config_file.h +++ b/src/common/config_file.h @@ -29,7 +29,6 @@ #include "common/logger.h" #include -#include #include #include @@ -157,9 +156,22 @@ private: std::vector result; std::stringstream ss(s); std::string item; + while (std::getline(ss, item, ',')) { - result.push_back(boost::lexical_cast(item)); + if constexpr (std::is_same_v) + { + result.push_back(item); + } + else + { + std::stringstream stream(item); + + T value; + stream >> value; + + result.push_back(value); + } } return result; } diff --git a/src/common/event.cpp b/src/common/event.cpp index 76f1a671..8656c111 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -21,8 +21,9 @@ #include "common/event.h" #include "common/logger.h" +#include "common/stringutils.h" -#include +#include namespace { @@ -580,12 +581,12 @@ std::string ParseEventType(EventType eventType) { if(eventType >= EVENT_INTERFACE_KEY && eventType <= EVENT_INTERFACE_KEY_END) { - return "EVENT_INTERFACE_KEY"+boost::lexical_cast(eventType-EVENT_INTERFACE_KEY); + return "EVENT_INTERFACE_KEY" + StrUtils::ToString(eventType-EVENT_INTERFACE_KEY); } if(eventType >= EVENT_OBJECT_SHORTCUT && eventType <= EVENT_OBJECT_SHORTCUT_MAX) { - return "EVENT_OBJECT_SHORTCUT"+boost::lexical_cast(eventType-EVENT_OBJECT_SHORTCUT); + return "EVENT_OBJECT_SHORTCUT" + StrUtils::ToString(eventType-EVENT_OBJECT_SHORTCUT); } const char* stdEvent = EVENT_TYPE_TEXT[eventType]; diff --git a/src/graphics/model/model_mod.cpp b/src/graphics/model/model_mod.cpp index 4053be9e..612ebee7 100644 --- a/src/graphics/model/model_mod.cpp +++ b/src/graphics/model/model_mod.cpp @@ -26,8 +26,8 @@ #include "graphics/model/model_io_structs.h" #include -#include +#include #include using namespace IOUtils; diff --git a/src/graphics/model/model_txt.cpp b/src/graphics/model/model_txt.cpp index 7fdac273..e574c78e 100644 --- a/src/graphics/model/model_txt.cpp +++ b/src/graphics/model/model_txt.cpp @@ -26,7 +26,6 @@ #include "graphics/model/model_io_structs.h" #include -#include using namespace IOUtils; @@ -58,7 +57,7 @@ std::unique_ptr ReadTextModel(const std::filesystem::path& path) int version = 0; try { - version = boost::lexical_cast(ReadLineString(stream, "version")); + version = std::stoi(ReadLineString(stream, "version")); stream.seekg(std::ios_base::beg); } catch (const std::exception& e) @@ -73,7 +72,7 @@ std::unique_ptr ReadTextModel(const std::filesystem::path& path) else if (version == 3) ReadTextModelV3(*model, stream); else - throw CModelIOException(std::string("Unexpected version number: ") + boost::lexical_cast(version)); + throw CModelIOException(std::string("Unexpected version number: ") + std::to_string(version)); return model; } @@ -84,8 +83,8 @@ void ReadTextModelV1AndV2(CModel& model, std::istream& stream) try { - header.version = boost::lexical_cast(ReadLineString(stream, "version")); - header.totalTriangles = boost::lexical_cast(ReadLineString(stream, "total_triangles")); + header.version = std::stoi(ReadLineString(stream, "version")); + header.totalTriangles = std::stoi(ReadLineString(stream, "total_triangles")); } catch (const std::exception& e) { @@ -121,9 +120,9 @@ void ReadTextModelV1AndV2(CModel& model, std::istream& stream) t.variableTex2 = varTex2Ch == "Y"; if (header.version == 1) - t.lodLevel = static_cast(boost::lexical_cast(ReadLineString(stream, "lod_level"))); + t.lodLevel = static_cast(std::stoi(ReadLineString(stream, "lod_level"))); - t.state = boost::lexical_cast(ReadLineString(stream, "state")); + t.state = std::stoi(ReadLineString(stream, "state")); if (t.lodLevel == ModelLODLevel::Low || t.lodLevel == ModelLODLevel::Medium) @@ -186,11 +185,11 @@ void ReadTextModelV3(CModel& model, std::istream& stream) ModelHeaderV3 ReadTextHeader(std::istream& stream) { ModelHeaderV3 header; - header.version = boost::lexical_cast(ReadLineString(stream, "version")); - header.totalCrashSpheres = boost::lexical_cast(ReadLineString(stream, "total_crash_spheres")); + header.version = std::stoi(ReadLineString(stream, "version")); + header.totalCrashSpheres = std::stoi(ReadLineString(stream, "total_crash_spheres")); header.hasShadowSpot = ReadLineString(stream, "has_shadow_spot") == std::string("Y"); header.hasCameraCollisionSphere = ReadLineString(stream, "has_camera_collision_sphere") == std::string("Y"); - header.totalMeshes = boost::lexical_cast(ReadLineString(stream, "total_meshes")); + header.totalMeshes = std::stoi(ReadLineString(stream, "total_meshes")); return header; } @@ -203,7 +202,7 @@ std::unique_ptr ReadTextMesh(std::istream& stream) mesh->SetScale(ParseVector(ReadLineString(stream, "scale"))); mesh->SetParent(ReadLineString(stream, "parent")); - int totalTriangles = boost::lexical_cast(ReadLineString(stream, "total_triangles")); + int totalTriangles = std::stoi(ReadLineString(stream, "total_triangles")); for (int i = 0; i < totalTriangles; ++i) { diff --git a/src/level/parser/parser.cpp b/src/level/parser/parser.cpp index e6ed61c3..397eec6b 100644 --- a/src/level/parser/parser.cpp +++ b/src/level/parser/parser.cpp @@ -40,7 +40,6 @@ #include #include -#include CLevelParser::CLevelParser() { @@ -255,13 +254,13 @@ void CLevelParser::Load() { pos = line.find_first_of("\"", 1); if (pos == std::string::npos) - throw CLevelParserException("Unclosed \" in " + m_filename + ":" + boost::lexical_cast(lineNumber)); + throw CLevelParserException("Unclosed \" in " + m_filename + ":" + StrUtils::ToString(lineNumber)); } else if (line[0] == '\'') { pos = line.find_first_of("'", 1); if (pos == std::string::npos) - throw CLevelParserException("Unclosed ' in " + m_filename + ":" + boost::lexical_cast(lineNumber)); + throw CLevelParserException("Unclosed ' in " + m_filename + ":" + StrUtils::ToString(lineNumber)); } else { diff --git a/src/level/parser/parserexceptions.cpp b/src/level/parser/parserexceptions.cpp index f8f279dd..a57689e7 100644 --- a/src/level/parser/parserexceptions.cpp +++ b/src/level/parser/parserexceptions.cpp @@ -21,13 +21,13 @@ #include "level/parser/parser.h" -#include +#include "common/stringutils.h" static std::string FormatMissingParamError(CLevelParserParam* thisParam) NOEXCEPT { auto paramName = thisParam->GetName(); - auto lineNumber = boost::lexical_cast(thisParam->GetLine()->GetLineNumber()); + auto lineNumber = StrUtils::ToString(thisParam->GetLine()->GetLineNumber()); auto fileName = thisParam->GetLine()->GetLevelFilename(); return "Missing required param '" + paramName + "' (in " + fileName + ":" + lineNumber + ")"; } @@ -41,7 +41,7 @@ static std::string FormatBadParamError(CLevelParserParam* thisParam, std::string { auto paramName = thisParam->GetName(); auto paramValue = thisParam->GetValue(); - auto lineNumber = boost::lexical_cast(thisParam->GetLine()->GetLineNumber()); + auto lineNumber = StrUtils::ToString(thisParam->GetLine()->GetLineNumber()); auto fileName = thisParam->GetLine()->GetLevelFilename(); return "Unable to parse '" + paramValue + "' as " + requestedType + " (param '" + paramName + "' in " + fileName + ":" + lineNumber + ")"; } diff --git a/src/level/parser/parserparam.cpp b/src/level/parser/parserparam.cpp index cdd989fb..b4971027 100644 --- a/src/level/parser/parserparam.cpp +++ b/src/level/parser/parserparam.cpp @@ -33,7 +33,6 @@ #include "level/parser/parser.h" -#include #include CLevelParserParam::CLevelParserParam(std::string name, std::string value) @@ -49,11 +48,11 @@ CLevelParserParam::CLevelParserParam(std::string name, bool empty) } CLevelParserParam::CLevelParserParam(int value) - : m_value(boost::lexical_cast(value)) + : m_value(StrUtils::ToString(value)) {} CLevelParserParam::CLevelParserParam(float value) - : m_value(boost::lexical_cast(value)) + : m_value(StrUtils::ToString(value)) {} CLevelParserParam::CLevelParserParam(std::string value) @@ -133,11 +132,14 @@ bool CLevelParserParam::IsDefined() } template -T CLevelParserParam::Cast(std::string value, std::string requestedType) +T CLevelParserParam::Cast(const std::string& value, const std::string& requestedType) { try { - return boost::lexical_cast(value); + std::stringstream stream(value); + T result; + stream >> result; + return result; } catch(...) { @@ -146,7 +148,7 @@ T CLevelParserParam::Cast(std::string value, std::string requestedType) } template -T CLevelParserParam::Cast(std::string requestedType) +T CLevelParserParam::Cast(const std::string& requestedType) { return Cast(m_value, requestedType); } @@ -750,7 +752,7 @@ const std::string CLevelParserParam::FromObjectType(ObjectType value) if (value == OBJECT_HUMAN ) return "Me"; if (value == OBJECT_TECH ) return "Tech"; if (value == OBJECT_CONTROLLER ) return "MissionController"; - return boost::lexical_cast(static_cast(value)); + return StrUtils::ToString(static_cast(value)); } ObjectType CLevelParserParam::AsObjectType() @@ -1052,7 +1054,7 @@ const std::string CLevelParserParam::FromCameraType(Gfx::CameraType value) { if (value == Gfx::CAM_TYPE_ONBOARD) return "ONBOARD"; if (value == Gfx::CAM_TYPE_FIX ) return "FIX"; - return boost::lexical_cast(static_cast(value)); + return StrUtils::ToString(static_cast(value)); } Gfx::CameraType CLevelParserParam::AsCameraType() @@ -1115,7 +1117,7 @@ void CLevelParserParam::ParseArray() { boost::algorithm::trim(value); if (value.empty()) continue; - auto param = std::make_unique(m_name + "[" + boost::lexical_cast(i) + "]", value); + auto param = std::make_unique(m_name + "[" + StrUtils::ToString(i) + "]", value); param->SetLine(m_line); m_array.push_back(std::move(param)); i++; diff --git a/src/level/parser/parserparam.h b/src/level/parser/parserparam.h index aadd9374..4dfcd44c 100644 --- a/src/level/parser/parserparam.h +++ b/src/level/parser/parserparam.h @@ -139,8 +139,8 @@ private: void ParseArray(); void LoadArray(); - template T Cast(std::string value, std::string requestedType); - template T Cast(std::string requestedType); + template T Cast(const std::string& value, const std::string& requestedType); + template T Cast(const std::string& requestedType); std::string ToPath(std::string path, const std::string defaultDir); ObjectType ToObjectType(std::string value); diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index d7c904c0..3e1f1d14 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -117,8 +117,6 @@ #include #include -#include - // Global variables. @@ -2914,7 +2912,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) { try { - int rank = boost::lexical_cast(line->GetParam(type)->GetValue()); + int rank = std::stoi(line->GetParam(type)->GetValue()); if (rank >= 0) { // TODO: Fix default levels and add a future removal warning @@ -2931,7 +2929,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) } } - catch (boost::bad_lexical_cast &e) + catch (std::invalid_argument &e) { return line->GetParam(type)->AsPath("levels"); } @@ -3767,7 +3765,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) if (!m_sceneReadPath.empty()) continue; // ignore errors when loading saved game (TODO: don't report ones that are just not loaded when loading saved game) if (resetObject) continue; // ignore when reseting just objects (TODO: see above) - throw CLevelParserException("Unknown command: '" + line->GetCommand() + "' in " + line->GetLevelFilename() + ":" + boost::lexical_cast(line->GetLineNumber())); + throw CLevelParserException("Unknown command: '" + line->GetCommand() + "' in " + line->GetLevelFilename() + ":" + StrUtils::ToString(line->GetLineNumber())); } // Do this here to prevent the first frame from taking a long time to render diff --git a/src/level/scoreboard.cpp b/src/level/scoreboard.cpp index 0a3781b1..9f7c7eb2 100644 --- a/src/level/scoreboard.cpp +++ b/src/level/scoreboard.cpp @@ -30,7 +30,7 @@ #include "ui/displaytext.h" -#include +#include void CScoreboard::CScoreboardRule::Read(CLevelParserLine* line) { diff --git a/src/object/implementation/program_storage_impl.cpp b/src/object/implementation/program_storage_impl.cpp index c88647d2..791c5ee9 100644 --- a/src/object/implementation/program_storage_impl.cpp +++ b/src/object/implementation/program_storage_impl.cpp @@ -46,8 +46,6 @@ #include "ui/controls/edit.h" -#include - #include #include #include @@ -240,7 +238,7 @@ void CProgramStorageObjectImpl::SaveAllUserPrograms(const std::string& userSourc std::smatch matches; if (std::regex_match(filename, matches, regex)) { - unsigned int id = boost::lexical_cast(matches[1]); + unsigned int id = std::stoul(matches[1]); if (id >= m_program.size() || !m_program[id]->filename.empty()) { GetLogger()->Trace("Removing old program '%s/%s'\n", dir.c_str(), filename.c_str()); @@ -303,7 +301,7 @@ void CProgramStorageObjectImpl::LoadAllProgramsForLevel(CLevelParserLine* levelS std::smatch matches; if (std::regex_match(filename, matches, regex)) { - unsigned int i = boost::lexical_cast(matches[1]); + unsigned int i = std::stoul(matches[1]); Program* program = GetOrAddProgram(i); if(GetCompile(program)) program = AddProgram(); // If original slot is already used, get a new one GetLogger()->Trace("Loading program '%s/%s' from user directory\n", dir.c_str(), filename.c_str()); @@ -348,7 +346,7 @@ void CProgramStorageObjectImpl::SaveAllProgramsForSavedScene(CLevelParserLine* l std::smatch matches; if (std::regex_match(filename, matches, regex)) { - unsigned int id = boost::lexical_cast(matches[1]); + unsigned int id = std::stoul(matches[1]); if (id >= m_program.size() || !m_program[id]->filename.empty()) { GetLogger()->Trace("Removing old program '%s/%s' from saved scene\n", levelSource.c_str(), filename.c_str()); diff --git a/src/object/object_create_exception.h b/src/object/object_create_exception.h index 46987f35..604629c0 100644 --- a/src/object/object_create_exception.h +++ b/src/object/object_create_exception.h @@ -21,18 +21,19 @@ #include "object/object_type.h" +#include "common/stringutils.h" + #include -#include class CObjectCreateException : public std::runtime_error { public: explicit CObjectCreateException(const std::string& error, ObjectType type) - : std::runtime_error("Error creating object type " + boost::lexical_cast(type)) + : std::runtime_error("Error creating object type " + StrUtils::ToString(type)) {} explicit CObjectCreateException(const std::string& error, ObjectType type, const std::string& modelName) : std::runtime_error("Error creating object type " + - boost::lexical_cast(type) + + StrUtils::ToString(type) + " from model " + modelName + ": " + error) {} }; diff --git a/src/object/old_object.cpp b/src/object/old_object.cpp index 53d85ad6..43848ec2 100644 --- a/src/object/old_object.cpp +++ b/src/object/old_object.cpp @@ -65,7 +65,6 @@ #include "ui/controls/edit.h" -#include #include @@ -1242,7 +1241,7 @@ void COldObject::Read(CLevelParserLine* line) m_auto->SetType(line->GetParam("autoType")->AsObjectType(OBJECT_NULL)); for (int i = 0; i < 5; i++) { - std::string op = "autoValue" + boost::lexical_cast(i+1); // autoValue1..autoValue5 + std::string op = "autoValue" + StrUtils::ToString(i+1); // autoValue1..autoValue5 m_auto->SetValue(i, line->GetParam(op)->AsFloat(0.0f)); } m_auto->SetString(const_cast(line->GetParam("autoString")->AsString("").c_str())); diff --git a/src/object/subclass/exchange_post.cpp b/src/object/subclass/exchange_post.cpp index ce981841..477e69a6 100644 --- a/src/object/subclass/exchange_post.cpp +++ b/src/object/subclass/exchange_post.cpp @@ -20,6 +20,7 @@ #include "object/subclass/exchange_post.h" #include "common/regex_utils.h" +#include "common/stringutils.h" #include "graphics/engine/engine.h" #include "graphics/engine/oldmodelmanager.h" @@ -38,8 +39,6 @@ #include "ui/controls/list.h" #include "ui/controls/window.h" -#include - CExchangePost::CExchangePost(int id) : CBaseBuilding(id, OBJECT_INFO) @@ -202,8 +201,8 @@ void CExchangePost::Write(CLevelParserLine* line) ++i; if (!info.name.empty()) { - auto key = "info" + boost::lexical_cast(i); - auto paramValue = info.name + "=" + boost::lexical_cast(info.value); + auto key = "info" + StrUtils::ToString(i); + auto paramValue = info.name + "=" + StrUtils::ToString(info.value); line->AddParam(key, std::make_unique(paramValue)); } } @@ -220,7 +219,7 @@ void CExchangePost::ReadInfo(CLevelParserLine* line) { for (int i = 1; i <= GetMaximumInfoListSize(); i++) { - std::string op = std::string("info") + boost::lexical_cast(i); + std::string op = std::string("info") + StrUtils::ToString(i); if (!line->GetParam(op)->IsDefined()) break; @@ -233,7 +232,7 @@ void CExchangePost::ReadInfo(CLevelParserLine* line) { auto matches = RegexUtils::AssertRegexMatch(text, "([^=]+)=(.*)"); info.name = matches[1]; - info.value = boost::lexical_cast(matches[2]); + info.value = std::stof(matches[2]); } catch (...) {