From 6e94848aec5d42eed5f9e1f5548ade26915ce90b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 4 Jun 2024 21:14:45 +0100 Subject: Tidy many stringy messes with std::format --- assetFactory/assimp.cpp | 7 ++--- assetFactory/object.cpp | 3 +- gfx/gl/program.cpp | 3 +- gfx/gl/shader.cpp | 3 +- gfx/image.cpp | 6 ++-- lib/persistence.h | 3 +- test/test-text.cpp | 8 ++--- ui/font.cpp | 79 +++++++++++++++++++++++++------------------------ ui/window.cpp | 3 +- 9 files changed, 62 insertions(+), 53 deletions(-) diff --git a/assetFactory/assimp.cpp b/assetFactory/assimp.cpp index f72e50a..1e11eda 100644 --- a/assetFactory/assimp.cpp +++ b/assetFactory/assimp.cpp @@ -1,12 +1,11 @@ #include "assimp.h" #include "assetFactory.h" #include "collections.h" -#include "ptr.h" #include "resource.h" #include #include #include -#include +#include #include template @@ -39,7 +38,7 @@ public: const aiNode * node; CreatedFaces - createMesh(ModelFactoryMesh & mesh, Scale3D) const + createMesh(ModelFactoryMesh & mesh, Scale3D) const override { CreatedFaces faces; addMesh(faces, mesh, node); @@ -103,7 +102,7 @@ AssImp::postLoad() SceneCPtr scene { aiImportFile(Resource::mapPath(path).c_str(), aiProcess_RemoveRedundantMaterials), &aiReleaseImport}; if (!scene) { - throw std::runtime_error("Failed to load asset library: " + path); + throw std::runtime_error(std::format("Failed to load asset library: {}", path)); } if (auto mf = Persistence::ParseBase::getShared("assetFactory")) { const auto root = AIRANGE(scene->mRootNode, Children); diff --git a/assetFactory/object.cpp b/assetFactory/object.cpp index f48dcf6..f87a487 100644 --- a/assetFactory/object.cpp +++ b/assetFactory/object.cpp @@ -1,5 +1,6 @@ #include "object.h" #include +#include Object::Object(std::string i) : id {std::move(i)} { } @@ -10,7 +11,7 @@ Object::createMesh(ModelFactoryMesh & mesh, Scale3D levelOfDetailFactor) const for (const auto & use : uses) { auto useFaces = use->createMesh(mesh, levelOfDetailFactor); std::transform(useFaces.begin(), useFaces.end(), std::inserter(faces, faces.end()), [this](auto && face) { - return std::make_pair(id + ":" + face.first, std::move(face.second)); + return std::make_pair(std::format("{}:{}", id, face.first), std::move(face.second)); }); } return faces; diff --git a/gfx/gl/program.cpp b/gfx/gl/program.cpp index 0b60eaa..7287fde 100644 --- a/gfx/gl/program.cpp +++ b/gfx/gl/program.cpp @@ -1,5 +1,6 @@ #include "program.h" #include "shader.h" +#include #include #include #include @@ -30,6 +31,6 @@ Program::RequiredUniformLocation::RequiredUniformLocation(GLuint program, const UniformLocation {program, name} { if (location < 0) { - throw std::logic_error(std::string {"Required uniform does not exist: "} + name); + throw std::logic_error {std::format("Required uniform does not exist: {}", name)}; } } diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index 0f75817..0bc127a 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -1,6 +1,7 @@ #include "shader.h" #include #include +#include #include #include @@ -70,6 +71,6 @@ Shader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, std::string glGetShaderInfoLog(shader, error.size(), nullptr, error.data()); } - throw std::runtime_error {std::string {errorMessage} + ": '" + std::string {error.data(), error.size()} + "'"}; + throw std::runtime_error {std::format("{}: '{}'", errorMessage, error.data())}; } } diff --git a/gfx/image.cpp b/gfx/image.cpp index fb86cb6..3b63054 100644 --- a/gfx/image.cpp +++ b/gfx/image.cpp @@ -1,5 +1,6 @@ #include "image.h" #include +#include #include #include @@ -13,7 +14,7 @@ Image::Image(const char * fileName, int flags) : width {}, height {}, numCompone numComponents = static_cast(nc); if (!bytes) { - throw std::runtime_error {std::string {"Unable to load image: "} + fileName}; + throw std::runtime_error {std::format("Unable to load image: {}", fileName)}; } data = {bytes, static_cast(width * height * numComponents)}; @@ -29,7 +30,8 @@ Image::Image(std::span buffer, int flags) numComponents = static_cast(nc); if (!bytes) { - throw std::runtime_error {"Unable to load image from memory buffer "}; + throw std::runtime_error {std::format("Unable to load image from memory buffer @ {} ({} bytes)", + static_cast(buffer.data()), buffer.size_bytes())}; } data = {bytes, static_cast(width * height * numComponents)}; diff --git a/lib/persistence.h b/lib/persistence.h index 3c95a00..e385b20 100644 --- a/lib/persistence.h +++ b/lib/persistence.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -571,7 +572,7 @@ namespace Persistence { make_default_as_needed(this->v); PersistenceSelect ps {mbr}; if (this->v->persist(ps)) { - throw std::runtime_error("cannot find member: " + mbr); + throw std::runtime_error {std::format("Cannot find member: {}", mbr)}; } return std::move(ps.sel); } diff --git a/test/test-text.cpp b/test/test-text.cpp index f652670..f185cf5 100644 --- a/test/test-text.cpp +++ b/test/test-text.cpp @@ -70,10 +70,10 @@ using CharDataTest = std::tuple({ - {'A', {0, {34, 35}, {627, 0}, {-1, 35}, 32}}, - {'I', {0, {6, 35}, {862, 0}, {4, 35}, 13}}, - {'j', {0, {11, 45}, {1656, 0}, {-3, 35}, 11}}, - {'o', {0, {24, 27}, {1748, 0}, {1, 27}, 27}}, + {'A', {0, {34, 35}, {712, 0}, {-1, 35}, 32}}, + {'I', {0, {6, 35}, {947, 0}, {4, 35}, 13}}, + {'j', {0, {11, 45}, {1741, 0}, {-3, 35}, 11}}, + {'o', {0, {24, 27}, {1833, 0}, {1, 27}, 27}}, }), character, expected) { diff --git a/ui/font.cpp b/ui/font.cpp index b6669b0..ebd29d0 100644 --- a/ui/font.cpp +++ b/ui/font.cpp @@ -1,6 +1,7 @@ #include "font.h" #include #include +#include #include #include FT_FREETYPE_H #include "gl_traits.h" @@ -15,49 +16,51 @@ // IWYU pragma: no_forward_declare FT_LibraryRec_ -std::string -FT_Error_StringSafe(FT_Error err) -{ - if (const auto errstr = FT_Error_String(err)) { - return {errstr}; +namespace { + std::string + FT_Error_StringSafe(FT_Error err) + { + if (const auto errstr = FT_Error_String(err)) { + return {errstr}; + } + return std::format("Unknown FT error: {}", err); } - return std::to_string(err); -} -template -void -FT_Check(Args &&... args) -{ - if (const auto err = Func(std::forward(args)...)) { - throw std::runtime_error {std::string {"FreeType error: "} + FT_Error_StringSafe(err)}; + template + void + FT_Check(Args &&... args) + { + if (const auto err = Func(std::forward(args)...)) { + throw std::runtime_error {std::format("FreeType error: {}", FT_Error_StringSafe(err))}; + } } -} -const std::string BASIC_CHARS = []() { - std::string chars; - for (char c {}; c >= 0; c++) { - if (isgraph(c)) { - chars += c; + const std::string BASIC_CHARS = []() { + std::string chars {"£€²³"}; + for (char c {}; c >= 0; c++) { + if (isgraph(c)) { + chars += c; + } } - } - return chars + "£€²³"; -}(); - -using FT = glRef(&ft); - return ft; - }, - FT_Done_FreeType>; - -using Face = glRef(ft, name, 0, &face); - return face; - }, - FT_Done_Face>; + return chars; + }(); + + using FT = glRef(&ft); + return ft; + }, + FT_Done_FreeType>; + + using Face = glRef(ft, name, 0, &face); + return face; + }, + FT_Done_Face>; +} Font::Font(std::filesystem::path p, unsigned s) : path {std::move(p)}, size {getTextureSize(s)} { diff --git a/ui/window.cpp b/ui/window.cpp index 6855aa0..dd488d7 100644 --- a/ui/window.cpp +++ b/ui/window.cpp @@ -1,5 +1,6 @@ #include "window.h" #include "uiComponent.h" +#include #include #include #include @@ -9,7 +10,7 @@ Window::GLInitHelper::GLInitHelper() [[maybe_unused]] static auto init = []() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) if (const auto version = gladLoadGL(reinterpret_cast(SDL_GL_GetProcAddress)); version < 30003) { - throw std::runtime_error {"Insufficient OpenGL version: " + std::to_string(version)}; + throw std::runtime_error {std::format("Insufficient OpenGL version: {}", version)}; } else { return version; -- cgit v1.2.3