From a4f0c7cdb515f13b33d0ad5c9a7ebda97ea34186 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 23 Mar 2024 14:52:30 -0500 Subject: [PATCH 01/16] [nostalgia/core/studio] Remove applicationData usages --- .../paletteeditor/paletteeditor-imgui.cpp | 14 +++++------ .../paletteeditor/paletteeditor-imgui.hpp | 5 ++-- .../tilesheeteditor/tilesheeteditor-imgui.cpp | 23 +++++++++--------- .../tilesheeteditor/tilesheeteditor-imgui.hpp | 5 ++-- .../tilesheeteditor/tilesheeteditormodel.cpp | 24 +++++++++---------- .../tilesheeteditor/tilesheeteditormodel.hpp | 5 ++-- .../tilesheeteditor/tilesheeteditorview.cpp | 4 ++-- .../tilesheeteditor/tilesheeteditorview.hpp | 2 +- 8 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp index baf95e27..832d3545 100644 --- a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp +++ b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp @@ -15,10 +15,11 @@ namespace nostalgia::core { -PaletteEditorImGui::PaletteEditorImGui(studio::StudioContext &ctx, ox::CRStringView path): +PaletteEditorImGui::PaletteEditorImGui(studio::StudioContext &sctx, ox::CRStringView path): Editor(path), - m_ctx(ctx.tctx), - m_pal(*keel::readObj(keelCtx(m_ctx), ox::FileAddress(itemPath())).unwrapThrow()) { + m_sctx(sctx), + m_tctx(sctx.tctx), + m_pal(*keel::readObj(keelCtx(m_tctx), ox::FileAddress(itemPath())).unwrapThrow()) { undoStack()->changeTriggered.connect(this, &PaletteEditorImGui::handleCommand); } @@ -27,12 +28,12 @@ void PaletteEditorImGui::keyStateChanged(turbine::Key key, bool down) { return; } if (key >= turbine::Key::Num_1 && key <= turbine::Key::Num_9) { - if (turbine::buttonDown(m_ctx, turbine::Key::Mod_Alt)) { + if (turbine::buttonDown(m_tctx, turbine::Key::Mod_Alt)) { m_page = ox::min( static_cast(key - turbine::Key::Num_1), m_pal.pages.size() - 1); } } else if (key == turbine::Key::Num_0) { - if (turbine::buttonDown(m_ctx, turbine::Key::Mod_Alt)) { + if (turbine::buttonDown(m_tctx, turbine::Key::Mod_Alt)) { m_selectedColorRow = ox::min( static_cast(key - turbine::Key::Num_1 + 9), m_pal.pages.size() - 1); @@ -57,8 +58,7 @@ void PaletteEditorImGui::draw(turbine::Context&) noexcept { } ox::Error PaletteEditorImGui::saveItem() noexcept { - const auto sctx = applicationData(m_ctx); - return sctx->project->writeObj(itemPath(), m_pal); + return m_sctx.project->writeObj(itemPath(), m_pal); } void PaletteEditorImGui::drawColumn(ox::CStringView txt) noexcept { diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.hpp b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.hpp index a683bdc0..8f022467 100644 --- a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.hpp +++ b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.hpp @@ -14,13 +14,14 @@ namespace nostalgia::core { class PaletteEditorImGui: public studio::Editor { private: - turbine::Context &m_ctx; + studio::StudioContext &m_sctx; + turbine::Context &m_tctx; Palette m_pal; size_t m_selectedColorRow = 0; size_t m_page = 0; public: - PaletteEditorImGui(studio::StudioContext &ctx, ox::CRStringView path); + PaletteEditorImGui(studio::StudioContext &sctx, ox::CRStringView path); void keyStateChanged(turbine::Key key, bool down) override; diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp index 1abaec74..faabe8a0 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp @@ -75,10 +75,11 @@ static ox::Error toPngFile( 8))); } -TileSheetEditorImGui::TileSheetEditorImGui(studio::StudioContext &ctx, ox::CRStringView path): +TileSheetEditorImGui::TileSheetEditorImGui(studio::StudioContext &sctx, ox::CRStringView path): Editor(path), - m_ctx(ctx.tctx), - m_view(m_ctx, path, *undoStack()), + m_sctx(sctx), + m_tctx(m_sctx.tctx), + m_view(m_sctx, path, *undoStack()), m_model(m_view.model()) { oxIgnoreError(setPaletteSelection()); // connect signal/slots @@ -134,7 +135,7 @@ void TileSheetEditorImGui::keyStateChanged(turbine::Key key, bool down) { setPasteEnabled(false); m_model.clearSelection(); } else if (key >= turbine::Key::Num_1 && key <= turbine::Key::Num_9) { - if (turbine::buttonDown(m_ctx, turbine::Key::Mod_Alt)) { + if (turbine::buttonDown(m_tctx, turbine::Key::Mod_Alt)) { auto const idx = ox::min( static_cast(key - turbine::Key::Num_1), m_model.pal().pages.size() - 1); m_model.setPalettePage(idx); @@ -143,7 +144,7 @@ void TileSheetEditorImGui::keyStateChanged(turbine::Key key, bool down) { m_view.setPalIdx(idx); } } else if (key == turbine::Key::Num_0) { - if (turbine::buttonDown(m_ctx, turbine::Key::Mod_Alt)) { + if (turbine::buttonDown(m_tctx, turbine::Key::Mod_Alt)) { auto const idx = ox::min( static_cast(key - turbine::Key::Num_1 + 9), m_model.pal().pages.size() - 1); m_model.setPalettePage(idx); @@ -235,8 +236,8 @@ void TileSheetEditorImGui::draw(turbine::Context&) noexcept { ImGui::EndChild(); } ImGui::EndChild(); - m_subsheetEditor.draw(m_ctx); - m_exportMenu.draw(m_ctx); + m_subsheetEditor.draw(m_tctx); + m_exportMenu.draw(m_tctx); } void TileSheetEditorImGui::drawSubsheetSelector( @@ -359,7 +360,7 @@ void TileSheetEditorImGui::drawTileSheet(ox::Vec2 const&fbSize) noexcept { auto const wheelh = io.MouseWheelH; if (wheel != 0) { const auto zoomMod = ox::defines::OS == ox::OS::Darwin ? - io.KeySuper : turbine::buttonDown(m_ctx, turbine::Key::Mod_Ctrl); + io.KeySuper : turbine::buttonDown(m_tctx, turbine::Key::Mod_Ctrl); m_view.scrollV(fbSize, wheel, zoomMod); } if (wheelh != 0) { @@ -399,8 +400,7 @@ void TileSheetEditorImGui::drawTileSheet(ox::Vec2 const&fbSize) noexcept { } void TileSheetEditorImGui::drawPaletteSelector() noexcept { - auto &sctx = *applicationData(m_ctx); - auto const&files = sctx.project->fileList(core::FileExt_npal); + auto const&files = m_sctx.project->fileList(core::FileExt_npal); auto const comboWidthSub = 62; ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - comboWidthSub); if (ig::ComboBox("Palette", files, m_selectedPaletteIdx)) { @@ -465,8 +465,7 @@ ox::Error TileSheetEditorImGui::updateActiveSubsheet(ox::StringView const&name, ox::Error TileSheetEditorImGui::setPaletteSelection() noexcept { auto const&palPath = m_model.palPath(); - auto &sctx = *applicationData(m_ctx); - auto const&palList = sctx.project->fileList(core::FileExt_npal); + auto const&palList = m_sctx.project->fileList(core::FileExt_npal); for (std::size_t i = 0; auto const&pal : palList) { if (palPath == pal) { m_selectedPaletteIdx = i; diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.hpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.hpp index 26d0c4df..529bba9a 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.hpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.hpp @@ -61,7 +61,8 @@ class TileSheetEditorImGui: public studio::Editor { inline bool isOpen() const noexcept { return m_show; } }; std::size_t m_selectedPaletteIdx = 0; - turbine::Context &m_ctx; + studio::StudioContext &m_sctx; + turbine::Context &m_tctx; ox::Vector m_paletteList; SubSheetEditor m_subsheetEditor; ExportMenu m_exportMenu; @@ -73,7 +74,7 @@ class TileSheetEditorImGui: public studio::Editor { Tool m_tool = Tool::Draw; public: - TileSheetEditorImGui(studio::StudioContext &ctx, ox::CRStringView path); + TileSheetEditorImGui(studio::StudioContext &sctx, ox::CRStringView path); ~TileSheetEditorImGui() override = default; diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.cpp index 7694efa9..d5348252 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.cpp @@ -41,12 +41,13 @@ static void normalizeSubsheets(TileSheet::SubSheet &ss) noexcept { } } -TileSheetEditorModel::TileSheetEditorModel(turbine::Context &ctx, ox::StringView path, studio::UndoStack &undoStack): - m_ctx(ctx), +TileSheetEditorModel::TileSheetEditorModel(studio::StudioContext &sctx, ox::StringView path, studio::UndoStack &undoStack): + m_sctx(sctx), + m_tctx(m_sctx.tctx), m_path(path), - m_img(*readObj(keelCtx(m_ctx), m_path).unwrapThrow()), + m_img(*readObj(keelCtx(m_tctx), m_path).unwrapThrow()), // ignore failure to load palette - m_pal(readObj(keelCtx(m_ctx), m_img.defaultPalette).value), + m_pal(readObj(keelCtx(m_tctx), m_img.defaultPalette).value), m_undoStack(undoStack) { normalizeSubsheets(m_img.subsheet); m_pal.updated.connect(this, &TileSheetEditorModel::markUpdated); @@ -70,7 +71,7 @@ void TileSheetEditorModel::cut() { } const auto pt1 = m_selectionOrigin == ox::Point(-1, -1) ? ox::Point(0, 0) : m_selectionOrigin; const auto pt2 = ox::Point(s.columns * TileWidth, s.rows * TileHeight); - turbine::setClipboardObject(m_ctx, std::move(cb)); + turbine::setClipboardObject(m_tctx, std::move(cb)); pushCommand(ox::make(CommandId::Cut, m_img, m_activeSubsSheetIdx, pt1, pt2, blankCb)); } @@ -87,11 +88,11 @@ void TileSheetEditorModel::copy() { cb->addPixel(pt, c); } } - turbine::setClipboardObject(m_ctx, std::move(cb)); + turbine::setClipboardObject(m_tctx, std::move(cb)); } void TileSheetEditorModel::paste() { - auto [cb, err] = turbine::getClipboardObject(m_ctx); + auto [cb, err] = turbine::getClipboardObject(m_tctx); if (err) { oxLogError(err); oxErrf("Could not read clipboard: {}", toStr(err)); @@ -111,7 +112,7 @@ ox::StringView TileSheetEditorModel::palPath() const noexcept { constexpr ox::StringView uuidPrefix = "uuid://"; if (ox::beginsWith(path, uuidPrefix)) { auto uuid = ox::StringView(path.data() + uuidPrefix.bytes(), path.bytes() - uuidPrefix.bytes()); - auto out = keelCtx(m_ctx).uuidToPath.at(uuid); + auto out = keelCtx(m_tctx).uuidToPath.at(uuid); if (out.error) { return {}; } @@ -122,7 +123,7 @@ ox::StringView TileSheetEditorModel::palPath() const noexcept { } ox::Error TileSheetEditorModel::setPalette(ox::StringView path) noexcept { - oxRequire(uuid, keelCtx(m_ctx).pathToUuid.at(path)); + oxRequire(uuid, keelCtx(m_tctx).pathToUuid.at(path)); pushCommand(ox::make(activeSubSheetIdx(), m_img, uuid->toString())); return {}; } @@ -239,7 +240,7 @@ ox::Error TileSheetEditorModel::markUpdatedCmdId(studio::UndoCommand const*cmd) m_updated = true; const auto cmdId = cmd->commandId(); if (static_cast(cmdId) == CommandId::PaletteChange) { - oxReturnError(readObj(keelCtx(m_ctx), m_img.defaultPalette).moveTo(m_pal)); + oxReturnError(readObj(keelCtx(m_tctx), m_img.defaultPalette).moveTo(m_pal)); m_palettePage = ox::min(m_pal->pages.size(), 0); paletteChanged.emit(); } @@ -261,8 +262,7 @@ void TileSheetEditorModel::ackUpdate() noexcept { } ox::Error TileSheetEditorModel::saveFile() noexcept { - const auto sctx = applicationData(m_ctx); - return sctx->project->writeObj(m_path, m_img, ox::ClawFormat::Metal); + return m_sctx.project->writeObj(m_path, m_img, ox::ClawFormat::Metal); } bool TileSheetEditorModel::pixelSelected(std::size_t idx) const noexcept { diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.hpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.hpp index 361d2006..f4fe90e5 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.hpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditormodel.hpp @@ -24,7 +24,8 @@ class TileSheetEditorModel: public ox::SignalHandler { private: static Palette const s_defaultPalette; - turbine::Context &m_ctx; + studio::StudioContext &m_sctx; + turbine::Context &m_tctx; ox::String m_path; TileSheet m_img; TileSheet::SubSheetIdx m_activeSubsSheetIdx; @@ -38,7 +39,7 @@ class TileSheetEditorModel: public ox::SignalHandler { ox::Bounds m_selectionBounds = {{-1, -1}, {-1, -1}}; public: - TileSheetEditorModel(turbine::Context &ctx, ox::StringView path, studio::UndoStack &undoStack); + TileSheetEditorModel(studio::StudioContext &sctx, ox::StringView path, studio::UndoStack &undoStack); ~TileSheetEditorModel() override = default; diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.cpp index b9f4ec8e..ec618a5f 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.cpp @@ -11,8 +11,8 @@ namespace nostalgia::core { -TileSheetEditorView::TileSheetEditorView(turbine::Context &ctx, ox::StringView path, studio::UndoStack &undoStack): - m_model(ctx, path, undoStack), +TileSheetEditorView::TileSheetEditorView(studio::StudioContext &sctx, ox::StringView path, studio::UndoStack &undoStack): + m_model(sctx, path, undoStack), m_pixelsDrawer(m_model) { // build shaders oxThrowError(m_pixelsDrawer.buildShader()); diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.hpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.hpp index 52ef3028..d421237d 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.hpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.hpp @@ -50,7 +50,7 @@ class TileSheetEditorView: public ox::SignalHandler { std::size_t m_palIdx = 0; public: - TileSheetEditorView(turbine::Context &ctx, ox::StringView path, studio::UndoStack &undoStack); + TileSheetEditorView(studio::StudioContext &sctx, ox::StringView path, studio::UndoStack &undoStack); ~TileSheetEditorView() override = default; From 27f1df8f3326cbe7dae886423a3bf1a8563f40b9 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 23 Mar 2024 16:07:49 -0500 Subject: [PATCH 02/16] [nostalgia,olympic] Further reduce use of applicationData --- .../paletteeditor/paletteeditor-imgui.cpp | 2 +- .../paletteeditor/paletteeditor-imgui.hpp | 2 +- .../tilesheeteditor/tilesheeteditor-imgui.cpp | 12 ++++----- .../tilesheeteditor/tilesheeteditor-imgui.hpp | 6 ++--- .../scene/src/studio/sceneeditor-imgui.cpp | 2 +- .../scene/src/studio/sceneeditor-imgui.hpp | 2 +- src/olympic/studio/applib/src/aboutpopup.cpp | 4 +-- src/olympic/studio/applib/src/aboutpopup.hpp | 2 +- src/olympic/studio/applib/src/clawviewer.cpp | 2 +- src/olympic/studio/applib/src/clawviewer.hpp | 2 +- src/olympic/studio/applib/src/newmenu.cpp | 27 +++++++++---------- src/olympic/studio/applib/src/newmenu.hpp | 10 +++---- src/olympic/studio/applib/src/newproject.cpp | 10 +++---- src/olympic/studio/applib/src/newproject.hpp | 6 ++--- .../studio/applib/src/projectexplorer.cpp | 4 +-- .../studio/applib/src/projectexplorer.hpp | 2 +- src/olympic/studio/applib/src/studioapp.cpp | 8 +++--- .../modlib/include/studio/itemmaker.hpp | 13 ++++----- .../studio/modlib/include/studio/popup.hpp | 2 +- .../studio/modlib/include/studio/widget.hpp | 4 ++- 20 files changed, 62 insertions(+), 60 deletions(-) diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp index 832d3545..37377a9a 100644 --- a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp +++ b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp @@ -42,7 +42,7 @@ void PaletteEditorImGui::keyStateChanged(turbine::Key key, bool down) { } -void PaletteEditorImGui::draw(turbine::Context&) noexcept { +void PaletteEditorImGui::draw(studio::StudioContext&) noexcept { auto const paneSize = ImGui::GetContentRegionAvail(); { ImGui::BeginChild("Pages", ImVec2(250, paneSize.y), true); diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.hpp b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.hpp index 8f022467..d8d5982f 100644 --- a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.hpp +++ b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.hpp @@ -25,7 +25,7 @@ class PaletteEditorImGui: public studio::Editor { void keyStateChanged(turbine::Key key, bool down) override; - void draw(turbine::Context&) noexcept final; + void draw(studio::StudioContext&) noexcept final; protected: ox::Error saveItem() noexcept final; diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp index faabe8a0..3d3a0687 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp @@ -157,7 +157,7 @@ void TileSheetEditorImGui::keyStateChanged(turbine::Key key, bool down) { } } -void TileSheetEditorImGui::draw(turbine::Context&) noexcept { +void TileSheetEditorImGui::draw(studio::StudioContext&) noexcept { auto const paneSize = ImGui::GetContentRegionAvail(); auto const tileSheetParentSize = ImVec2(paneSize.x - m_palViewWidth, paneSize.y); auto const fbSize = ox::Vec2(tileSheetParentSize.x - 16, tileSheetParentSize.y - 16); @@ -237,7 +237,7 @@ void TileSheetEditorImGui::draw(turbine::Context&) noexcept { } ImGui::EndChild(); m_subsheetEditor.draw(m_tctx); - m_exportMenu.draw(m_tctx); + m_exportMenu.draw(m_sctx); } void TileSheetEditorImGui::drawSubsheetSelector( @@ -481,7 +481,7 @@ ox::Error TileSheetEditorImGui::markUnsavedChanges(studio::UndoCommand const*) n return {}; } -void TileSheetEditorImGui::SubSheetEditor::draw(turbine::Context &ctx) noexcept { +void TileSheetEditorImGui::SubSheetEditor::draw(turbine::Context &sctx) noexcept { constexpr auto popupName = "Edit Subsheet"; if (!m_show) { return; @@ -490,7 +490,7 @@ void TileSheetEditorImGui::SubSheetEditor::draw(turbine::Context &ctx) noexcept auto constexpr popupWidth = 235.f; auto const popupHeight = modSize ? 130.f : 85.f; auto const popupSz = ImVec2(popupWidth, popupHeight); - if (ig::BeginPopup(ctx, popupName, m_show, popupSz)) { + if (ig::BeginPopup(sctx, popupName, m_show, popupSz)) { ImGui::InputText("Name", m_name.data(), m_name.cap()); if (modSize) { ImGui::InputInt("Columns", &m_cols); @@ -507,7 +507,7 @@ void TileSheetEditorImGui::SubSheetEditor::close() noexcept { m_show = false; } -void TileSheetEditorImGui::ExportMenu::draw(turbine::Context &ctx) noexcept { +void TileSheetEditorImGui::ExportMenu::draw(studio::StudioContext &sctx) noexcept { constexpr auto popupName = "Export Tile Sheet"; if (!m_show) { return; @@ -515,7 +515,7 @@ void TileSheetEditorImGui::ExportMenu::draw(turbine::Context &ctx) noexcept { constexpr auto popupWidth = 235.f; constexpr auto popupHeight = 85.f; constexpr auto popupSz = ImVec2(popupWidth, popupHeight); - if (ig::BeginPopup(ctx, popupName, m_show, popupSz)) { + if (ig::BeginPopup(sctx.tctx, popupName, m_show, popupSz)) { ImGui::InputInt("Scale", &m_scale); m_scale = ox::clamp(m_scale, 1, 50); if (ig::PopupControlsOkCancel(popupWidth, m_show) == ig::PopupResponse::OK) { diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.hpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.hpp index 529bba9a..6a545873 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.hpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.hpp @@ -40,7 +40,7 @@ class TileSheetEditorImGui: public studio::Editor { m_cols = cols; m_rows = rows; } - void draw(turbine::Context &ctx) noexcept; + void draw(turbine::Context &sctx) noexcept; void close() noexcept; [[nodiscard]] inline bool isOpen() const noexcept { return m_show; } @@ -55,7 +55,7 @@ class TileSheetEditorImGui: public studio::Editor { m_show = true; m_scale = 5; } - void draw(turbine::Context &ctx) noexcept; + void draw(studio::StudioContext &sctx) noexcept; void close() noexcept; [[nodiscard]] inline bool isOpen() const noexcept { return m_show; } @@ -88,7 +88,7 @@ class TileSheetEditorImGui: public studio::Editor { void keyStateChanged(turbine::Key key, bool down) override; - void draw(turbine::Context&) noexcept override; + void draw(studio::StudioContext&) noexcept override; void drawSubsheetSelector(TileSheet::SubSheet&, TileSheet::SubSheetIdx &path); diff --git a/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.cpp b/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.cpp index 83dd413d..beef45fb 100644 --- a/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.cpp +++ b/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.cpp @@ -18,7 +18,7 @@ SceneEditorImGui::SceneEditorImGui(studio::StudioContext &ctx, ox::StringView pa setRequiresConstantRefresh(false); } -void SceneEditorImGui::draw(turbine::Context&) noexcept { +void SceneEditorImGui::draw(studio::StudioContext&) noexcept { auto const paneSize = ImGui::GetContentRegionAvail(); m_view.draw(ox::Size{static_cast(paneSize.x), static_cast(paneSize.y)}); auto &fb = m_view.framebuffer(); diff --git a/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.hpp b/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.hpp index 0508f424..24bc0b65 100644 --- a/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.hpp +++ b/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.hpp @@ -23,7 +23,7 @@ class SceneEditorImGui: public studio::Editor { public: SceneEditorImGui(studio::StudioContext &ctx, ox::StringView path); - void draw(turbine::Context&) noexcept final; + void draw(studio::StudioContext&) noexcept final; void onActivated() noexcept override; diff --git a/src/olympic/studio/applib/src/aboutpopup.cpp b/src/olympic/studio/applib/src/aboutpopup.cpp index ebd93ddc..6bbd2c7e 100644 --- a/src/olympic/studio/applib/src/aboutpopup.cpp +++ b/src/olympic/studio/applib/src/aboutpopup.cpp @@ -29,7 +29,7 @@ bool AboutPopup::isOpen() const noexcept { return m_stage == Stage::Open; } -void AboutPopup::draw(turbine::Context &ctx) noexcept { +void AboutPopup::draw(studio::StudioContext &sctx) noexcept { switch (m_stage) { case Stage::Closed: break; @@ -41,7 +41,7 @@ void AboutPopup::draw(turbine::Context &ctx) noexcept { constexpr auto modalFlags = ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize; ImGui::SetNextWindowSize(ImVec2(215, 90)); - studio::ig::centerNextWindow(ctx); + studio::ig::centerNextWindow(sctx.tctx); auto open = true; if (ImGui::BeginPopupModal("About", &open, modalFlags)) { ImGui::Text("%s", m_text.c_str()); diff --git a/src/olympic/studio/applib/src/aboutpopup.hpp b/src/olympic/studio/applib/src/aboutpopup.hpp index 155bef66..39e8c407 100644 --- a/src/olympic/studio/applib/src/aboutpopup.hpp +++ b/src/olympic/studio/applib/src/aboutpopup.hpp @@ -35,7 +35,7 @@ class AboutPopup: public studio::Popup { [[nodiscard]] bool isOpen() const noexcept override; - void draw(turbine::Context &ctx) noexcept override; + void draw(studio::StudioContext &sctx) noexcept override; }; diff --git a/src/olympic/studio/applib/src/clawviewer.cpp b/src/olympic/studio/applib/src/clawviewer.cpp index 58df6d6e..861a04b0 100644 --- a/src/olympic/studio/applib/src/clawviewer.cpp +++ b/src/olympic/studio/applib/src/clawviewer.cpp @@ -13,7 +13,7 @@ ClawEditor::ClawEditor(ox::CRStringView path, ox::ModelObject obj) noexcept: m_obj(std::move(obj)) { } -void ClawEditor::draw(turbine::Context&) noexcept { +void ClawEditor::draw(studio::StudioContext&) noexcept { ImGui::BeginChild("PaletteEditor"); static constexpr auto flags = ImGuiTableFlags_RowBg | ImGuiTableFlags_NoBordersInBody; if (ImGui::BeginTable("ObjTree", 3, flags)) { diff --git a/src/olympic/studio/applib/src/clawviewer.hpp b/src/olympic/studio/applib/src/clawviewer.hpp index ed6697e9..5441ac30 100644 --- a/src/olympic/studio/applib/src/clawviewer.hpp +++ b/src/olympic/studio/applib/src/clawviewer.hpp @@ -18,7 +18,7 @@ class ClawEditor: public studio::Editor { public: ClawEditor(ox::CRStringView path, ox::ModelObject obj) noexcept; - void draw(turbine::Context&) noexcept final; + void draw(studio::StudioContext&) noexcept final; private: static void drawRow(ox::ModelValue const&value) noexcept; diff --git a/src/olympic/studio/applib/src/newmenu.cpp b/src/olympic/studio/applib/src/newmenu.cpp index e5786cac..7fea9923 100644 --- a/src/olympic/studio/applib/src/newmenu.cpp +++ b/src/olympic/studio/applib/src/newmenu.cpp @@ -33,7 +33,7 @@ bool NewMenu::isOpen() const noexcept { return m_open; } -void NewMenu::draw(turbine::Context &ctx) noexcept { +void NewMenu::draw(studio::StudioContext &sctx) noexcept { switch (m_stage) { case Stage::Opening: ImGui::OpenPopup(title().c_str()); @@ -41,10 +41,10 @@ void NewMenu::draw(turbine::Context &ctx) noexcept { m_open = true; [[fallthrough]]; case Stage::NewItemType: - drawNewItemType(ctx); + drawNewItemType(sctx); break; case Stage::NewItemName: - drawNewItemName(ctx); + drawNewItemName(sctx); break; case Stage::Closed: m_open = false; @@ -61,8 +61,8 @@ void NewMenu::addItemMaker(ox::UniquePtr &&im) noexcept { }); } -void NewMenu::drawNewItemType(turbine::Context &ctx) noexcept { - drawWindow(ctx, &m_open, [this] { +void NewMenu::drawNewItemType(studio::StudioContext &sctx) noexcept { + drawWindow(sctx.tctx, &m_open, [this] { auto items = ox_malloca(m_types.size() * sizeof(char const*), char const*, nullptr); for (auto i = 0u; auto const&im : m_types) { items.get()[i] = im->typeName.c_str(); @@ -73,13 +73,13 @@ void NewMenu::drawNewItemType(turbine::Context &ctx) noexcept { }); } -void NewMenu::drawNewItemName(turbine::Context &ctx) noexcept { - drawWindow(ctx, &m_open, [this, &ctx] { +void NewMenu::drawNewItemName(studio::StudioContext &sctx) noexcept { + drawWindow(sctx.tctx, &m_open, [this, &sctx] { auto const typeIdx = static_cast(m_selectedType); if (typeIdx < m_types.size()) { ImGui::InputText("Name", m_itemName.data(), m_itemName.cap()); } - drawLastPageButtons(ctx); + drawLastPageButtons(sctx); }); } @@ -97,7 +97,7 @@ void NewMenu::drawFirstPageButtons() noexcept { } } -void NewMenu::drawLastPageButtons(turbine::Context &ctx) noexcept { +void NewMenu::drawLastPageButtons(studio::StudioContext &sctx) noexcept { ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - 138); ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetContentRegionAvail().y - 20); if (ImGui::Button("Back")) { @@ -105,7 +105,7 @@ void NewMenu::drawLastPageButtons(turbine::Context &ctx) noexcept { } ImGui::SameLine(); if (ImGui::Button("Finish")) { - finish(ctx); + finish(sctx); } ImGui::SameLine(); if (ImGui::Button("Quit")) { @@ -114,17 +114,16 @@ void NewMenu::drawLastPageButtons(turbine::Context &ctx) noexcept { } } -void NewMenu::finish(turbine::Context &ctx) noexcept { +void NewMenu::finish(studio::StudioContext &sctx) noexcept { if (m_itemName.len() == 0) { return; } - auto const sctx = turbine::applicationData(ctx); auto const&typeMaker = *m_types[static_cast(m_selectedType)]; - if (sctx->project->exists(typeMaker.itemPath(m_itemName))) { + if (sctx.project->exists(typeMaker.itemPath(m_itemName))) { oxLogError(OxError(1, "New file error: File already exists")); return; } - auto const [path, err] = typeMaker.write(ctx, m_itemName); + auto const [path, err] = typeMaker.write(sctx, m_itemName); if (err) { oxLogError(err); return; diff --git a/src/olympic/studio/applib/src/newmenu.hpp b/src/olympic/studio/applib/src/newmenu.hpp index cf2ef516..2852ddc9 100644 --- a/src/olympic/studio/applib/src/newmenu.hpp +++ b/src/olympic/studio/applib/src/newmenu.hpp @@ -43,7 +43,7 @@ class NewMenu: public studio::Popup { [[nodiscard]] bool isOpen() const noexcept override; - void draw(turbine::Context &ctx) noexcept override; + void draw(studio::StudioContext &sctx) noexcept override; template void addItemType(ox::String name, ox::String parentDir, ox::String fileExt, T itemTempl, ox::ClawFormat pFmt = ox::ClawFormat::Metal) noexcept; @@ -54,15 +54,15 @@ class NewMenu: public studio::Popup { void addItemMaker(ox::UniquePtr &&im) noexcept; private: - void drawNewItemType(turbine::Context &ctx) noexcept; + void drawNewItemType(studio::StudioContext &sctx) noexcept; - void drawNewItemName(turbine::Context &ctx) noexcept; + void drawNewItemName(studio::StudioContext &sctx) noexcept; void drawFirstPageButtons() noexcept; - void drawLastPageButtons(turbine::Context &ctx) noexcept; + void drawLastPageButtons(studio::StudioContext &sctx) noexcept; - void finish(turbine::Context &ctx) noexcept; + void finish(studio::StudioContext &sctx) noexcept; }; diff --git a/src/olympic/studio/applib/src/newproject.cpp b/src/olympic/studio/applib/src/newproject.cpp index 647dd25c..284810ad 100644 --- a/src/olympic/studio/applib/src/newproject.cpp +++ b/src/olympic/studio/applib/src/newproject.cpp @@ -32,7 +32,7 @@ bool NewProject::isOpen() const noexcept { return m_open; } -void NewProject::draw(turbine::Context &ctx) noexcept { +void NewProject::draw(studio::StudioContext &ctx) noexcept { switch (m_stage) { case Stage::Opening: ImGui::OpenPopup(title().c_str()); @@ -48,14 +48,14 @@ void NewProject::draw(turbine::Context &ctx) noexcept { } } -void NewProject::drawNewProjectName(turbine::Context &ctx) noexcept { - drawWindow(ctx, &m_open, [this, &ctx] { +void NewProject::drawNewProjectName(studio::StudioContext &sctx) noexcept { + drawWindow(sctx.tctx, &m_open, [this, &sctx] { ImGui::InputText("Name", m_projectName.data(), m_projectName.cap()); ImGui::Text("Path: %s", m_projectPath.c_str()); if (ImGui::Button("Browse")) { oxLogError(studio::chooseDirectory().moveTo(m_projectPath)); } - drawLastPageButtons(ctx); + drawLastPageButtons(sctx); }); } @@ -73,7 +73,7 @@ void NewProject::drawFirstPageButtons() noexcept { } } -void NewProject::drawLastPageButtons(turbine::Context&) noexcept { +void NewProject::drawLastPageButtons(studio::StudioContext&) noexcept { ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - 95); ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetContentRegionAvail().y - 20); if (ImGui::Button("Finish")) { diff --git a/src/olympic/studio/applib/src/newproject.hpp b/src/olympic/studio/applib/src/newproject.hpp index bd08776f..6aad38e0 100644 --- a/src/olympic/studio/applib/src/newproject.hpp +++ b/src/olympic/studio/applib/src/newproject.hpp @@ -42,14 +42,14 @@ class NewProject: public studio::Popup { [[nodiscard]] bool isOpen() const noexcept override; - void draw(turbine::Context &ctx) noexcept override; + void draw(studio::StudioContext &ctx) noexcept override; private: - void drawNewProjectName(turbine::Context &ctx) noexcept; + void drawNewProjectName(studio::StudioContext &ctx) noexcept; void drawFirstPageButtons() noexcept; - void drawLastPageButtons(turbine::Context &ctx) noexcept; + void drawLastPageButtons(studio::StudioContext &ctx) noexcept; void finish() noexcept; diff --git a/src/olympic/studio/applib/src/projectexplorer.cpp b/src/olympic/studio/applib/src/projectexplorer.cpp index 3cc779e5..8961c4c0 100644 --- a/src/olympic/studio/applib/src/projectexplorer.cpp +++ b/src/olympic/studio/applib/src/projectexplorer.cpp @@ -37,12 +37,12 @@ static ox::Result> buildProjectTreeModel( ProjectExplorer::ProjectExplorer(turbine::Context &ctx) noexcept: m_ctx(ctx) { } -void ProjectExplorer::draw(turbine::Context &ctx) noexcept { +void ProjectExplorer::draw(studio::StudioContext &ctx) noexcept { auto const viewport = ImGui::GetContentRegionAvail(); ImGui::BeginChild("ProjectExplorer", ImVec2(300, viewport.y), true); ImGui::SetNextItemOpen(true); if (m_treeModel) { - m_treeModel->draw(ctx); + m_treeModel->draw(ctx.tctx); } ImGui::EndChild(); } diff --git a/src/olympic/studio/applib/src/projectexplorer.hpp b/src/olympic/studio/applib/src/projectexplorer.hpp index 50051f69..b7c15a45 100644 --- a/src/olympic/studio/applib/src/projectexplorer.hpp +++ b/src/olympic/studio/applib/src/projectexplorer.hpp @@ -19,7 +19,7 @@ class ProjectExplorer: public studio::Widget { public: explicit ProjectExplorer(turbine::Context &ctx) noexcept; - void draw(turbine::Context &ctx) noexcept override; + void draw(studio::StudioContext &ctx) noexcept override; void setModel(ox::UPtr &&model) noexcept; diff --git a/src/olympic/studio/applib/src/studioapp.cpp b/src/olympic/studio/applib/src/studioapp.cpp index 790e9944..d4b86789 100644 --- a/src/olympic/studio/applib/src/studioapp.cpp +++ b/src/olympic/studio/applib/src/studioapp.cpp @@ -159,15 +159,15 @@ void StudioUI::draw() noexcept { ImGui::Begin("MainWindow##Studio", nullptr, windowFlags); { if (m_showProjectExplorer) { - m_projectExplorer.draw(m_ctx); + m_projectExplorer.draw(m_sctx); ImGui::SameLine(); } drawTabBar(); for (auto &w: m_widgets) { - w->draw(m_ctx); + w->draw(m_sctx); } for (auto p: m_popups) { - p->draw(m_ctx); + p->draw(m_sctx); } } ImGui::End(); @@ -261,7 +261,7 @@ void StudioUI::drawTabs() noexcept { m_activeEditor->onActivated(); turbine::setConstantRefresh(m_ctx, m_activeEditor->requiresConstantRefresh()); } - e->draw(m_ctx); + e->draw(m_sctx); m_activeEditorOnLastDraw = e.get(); ImGui::EndTabItem(); } diff --git a/src/olympic/studio/modlib/include/studio/itemmaker.hpp b/src/olympic/studio/modlib/include/studio/itemmaker.hpp index 2102f3d0..1f7bf10d 100644 --- a/src/olympic/studio/modlib/include/studio/itemmaker.hpp +++ b/src/olympic/studio/modlib/include/studio/itemmaker.hpp @@ -18,7 +18,8 @@ class ItemMaker { ox::String const typeName; ox::String const parentDir; ox::String const fileExt; - constexpr explicit ItemMaker(ox::StringView pName, ox::StringView pParentDir, ox::CRStringView pFileExt) noexcept: + constexpr explicit ItemMaker( + ox::StringView pName, ox::StringView pParentDir, ox::CRStringView pFileExt) noexcept: typeName(pName), parentDir(pParentDir), fileExt(pFileExt) { @@ -36,7 +37,8 @@ class ItemMaker { * @param pName * @return path of file or error in Result */ - virtual ox::Result write(turbine::Context &ctx, ox::CRStringView pName) const noexcept = 0; + virtual ox::Result write( + studio::StudioContext &ctx, ox::CRStringView pName) const noexcept = 0; }; template @@ -73,11 +75,10 @@ class ItemMakerT: public ItemMaker { m_item(std::move(pItem)), m_fmt(pFmt) { } - ox::Result write(turbine::Context &ctx, ox::CRStringView pName) const noexcept override { + ox::Result write(studio::StudioContext &sctx, ox::CRStringView pName) const noexcept override { auto const path = itemPath(pName); - auto const sctx = turbine::applicationData(ctx); - keel::createUuidMapping(keelCtx(ctx), path, ox::UUID::generate().unwrap()); - oxReturnError(sctx->project->writeObj(path, m_item, m_fmt)); + keel::createUuidMapping(keelCtx(sctx.tctx), path, ox::UUID::generate().unwrap()); + oxReturnError(sctx.project->writeObj(path, m_item, m_fmt)); return path; } }; diff --git a/src/olympic/studio/modlib/include/studio/popup.hpp b/src/olympic/studio/modlib/include/studio/popup.hpp index d9a7aff9..1fb0891c 100644 --- a/src/olympic/studio/modlib/include/studio/popup.hpp +++ b/src/olympic/studio/modlib/include/studio/popup.hpp @@ -32,7 +32,7 @@ class Popup { [[nodiscard]] virtual bool isOpen() const noexcept = 0; - virtual void draw(turbine::Context &ctx) noexcept = 0; + virtual void draw(studio::StudioContext &ctx) noexcept = 0; protected: constexpr void setSize(ox::Size sz) noexcept { diff --git a/src/olympic/studio/modlib/include/studio/widget.hpp b/src/olympic/studio/modlib/include/studio/widget.hpp index 54365bd1..0b08ddfd 100644 --- a/src/olympic/studio/modlib/include/studio/widget.hpp +++ b/src/olympic/studio/modlib/include/studio/widget.hpp @@ -8,12 +8,14 @@ #include +#include "context.hpp" + namespace studio { class Widget: public ox::SignalHandler { public: ~Widget() noexcept override = default; - virtual void draw(turbine::Context&) noexcept = 0; + virtual void draw(studio::StudioContext&) noexcept = 0; }; } From 3fa247e3d4aa0df49414a0fa44b37101d780e2e7 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 23 Mar 2024 16:52:43 -0500 Subject: [PATCH 03/16] [ox/std] Add AnyPtr --- deps/ox/src/ox/std/anyptr.hpp | 73 +++++++++++++++++++++++++++++++++++ deps/ox/src/ox/std/array.hpp | 1 - deps/ox/src/ox/std/span.hpp | 1 + deps/ox/src/ox/std/std.hpp | 1 + 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 deps/ox/src/ox/std/anyptr.hpp diff --git a/deps/ox/src/ox/std/anyptr.hpp b/deps/ox/src/ox/std/anyptr.hpp new file mode 100644 index 00000000..3a8729f1 --- /dev/null +++ b/deps/ox/src/ox/std/anyptr.hpp @@ -0,0 +1,73 @@ + +#pragma once + +#include "array.hpp" +#include "span.hpp" + +namespace ox { + +class AnyPtr { + private: + struct WrapBase { + virtual ~WrapBase() = default; + virtual WrapBase *copyTo(ox::Span s) noexcept = 0; + virtual operator bool() const noexcept = 0; + }; + + template + struct Wrap: public WrapBase { + T *data{}; + constexpr Wrap(T *pData) noexcept: data(pData) { + } + inline WrapBase *copyTo(ox::Span s) noexcept override { + return new(s.data()) Wrap{data}; + } + constexpr operator bool() const noexcept override { + return data != nullptr; + } + }; + + WrapBase *m_wrapPtr{}; + ox::Array)> m_wrapData; + public: + constexpr AnyPtr() noexcept = default; + template + inline AnyPtr(T *ptr) noexcept { + m_wrapPtr = new(m_wrapData.data()) Wrap(ptr); + } + inline AnyPtr(AnyPtr const&other) noexcept { + if (other) { + m_wrapPtr = other.m_wrapPtr->copyTo(m_wrapData); + } + } + template + inline AnyPtr &operator=(T *ptr) noexcept { + m_wrapPtr = new(m_wrapData.data()) Wrap(ptr); + return *this; + } + inline AnyPtr &operator=(AnyPtr const&ptr) noexcept { + if (this != &ptr) { + if (ptr) { + m_wrapPtr = ptr.m_wrapPtr->copyTo(m_wrapData); + } else { + m_wrapPtr = nullptr; + } + } + return *this; + } + constexpr operator bool() const noexcept { + return m_wrapPtr && *m_wrapPtr; + } + template + [[nodiscard]] + constexpr T *get() const noexcept { +#ifdef OX_BARE_METAL + auto const out = static_cast*>(m_wrapPtr); +#else + auto const out = dynamic_cast*>(m_wrapPtr); +#endif + return out->data; + } +}; + +} \ No newline at end of file diff --git a/deps/ox/src/ox/std/array.hpp b/deps/ox/src/ox/std/array.hpp index b18efc2b..bef35d09 100644 --- a/deps/ox/src/ox/std/array.hpp +++ b/deps/ox/src/ox/std/array.hpp @@ -13,7 +13,6 @@ #include "initializerlist.hpp" #include "iterator.hpp" #include "math.hpp" -#include "memory.hpp" #include "new.hpp" #include "types.hpp" #include "utility.hpp" diff --git a/deps/ox/src/ox/std/span.hpp b/deps/ox/src/ox/std/span.hpp index 1d0af4fb..da9272f8 100644 --- a/deps/ox/src/ox/std/span.hpp +++ b/deps/ox/src/ox/std/span.hpp @@ -8,6 +8,7 @@ #pragma once +#include "array.hpp" #include "bit.hpp" #include "iterator.hpp" #include "vector.hpp" diff --git a/deps/ox/src/ox/std/std.hpp b/deps/ox/src/ox/std/std.hpp index 70b110ea..0cf050d3 100644 --- a/deps/ox/src/ox/std/std.hpp +++ b/deps/ox/src/ox/std/std.hpp @@ -8,6 +8,7 @@ #pragma once +#include "anyptr.hpp" #include "array.hpp" #include "assert.hpp" #include "bit.hpp" From 1616ca7018e91e23e580065f6b1056fe116d70e0 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 23 Mar 2024 16:53:04 -0500 Subject: [PATCH 04/16] [turbine] Replace WrapPtr with ox::AnyPtr --- .../turbine/include/turbine/context.hpp | 67 ++----------------- src/olympic/turbine/src/gba/context.cpp | 6 +- src/olympic/turbine/src/gba/context.hpp | 2 +- src/olympic/turbine/src/glfw/context.cpp | 6 +- src/olympic/turbine/src/glfw/context.hpp | 2 +- 5 files changed, 13 insertions(+), 70 deletions(-) diff --git a/src/olympic/turbine/include/turbine/context.hpp b/src/olympic/turbine/include/turbine/context.hpp index 45cc5d0d..8f84e765 100644 --- a/src/olympic/turbine/include/turbine/context.hpp +++ b/src/olympic/turbine/include/turbine/context.hpp @@ -37,78 +37,21 @@ inline ox::FileSystem *rom(Context &ctx) noexcept { return keelCtx(ctx).rom.get(); } -struct WrapBase { - virtual ~WrapBase() = default; - virtual WrapBase *copyTo(ox::Span s) noexcept = 0; - virtual operator bool() const noexcept = 0; -}; - -template -struct Wrap: public WrapBase { - T *data{}; - Wrap(T *pData) noexcept: data(pData) { - } - WrapBase *copyTo(ox::Span s) noexcept override { - oxAssert(s.size() >= sizeof(Wrap), "too small buffer"); - return new(s.data()) Wrap{data}; - } - operator bool() const noexcept override { - return data != nullptr; - } -}; - -class WrapPtr { - private: - WrapBase *m_wrapPtr{}; - ox::Array)> m_wrapData; - public: - template - inline WrapPtr &operator=(T *ptr) noexcept { - m_wrapPtr = new(m_wrapData.data()) Wrap(ptr); - return *this; - } - inline WrapPtr &operator=(WrapBase &ptr) noexcept { - if (ptr) { - m_wrapPtr = ptr.copyTo(m_wrapData); - } else { - m_wrapPtr = nullptr; - } - return *this; - } - inline operator bool() const noexcept { - return m_wrapPtr && *m_wrapPtr; - } - [[nodiscard]] - inline WrapBase *getWrapBase() const noexcept { - return m_wrapPtr; - } -}; - -void setApplicationDataRaw(Context &ctx, WrapBase &applicationData) noexcept; +void setApplicationDataRaw(Context &ctx, ox::AnyPtr const&applicationData) noexcept; template void setApplicationData(Context &ctx, T *applicationData) noexcept { - Wrap w(applicationData); - setApplicationDataRaw(ctx, w); + setApplicationDataRaw(ctx, applicationData); } [[nodiscard]] -WrapBase *applicationDataRaw(Context &ctx) noexcept; +ox::AnyPtr &applicationDataRaw(Context &ctx) noexcept; template [[nodiscard]] T *applicationData(Context &ctx) noexcept { - auto const raw = applicationDataRaw(ctx); - if (!raw) [[unlikely]] { - return nullptr; - } -#ifdef OX_BARE_METAL - auto const out = static_cast*>(raw); -#else - auto const out = dynamic_cast*>(raw); -#endif - oxAssert(out, "Cast failed - wrong type"); - return out->data; + auto const ptr = applicationDataRaw(ctx); + return ptr.get(); } void setKeyEventHandler(Context &ctx, KeyEventHandler h) noexcept; diff --git a/src/olympic/turbine/src/gba/context.cpp b/src/olympic/turbine/src/gba/context.cpp index 5b5f1330..389e04c2 100644 --- a/src/olympic/turbine/src/gba/context.cpp +++ b/src/olympic/turbine/src/gba/context.cpp @@ -18,12 +18,12 @@ keel::Context &keelCtx(Context &ctx) noexcept { return ctx.keelCtx; } -void setApplicationDataRaw(Context &ctx, WrapBase &applicationData) noexcept { +void setApplicationDataRaw(Context &ctx, ox::AnyPtr const&applicationData) noexcept { ctx.applicationData = applicationData; } -WrapBase *applicationDataRaw(Context &ctx) noexcept { - return ctx.applicationData.getWrapBase(); +ox::AnyPtr &applicationDataRaw(Context &ctx) noexcept { + return ctx.applicationData; } } diff --git a/src/olympic/turbine/src/gba/context.hpp b/src/olympic/turbine/src/gba/context.hpp index 02b05209..36d0fc10 100644 --- a/src/olympic/turbine/src/gba/context.hpp +++ b/src/olympic/turbine/src/gba/context.hpp @@ -17,7 +17,7 @@ class Context { UpdateHandler updateHandler = [](Context&) -> int {return 0;}; keel::Context keelCtx; KeyEventHandler keyEventHandler = nullptr; - WrapPtr applicationData; + ox::AnyPtr applicationData; // GBA impl data ///////////////////////////////////////////////////////// bool running = true; diff --git a/src/olympic/turbine/src/glfw/context.cpp b/src/olympic/turbine/src/glfw/context.cpp index 30db3183..73775482 100644 --- a/src/olympic/turbine/src/glfw/context.cpp +++ b/src/olympic/turbine/src/glfw/context.cpp @@ -20,12 +20,12 @@ keel::Context &keelCtx(Context &ctx) noexcept { return ctx.keelCtx; } -void setApplicationDataRaw(Context &ctx, WrapBase &applicationData) noexcept { +void setApplicationDataRaw(Context &ctx, ox::AnyPtr const&applicationData) noexcept { ctx.applicationData = applicationData; } -WrapBase *applicationDataRaw(Context &ctx) noexcept { - return ctx.applicationData.getWrapBase(); +ox::AnyPtr &applicationDataRaw(Context &ctx) noexcept { + return ctx.applicationData; } } diff --git a/src/olympic/turbine/src/glfw/context.hpp b/src/olympic/turbine/src/glfw/context.hpp index 21d420ec..6a08a7db 100644 --- a/src/olympic/turbine/src/glfw/context.hpp +++ b/src/olympic/turbine/src/glfw/context.hpp @@ -16,7 +16,7 @@ class Context { UpdateHandler updateHandler = [](Context&) -> int {return 0;}; keel::Context keelCtx; KeyEventHandler keyEventHandler = nullptr; - WrapPtr applicationData; + ox::AnyPtr applicationData; // GLFW impl data //////////////////////////////////////////////////////// int uninterruptedRefreshes = 3; From 50f3479d10688592d2d9fecbe169608e38b65767 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 23 Mar 2024 17:01:49 -0500 Subject: [PATCH 05/16] [ox/std] Make AnyPtr constexpr --- deps/ox/src/ox/std/anyptr.hpp | 54 ++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/deps/ox/src/ox/std/anyptr.hpp b/deps/ox/src/ox/std/anyptr.hpp index 3a8729f1..c6611318 100644 --- a/deps/ox/src/ox/std/anyptr.hpp +++ b/deps/ox/src/ox/std/anyptr.hpp @@ -9,9 +9,9 @@ namespace ox { class AnyPtr { private: struct WrapBase { - virtual ~WrapBase() = default; - virtual WrapBase *copyTo(ox::Span s) noexcept = 0; - virtual operator bool() const noexcept = 0; + virtual constexpr ~WrapBase() = default; + virtual constexpr WrapBase *copyTo(ox::Span s) noexcept = 0; + virtual constexpr operator bool() const noexcept = 0; }; template @@ -19,8 +19,12 @@ class AnyPtr { T *data{}; constexpr Wrap(T *pData) noexcept: data(pData) { } - inline WrapBase *copyTo(ox::Span s) noexcept override { - return new(s.data()) Wrap{data}; + constexpr WrapBase *copyTo(ox::Span s) noexcept override { + if (std::is_constant_evaluated()) { + return new Wrap(data); + } else { + return new(s.data()) Wrap(data); + } } constexpr operator bool() const noexcept override { return data != nullptr; @@ -29,25 +33,46 @@ class AnyPtr { WrapBase *m_wrapPtr{}; ox::Array)> m_wrapData; + public: constexpr AnyPtr() noexcept = default; + template - inline AnyPtr(T *ptr) noexcept { - m_wrapPtr = new(m_wrapData.data()) Wrap(ptr); + constexpr AnyPtr(T *ptr) noexcept { + if (std::is_constant_evaluated()) { + m_wrapPtr = new Wrap(ptr); + } else { + m_wrapPtr = new(m_wrapData.data()) Wrap(ptr); + } } - inline AnyPtr(AnyPtr const&other) noexcept { + + constexpr AnyPtr(AnyPtr const&other) noexcept { if (other) { m_wrapPtr = other.m_wrapPtr->copyTo(m_wrapData); } } + + constexpr ~AnyPtr() noexcept { + if (std::is_constant_evaluated()) { + ox::safeDelete(m_wrapPtr); + } + } + template - inline AnyPtr &operator=(T *ptr) noexcept { - m_wrapPtr = new(m_wrapData.data()) Wrap(ptr); + constexpr AnyPtr &operator=(T *ptr) noexcept { + if (std::is_constant_evaluated()) { + ox::safeDelete(m_wrapPtr); + m_wrapPtr = new Wrap(ptr); + } else { + m_wrapPtr = new(m_wrapData.data()) Wrap(ptr); + } return *this; } - inline AnyPtr &operator=(AnyPtr const&ptr) noexcept { + + constexpr AnyPtr &operator=(AnyPtr const&ptr) noexcept { if (this != &ptr) { if (ptr) { + ox::safeDelete(m_wrapPtr); m_wrapPtr = ptr.m_wrapPtr->copyTo(m_wrapData); } else { m_wrapPtr = nullptr; @@ -55,18 +80,19 @@ class AnyPtr { } return *this; } + constexpr operator bool() const noexcept { return m_wrapPtr && *m_wrapPtr; } + template [[nodiscard]] constexpr T *get() const noexcept { #ifdef OX_BARE_METAL - auto const out = static_cast*>(m_wrapPtr); + return static_cast*>(m_wrapPtr)->data; #else - auto const out = dynamic_cast*>(m_wrapPtr); + return dynamic_cast*>(m_wrapPtr)->data; #endif - return out->data; } }; From 41e08d67aad06d4bc5da801788f09601ffc3fca2 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 23 Mar 2024 17:12:44 -0500 Subject: [PATCH 06/16] [turbine] Make keyEventHandler nodiscard --- src/olympic/turbine/include/turbine/context.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/olympic/turbine/include/turbine/context.hpp b/src/olympic/turbine/include/turbine/context.hpp index 8f84e765..ef288747 100644 --- a/src/olympic/turbine/include/turbine/context.hpp +++ b/src/olympic/turbine/include/turbine/context.hpp @@ -56,6 +56,7 @@ T *applicationData(Context &ctx) noexcept { void setKeyEventHandler(Context &ctx, KeyEventHandler h) noexcept; +[[nodiscard]] KeyEventHandler keyEventHandler(Context &ctx) noexcept; } From e3c74637db2acbe7b5953f5ba03a7a40e1a0f473 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 23 Mar 2024 17:19:33 -0500 Subject: [PATCH 07/16] [turbine] Make applicationData return const& --- src/olympic/turbine/include/turbine/context.hpp | 5 ++--- src/olympic/turbine/src/gba/context.cpp | 2 +- src/olympic/turbine/src/glfw/context.cpp | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/olympic/turbine/include/turbine/context.hpp b/src/olympic/turbine/include/turbine/context.hpp index ef288747..2108f082 100644 --- a/src/olympic/turbine/include/turbine/context.hpp +++ b/src/olympic/turbine/include/turbine/context.hpp @@ -45,13 +45,12 @@ void setApplicationData(Context &ctx, T *applicationData) noexcept { } [[nodiscard]] -ox::AnyPtr &applicationDataRaw(Context &ctx) noexcept; +ox::AnyPtr const&applicationDataRaw(Context &ctx) noexcept; template [[nodiscard]] T *applicationData(Context &ctx) noexcept { - auto const ptr = applicationDataRaw(ctx); - return ptr.get(); + return applicationDataRaw(ctx).get(); } void setKeyEventHandler(Context &ctx, KeyEventHandler h) noexcept; diff --git a/src/olympic/turbine/src/gba/context.cpp b/src/olympic/turbine/src/gba/context.cpp index 389e04c2..37a4c710 100644 --- a/src/olympic/turbine/src/gba/context.cpp +++ b/src/olympic/turbine/src/gba/context.cpp @@ -22,7 +22,7 @@ void setApplicationDataRaw(Context &ctx, ox::AnyPtr const&applicationData) noexc ctx.applicationData = applicationData; } -ox::AnyPtr &applicationDataRaw(Context &ctx) noexcept { +ox::AnyPtr const&applicationDataRaw(Context &ctx) noexcept { return ctx.applicationData; } diff --git a/src/olympic/turbine/src/glfw/context.cpp b/src/olympic/turbine/src/glfw/context.cpp index 73775482..ed83f007 100644 --- a/src/olympic/turbine/src/glfw/context.cpp +++ b/src/olympic/turbine/src/glfw/context.cpp @@ -24,7 +24,7 @@ void setApplicationDataRaw(Context &ctx, ox::AnyPtr const&applicationData) noexc ctx.applicationData = applicationData; } -ox::AnyPtr &applicationDataRaw(Context &ctx) noexcept { +ox::AnyPtr const&applicationDataRaw(Context &ctx) noexcept { return ctx.applicationData; } From 2c0e02277ce98003de144018693a89a3749df52b Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 24 Mar 2024 02:22:37 -0500 Subject: [PATCH 08/16] [ox/std] Add assert to AnyPtr::Wrap::copyTo to ensure sufficiently large buff --- deps/ox/src/ox/std/anyptr.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/deps/ox/src/ox/std/anyptr.hpp b/deps/ox/src/ox/std/anyptr.hpp index c6611318..ac11e484 100644 --- a/deps/ox/src/ox/std/anyptr.hpp +++ b/deps/ox/src/ox/std/anyptr.hpp @@ -1,7 +1,9 @@ #pragma once +#include "assert.hpp" #include "array.hpp" +#include "def.hpp" #include "span.hpp" namespace ox { @@ -20,6 +22,7 @@ class AnyPtr { constexpr Wrap(T *pData) noexcept: data(pData) { } constexpr WrapBase *copyTo(ox::Span s) noexcept override { + oxAssert(s.size() >= sizeof(Wrap), "too small buffer"); if (std::is_constant_evaluated()) { return new Wrap(data); } else { From a20d7fd92370770f68b2023bba696ef80104f05b Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 18 Apr 2024 19:23:32 -0500 Subject: [PATCH 09/16] [ox] Cleanup --- deps/ox/src/ox/clargs/clargs.cpp | 2 +- deps/ox/src/ox/claw/read.cpp | 8 ++-- deps/ox/src/ox/claw/write.hpp | 2 +- deps/ox/src/ox/fs/filesystem/directory.hpp | 2 +- deps/ox/src/ox/fs/filesystem/filelocation.cpp | 2 +- .../ox/src/ox/fs/filesystem/passthroughfs.cpp | 2 +- deps/ox/src/ox/fs/filesystem/pathiterator.cpp | 48 +++++++++---------- deps/ox/src/ox/fs/test/tests.cpp | 32 ++++++------- deps/ox/src/ox/mc/write.hpp | 2 +- deps/ox/src/ox/oc/read.cpp | 12 ++--- deps/ox/src/ox/oc/read.hpp | 2 +- deps/ox/src/ox/oc/write.cpp | 2 +- deps/ox/src/ox/std/CMakeLists.txt | 1 + deps/ox/src/ox/std/basestringview.hpp | 2 +- deps/ox/src/ox/std/bstring.hpp | 14 +++--- deps/ox/src/ox/std/cstrops.hpp | 22 +++++---- deps/ox/src/ox/std/fmt.hpp | 6 +-- deps/ox/src/ox/std/hashmap.hpp | 20 ++++---- deps/ox/src/ox/std/maybeview.hpp | 26 ++++++++++ deps/ox/src/ox/std/std.hpp | 1 + deps/ox/src/ox/std/string.hpp | 22 ++++----- deps/ox/src/ox/std/stringview.hpp | 22 +++------ deps/ox/src/ox/std/strops.cpp | 24 +++++----- deps/ox/src/ox/std/strops.hpp | 6 +-- deps/ox/src/ox/std/test/tests.cpp | 6 +-- deps/ox/src/ox/std/tracehook.cpp | 20 ++++---- deps/ox/src/ox/std/vector.hpp | 16 +++---- 27 files changed, 174 insertions(+), 150 deletions(-) create mode 100644 deps/ox/src/ox/std/maybeview.hpp diff --git a/deps/ox/src/ox/clargs/clargs.cpp b/deps/ox/src/ox/clargs/clargs.cpp index 9acdecbb..c6edb9c8 100644 --- a/deps/ox/src/ox/clargs/clargs.cpp +++ b/deps/ox/src/ox/clargs/clargs.cpp @@ -27,7 +27,7 @@ ClArgs::ClArgs(int argc, const char **args) noexcept { m_bools[arg] = false; } m_strings[arg] = val; - if (auto r = ox_atoi(val.c_str()); r.error == 0) { + if (auto r = ox::atoi(val.c_str()); r.error == 0) { m_ints[arg] = r.value; } ++i; diff --git a/deps/ox/src/ox/claw/read.cpp b/deps/ox/src/ox/claw/read.cpp index 2df7a1a9..9506cead 100644 --- a/deps/ox/src/ox/claw/read.cpp +++ b/deps/ox/src/ox/claw/read.cpp @@ -13,7 +13,7 @@ namespace ox { Result readClawHeader(const char *buff, std::size_t buffLen) noexcept { - const auto s1End = ox_strchr(buff, ';', buffLen); + const auto s1End = ox::strchr(buff, ';', buffLen); if (!s1End) { return OxError(1, "Could not read Claw header"); } @@ -22,7 +22,7 @@ Result readClawHeader(const char *buff, std::size_t buffLen) noexcep buff += s1Size + 1; buffLen -= s1Size + 1; - const auto s2End = ox_strchr(buff, ';', buffLen); + const auto s2End = ox::strchr(buff, ';', buffLen); if (!s2End) { return OxError(2, "Could not read Claw header"); } @@ -31,7 +31,7 @@ Result readClawHeader(const char *buff, std::size_t buffLen) noexcep buff += s2Size + 1; buffLen -= s2Size + 1; - const auto s3End = ox_strchr(buff, ';', buffLen); + const auto s3End = ox::strchr(buff, ';', buffLen); if (!s3End) { return OxError(3, "Could not read Claw header"); } @@ -49,7 +49,7 @@ Result readClawHeader(const char *buff, std::size_t buffLen) noexcep return OxError(4, "Claw format does not match any supported format/version combo"); } hdr.typeName = typeName; - if (auto r = ox_atoi(versionStr.c_str()); r.error == 0) { + if (auto r = ox::atoi(versionStr.c_str()); r.error == 0) { hdr.typeVersion = r.value; } hdr.data = buff; diff --git a/deps/ox/src/ox/claw/write.hpp b/deps/ox/src/ox/claw/write.hpp index e8ec3bf0..f2d45dcb 100644 --- a/deps/ox/src/ox/claw/write.hpp +++ b/deps/ox/src/ox/claw/write.hpp @@ -88,7 +88,7 @@ ox::Error writeClawHeader(Writer_c auto &writer, const T *t, ClawFormat fmt) noe oxReturnError(writer.put(';')); const auto tn = detail::getTypeVersion(t); if (tn > -1) { - oxReturnError(ox::itoa(tn, writer)); + oxReturnError(ox::writeItoa(tn, writer)); } oxReturnError(writer.put(';')); return {}; diff --git a/deps/ox/src/ox/fs/filesystem/directory.hpp b/deps/ox/src/ox/fs/filesystem/directory.hpp index 10407529..ed67b75f 100644 --- a/deps/ox/src/ox/fs/filesystem/directory.hpp +++ b/deps/ox/src/ox/fs/filesystem/directory.hpp @@ -48,7 +48,7 @@ struct OX_PACKED DirectoryEntry { auto d = data(); if (d.valid()) { d->inode = inode; - ox_strncpy(d->name, name, ox::min(bufferSize, static_cast(MaxFileNameLength))); + ox::strncpy(d->name, name, ox::min(bufferSize, static_cast(MaxFileNameLength))); return OxError(0); } return OxError(1); diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.cpp b/deps/ox/src/ox/fs/filesystem/filelocation.cpp index 88f9ee21..72238e01 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.cpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.cpp @@ -46,7 +46,7 @@ FileAddress &FileAddress::operator=(const FileAddress &other) noexcept { case FileAddressType::Path: { if (other.m_data.path) { - auto strSize = ox_strlen(other.m_data.path) + 1; + auto strSize = ox::strlen(other.m_data.path) + 1; m_data.path = new char[strSize]; ox_memcpy(m_data.path, other.m_data.path, strSize); } else { diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp index d9cd254a..e600e1be 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp @@ -186,7 +186,7 @@ Error PassThroughFS::writeFileInode(uint64_t, const void*, uint64_t, FileType) n } std::string_view PassThroughFS::stripSlash(StringView path) noexcept { - const auto pathLen = ox_strlen(path); + const auto pathLen = ox::strlen(path); for (auto i = 0u; i < pathLen && path[0] == '/'; i++) { path = substr(path, 1); } diff --git a/deps/ox/src/ox/fs/filesystem/pathiterator.cpp b/deps/ox/src/ox/fs/filesystem/pathiterator.cpp index d6edf192..87885d61 100644 --- a/deps/ox/src/ox/fs/filesystem/pathiterator.cpp +++ b/deps/ox/src/ox/fs/filesystem/pathiterator.cpp @@ -19,7 +19,7 @@ PathIterator::PathIterator(const char *path, std::size_t maxSize, std::size_t it m_iterator = iterator; } -PathIterator::PathIterator(const char *path): PathIterator(path, ox_strlen(path)) { +PathIterator::PathIterator(const char *path): PathIterator(path, ox::strlen(path)) { } PathIterator::PathIterator(CRStringView path): PathIterator(path.data(), path.bytes()) { @@ -29,10 +29,10 @@ PathIterator::PathIterator(CRStringView path): PathIterator(path.data(), path.by * @return 0 if no error */ Error PathIterator::dirPath(char *out, std::size_t outSize) { - const auto idx = ox_lastIndexOf(m_path, '/', m_maxSize); + const auto idx = ox::lastIndexOf(m_path, '/', m_maxSize); const auto size = static_cast(idx + 1); if (idx >= 0 && size < outSize) { - ox_memcpy(out, m_path, size); + ox::memcpy(out, m_path, size); out[size] = 0; return OxError(0); } else { @@ -44,12 +44,12 @@ Error PathIterator::dirPath(char *out, std::size_t outSize) { * @return 0 if no error */ Error PathIterator::fileName(char *out, std::size_t outSize) { - auto idx = ox_lastIndexOf(m_path, '/', m_maxSize); + auto idx = ox::lastIndexOf(m_path, '/', m_maxSize); if (idx >= 0) { idx++; // pass up the preceding / - std::size_t fileNameSize = static_cast(ox_strlen(&m_path[idx])); + std::size_t fileNameSize = static_cast(ox::strlen(&m_path[idx])); if (fileNameSize < outSize) { - ox_memcpy(out, &m_path[idx], fileNameSize); + ox::memcpy(out, &m_path[idx], fileNameSize); out[fileNameSize] = 0; return OxError(0); } else { @@ -67,8 +67,8 @@ Error PathIterator::get(char *pathOut, std::size_t pathOutSize) { oxTracef("ox.fs.PathIterator.get", "m_iterator ({}) >= m_maxSize ({})", m_iterator, m_maxSize); return OxError(1); } - if (!ox_strlen(&m_path[m_iterator])) { - oxTrace("ox.fs.PathIterator.get", "!ox_strlen(&m_path[m_iterator])"); + if (!ox::strlen(&m_path[m_iterator])) { + oxTrace("ox.fs.PathIterator.get", "!ox::strlen(&m_path[m_iterator])"); return OxError(1); } auto start = m_iterator; @@ -76,10 +76,10 @@ Error PathIterator::get(char *pathOut, std::size_t pathOutSize) { start++; } // end is at the next / - const char *substr = ox_strchr(&m_path[start], '/', m_maxSize - start); + const char *substr = ox::strchr(&m_path[start], '/', m_maxSize - start); // correct end if it is invalid, which happens if there is no next / if (!substr) { - substr = ox_strchr(&m_path[start], 0, m_maxSize - start); + substr = ox::strchr(&m_path[start], 0, m_maxSize - start); } const auto end = static_cast(substr - m_path); size = end - start; @@ -87,7 +87,7 @@ Error PathIterator::get(char *pathOut, std::size_t pathOutSize) { if (size >= pathOutSize || size == 0) { return OxError(1); } - ox_memcpy(pathOut, &m_path[start], size); + ox::memcpy(pathOut, &m_path[start], size); // truncate trailing / if (size && pathOut[size - 1] == '/') { size--; @@ -100,17 +100,17 @@ Error PathIterator::get(char *pathOut, std::size_t pathOutSize) { Error PathIterator::next(char *pathOut, std::size_t pathOutSize) { std::size_t size = 0; auto retval = OxError(1); - if (m_iterator < m_maxSize && ox_strlen(&m_path[m_iterator])) { + if (m_iterator < m_maxSize && ox::strlen(&m_path[m_iterator])) { retval = OxError(0); if (m_path[m_iterator] == '/') { m_iterator++; } const auto start = m_iterator; // end is at the next / - const char *substr = ox_strchr(&m_path[start], '/', m_maxSize - start); + const char *substr = ox::strchr(&m_path[start], '/', m_maxSize - start); // correct end if it is invalid, which happens if there is no next / if (!substr) { - substr = ox_strchr(&m_path[start], 0, m_maxSize - start); + substr = ox::strchr(&m_path[start], 0, m_maxSize - start); } const auto end = static_cast(substr - m_path); size = end - start; @@ -118,7 +118,7 @@ Error PathIterator::next(char *pathOut, std::size_t pathOutSize) { if (size >= pathOutSize) { return OxError(1); } - ox_memcpy(pathOut, &m_path[start], size); + ox::memcpy(pathOut, &m_path[start], size); } // truncate trailing / if (size && pathOut[size - 1] == '/') { @@ -147,17 +147,17 @@ Result PathIterator::nextSize() const { std::size_t size = 0; auto retval = OxError(1); auto it = m_iterator; - if (it < m_maxSize && ox_strlen(&m_path[it])) { + if (it < m_maxSize && ox::strlen(&m_path[it])) { retval = OxError(0); if (m_path[it] == '/') { it++; } const auto start = it; // end is at the next / - const char *substr = ox_strchr(&m_path[start], '/', m_maxSize - start); + const char *substr = ox::strchr(&m_path[start], '/', m_maxSize - start); // correct end if it is invalid, which happens if there is no next / if (!substr) { - substr = ox_strchr(&m_path[start], 0, m_maxSize - start); + substr = ox::strchr(&m_path[start], 0, m_maxSize - start); } const auto end = static_cast(substr - m_path); size = end - start; @@ -168,16 +168,16 @@ Result PathIterator::nextSize() const { bool PathIterator::hasNext() const { std::size_t size = 0; - if (m_iterator < m_maxSize && ox_strlen(&m_path[m_iterator])) { + if (m_iterator < m_maxSize && ox::strlen(&m_path[m_iterator])) { std::size_t start = m_iterator; if (m_path[start] == '/') { start++; } // end is at the next / - const char *substr = ox_strchr(&m_path[start], '/', m_maxSize - start); + const char *substr = ox::strchr(&m_path[start], '/', m_maxSize - start); // correct end if it is invalid, which happens if there is no next / if (!substr) { - substr = ox_strchr(&m_path[start], 0, m_maxSize - start); + substr = ox::strchr(&m_path[start], 0, m_maxSize - start); } const auto end = static_cast(substr - m_path); size = end - start; @@ -192,16 +192,16 @@ bool PathIterator::valid() const { PathIterator PathIterator::next() const { std::size_t size = 0; auto iterator = m_iterator; - if (iterator < m_maxSize && ox_strlen(&m_path[iterator])) { + if (iterator < m_maxSize && ox::strlen(&m_path[iterator])) { if (m_path[iterator] == '/') { iterator++; } const auto start = iterator; // end is at the next / - const char *substr = ox_strchr(&m_path[start], '/', m_maxSize - start); + const char *substr = ox::strchr(&m_path[start], '/', m_maxSize - start); // correct end if it is invalid, which happens if there is no next / if (!substr) { - substr = ox_strchr(&m_path[start], 0, m_maxSize - start); + substr = ox::strchr(&m_path[start], 0, m_maxSize - start); } const auto end = static_cast(substr - m_path); size = end - start; diff --git a/deps/ox/src/ox/fs/test/tests.cpp b/deps/ox/src/ox/fs/test/tests.cpp index be6fdb2c..154b0e0a 100644 --- a/deps/ox/src/ox/fs/test/tests.cpp +++ b/deps/ox/src/ox/fs/test/tests.cpp @@ -61,9 +61,9 @@ const std::map> tests = auto const path = ox::String("/usr/share/charset.gbag"); ox::PathIterator it(path.c_str(), path.len()); auto buff = static_cast(ox_alloca(path.len() + 1)); - oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "usr") == 0, "PathIterator shows wrong next"); - oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "share") == 0, "PathIterator shows wrong next"); - oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "charset.gbag") == 0, "PathIterator shows wrong next"); + oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "usr") == 0, "PathIterator shows wrong next"); + oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "share") == 0, "PathIterator shows wrong next"); + oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "charset.gbag") == 0, "PathIterator shows wrong next"); return OxError(0); } }, @@ -73,8 +73,8 @@ const std::map> tests = auto const path = ox::String("/usr/share/"); ox::PathIterator it(path.c_str(), path.len()); auto buff = static_cast(ox_alloca(path.len() + 1)); - oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "usr") == 0, "PathIterator shows wrong next"); - oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "share") == 0, "PathIterator shows wrong next"); + oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "usr") == 0, "PathIterator shows wrong next"); + oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "share") == 0, "PathIterator shows wrong next"); return OxError(0); } }, @@ -84,7 +84,7 @@ const std::map> tests = auto const path = ox::String("/"); ox::PathIterator it(path.c_str(), path.len()); auto buff = static_cast(ox_alloca(path.len() + 1)); - oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "\0") == 0, "PathIterator shows wrong next"); + oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "\0") == 0, "PathIterator shows wrong next"); return OxError(0); } }, @@ -94,9 +94,9 @@ const std::map> tests = auto const path = ox::String("usr/share/charset.gbag"); ox::PathIterator it(path.c_str(), path.len()); auto buff = static_cast(ox_alloca(path.len() + 1)); - oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "usr") == 0, "PathIterator shows wrong next"); - oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "share") == 0, "PathIterator shows wrong next"); - oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "charset.gbag") == 0, "PathIterator shows wrong next"); + oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "usr") == 0, "PathIterator shows wrong next"); + oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "share") == 0, "PathIterator shows wrong next"); + oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "charset.gbag") == 0, "PathIterator shows wrong next"); return OxError(0); } }, @@ -106,8 +106,8 @@ const std::map> tests = auto const path = ox::String("usr/share/"); ox::PathIterator it(path.c_str(), path.len()); auto buff = static_cast(ox_alloca(path.len() + 1)); - oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "usr") == 0, "PathIterator shows wrong next"); - oxAssert(it.next(buff, path.len()) == 0 && ox_strcmp(buff, "share") == 0, "PathIterator shows wrong next"); + oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "usr") == 0, "PathIterator shows wrong next"); + oxAssert(it.next(buff, path.len()) == 0 && ox::strcmp(buff, "share") == 0, "PathIterator shows wrong next"); return OxError(0); } }, @@ -117,7 +117,7 @@ const std::map> tests = auto const path = ox::String("/usr/share/charset.gbag"); ox::PathIterator it(path.c_str(), path.len()); auto buff = static_cast(ox_alloca(path.len() + 1)); - oxAssert(it.dirPath(buff, path.len()) == 0 && ox_strcmp(buff, "/usr/share/") == 0, "PathIterator shows incorrect dir path"); + oxAssert(it.dirPath(buff, path.len()) == 0 && ox::strcmp(buff, "/usr/share/") == 0, "PathIterator shows incorrect dir path"); return OxError(0); } }, @@ -127,7 +127,7 @@ const std::map> tests = auto const path = ox::String("/usr/share/charset.gbag"); ox::PathIterator it(path.c_str(), path.len()); auto buff = static_cast(ox_alloca(path.len() + 1)); - oxAssert(it.fileName(buff, path.len()) == 0 && ox_strcmp(buff, "charset.gbag") == 0, "PathIterator shows incorrect file name"); + oxAssert(it.fileName(buff, path.len()) == 0 && ox::strcmp(buff, "charset.gbag") == 0, "PathIterator shows incorrect file name"); return OxError(0); } }, @@ -135,7 +135,7 @@ const std::map> tests = "PathIterator::hasNext", [](ox::StringView) { const auto path = "/file1"; - ox::PathIterator it(path, ox_strlen(path)); + ox::PathIterator it(path, ox::strlen(path)); oxAssert(it.hasNext(), "PathIterator shows incorrect hasNext"); oxAssert(!it.next().hasNext(), "PathIterator shows incorrect hasNext"); return OxError(0); @@ -171,9 +171,9 @@ const std::map> tests = [](ox::StringView) { constexpr auto buffLen = 5000; constexpr auto str1 = "Hello, World!"; - constexpr auto str1Len = ox_strlen(str1) + 1; + constexpr auto str1Len = ox::strlen(str1) + 1; constexpr auto str2 = "Hello, Moon!"; - constexpr auto str2Len = ox_strlen(str2) + 1; + constexpr auto str2Len = ox::strlen(str2) + 1; auto list = new (ox_alloca(buffLen)) ox::ptrarith::NodeBuffer>(buffLen); oxAssert(ox::FileStore32::format(list, buffLen), "FileStore::format failed."); ox::FileStore32 fileStore(list, buffLen); diff --git a/deps/ox/src/ox/mc/write.hpp b/deps/ox/src/ox/mc/write.hpp index 8eb94808..b3453b6d 100644 --- a/deps/ox/src/ox/mc/write.hpp +++ b/deps/ox/src/ox/mc/write.hpp @@ -214,7 +214,7 @@ template constexpr Error MetalClawWriter::fieldCString(const char*, const char *const*val, std::size_t) noexcept { bool fieldSet = false; if (!m_unionIdx.has_value() || *m_unionIdx == m_field) { - const auto strLen = *val ? ox_strlen(*val) : 0; + const auto strLen = *val ? ox::strlen(*val) : 0; // write the length const auto strLenBuff = mc::encodeInteger(strLen); oxReturnError(m_writer.write(reinterpret_cast(strLenBuff.data), strLenBuff.length)); diff --git a/deps/ox/src/ox/oc/read.cpp b/deps/ox/src/ox/oc/read.cpp index 33a7d057..a6d634f5 100644 --- a/deps/ox/src/ox/oc/read.cpp +++ b/deps/ox/src/ox/oc/read.cpp @@ -15,7 +15,7 @@ namespace ox { OrganicClawReader::OrganicClawReader(const uint8_t *buff, std::size_t buffSize) { auto json = reinterpret_cast(buff); - auto jsonLen = ox_strnlen(json, buffSize); + auto jsonLen = ox::strnlen(json, buffSize); Json::CharReaderBuilder parserBuilder; auto parser = std::unique_ptr(parserBuilder.newCharReader()); if (!parser->parse(json, json + jsonLen, &m_json, nullptr)) { @@ -198,7 +198,7 @@ Error OrganicClawReader::fieldCString(const char *key, char *val, std::size_t bu if (strSize >= buffLen) { err = OxError(2, "String size exceeds capacity of destination"); } else { - ox_memcpy(data, begin, static_cast(strSize)); + ox::memcpy(data, begin, static_cast(strSize)); data[strSize] = 0; } } else { @@ -224,7 +224,7 @@ Error OrganicClawReader::fieldCString(const char *key, char **val) noexcept { const auto strSize = static_cast(end - begin); safeDelete(*val); *val = new char[strSize + 1]; - ox_memcpy(data, begin, static_cast(strSize)); + ox::memcpy(data, begin, static_cast(strSize)); data[strSize] = 0; } else { err = OxError(1, "Type mismatch"); @@ -242,7 +242,7 @@ Error OrganicClawReader::fieldCString(const char *key, char **val, std::size_t b if (jv.empty()) { auto data = val; if (data) { - data[0] = 0; + data[0] = nullptr; } } else if (jv.isString()) { jv.getString(&begin, &end); @@ -252,8 +252,8 @@ Error OrganicClawReader::fieldCString(const char *key, char **val, std::size_t b safeDelete(*val); *val = new char[strSize + 1]; } - ox_memcpy(data, begin, static_cast(strSize)); - data[strSize] = 0; + ox::memcpy(data, begin, static_cast(strSize)); + data[strSize] = nullptr; } else { err = OxError(1, "Type mismatch"); } diff --git a/deps/ox/src/ox/oc/read.hpp b/deps/ox/src/ox/oc/read.hpp index 5c16ff72..15ecb7e6 100644 --- a/deps/ox/src/ox/oc/read.hpp +++ b/deps/ox/src/ox/oc/read.hpp @@ -274,7 +274,7 @@ Result readOC(const char *json, std::size_t jsonLen) noexcept { template Result readOC(const char *json) noexcept { - return readOC(json, ox_strlen(json)); + return readOC(json, ox::strlen(json)); } template diff --git a/deps/ox/src/ox/oc/write.cpp b/deps/ox/src/ox/oc/write.cpp index 024e3545..ddec2ce4 100644 --- a/deps/ox/src/ox/oc/write.cpp +++ b/deps/ox/src/ox/oc/write.cpp @@ -27,7 +27,7 @@ Error OrganicClawWriter::fieldCString(const char *key, const char *const*val, in } Error OrganicClawWriter::fieldCString(const char *key, const char *const*val) noexcept { - return fieldCString(key, const_cast(val), static_cast(ox_strlen(val))); + return fieldCString(key, const_cast(val), static_cast(ox::strlen(val))); } Error OrganicClawWriter::field(const char *key, const UUID *uuid) noexcept { diff --git a/deps/ox/src/ox/std/CMakeLists.txt b/deps/ox/src/ox/std/CMakeLists.txt index 07b965bc..73c00578 100644 --- a/deps/ox/src/ox/std/CMakeLists.txt +++ b/deps/ox/src/ox/std/CMakeLists.txt @@ -105,6 +105,7 @@ install( heapmgr.hpp iterator.hpp math.hpp + maybeview.hpp memops.hpp memory.hpp new.hpp diff --git a/deps/ox/src/ox/std/basestringview.hpp b/deps/ox/src/ox/std/basestringview.hpp index e4ed55ce..b111f0e1 100644 --- a/deps/ox/src/ox/std/basestringview.hpp +++ b/deps/ox/src/ox/std/basestringview.hpp @@ -131,7 +131,7 @@ class BaseStringView { constexpr explicit BaseStringView(std::nullptr_t) noexcept {} - constexpr explicit BaseStringView(const char *str) noexcept: m_str(str), m_len(str ? ox_strlen(str) : 0) {} + constexpr explicit BaseStringView(const char *str) noexcept: m_str(str), m_len(str ? ox::strlen(str) : 0) {} constexpr explicit BaseStringView(const char *str, std::size_t len) noexcept: m_str(str), m_len(len) {} diff --git a/deps/ox/src/ox/std/bstring.hpp b/deps/ox/src/ox/std/bstring.hpp index 1dd5ada5..7a450ede 100644 --- a/deps/ox/src/ox/std/bstring.hpp +++ b/deps/ox/src/ox/std/bstring.hpp @@ -109,7 +109,7 @@ constexpr BString::BString(const char *str) noexcept: m_buff{{0}} { template constexpr BString &BString::operator=(Integer_c auto i) noexcept { char str[65] = {}; - ox_itoa(i, str); + ox::itoa(i, str); return this->operator=(str); } @@ -127,7 +127,7 @@ constexpr BString &BString::operator=(ox::CRStringView str) noexcept template constexpr BString &BString::operator=(const char *str) noexcept { - std::size_t strLen = ox_strlen(str) + 1; + std::size_t strLen = ox::strlen(str) + 1; if (cap() < strLen) { strLen = cap(); } @@ -144,7 +144,7 @@ constexpr BString &BString::operator=(char *str) noexcept { template constexpr BString &BString::operator+=(const char *str) noexcept { - std::size_t strLen = ox_strlen(str) + 1; + std::size_t strLen = ox::strlen(str) + 1; oxIgnoreError(append(str, strLen)); return *this; } @@ -157,7 +157,7 @@ constexpr BString &BString::operator+=(char *str) noexcept { template constexpr BString &BString::operator+=(Integer_c auto i) noexcept { char str[65] = {}; - ox_itoa(i, str); + ox::itoa(i, str); return this->operator+=(str); } @@ -171,7 +171,7 @@ constexpr BString &BString::operator+=(StringView s) noexcept { template constexpr BString BString::operator+(const char *str) const noexcept { auto out = *this; - std::size_t strLen = ox_strlen(str) + 1; + std::size_t strLen = ox::strlen(str) + 1; oxIgnoreError(out.append(str, strLen)); return out; } @@ -184,7 +184,7 @@ constexpr BString BString::operator+(char *str) const noexcept { template constexpr BString BString::operator+(Integer_c auto i) const noexcept { char str[65] = {}; - ox_itoa(i, str); + ox::itoa(i, str); return this->operator+(str); } @@ -226,7 +226,7 @@ constexpr Error BString::append(const char *str, std::size_t strLen) no strLen = cap() - currentLen; err = OxError(1, "Insufficient space for full string"); } - ox_strncpy(m_buff + currentLen, str, strLen); + ox::strncpy(m_buff + currentLen, str, strLen); // make sure last element is a null terminator m_buff[currentLen + strLen] = 0; return err; diff --git a/deps/ox/src/ox/std/cstrops.hpp b/deps/ox/src/ox/std/cstrops.hpp index d0d26ffe..4b5b02c1 100644 --- a/deps/ox/src/ox/std/cstrops.hpp +++ b/deps/ox/src/ox/std/cstrops.hpp @@ -11,8 +11,10 @@ #include "types.hpp" #include "typetraits.hpp" +namespace ox { + template -constexpr T1 ox_strncpy(T1 dest, T2 src, std::size_t maxLen) noexcept { +constexpr T1 strncpy(T1 dest, T2 src, std::size_t maxLen) noexcept { using T1Type = typename ox::remove_reference::type; std::size_t i = 0; while (i < maxLen && src[i]) { @@ -25,7 +27,7 @@ constexpr T1 ox_strncpy(T1 dest, T2 src, std::size_t maxLen) noexcept { } [[nodiscard]] -constexpr auto ox_strnlen(const char *str1, std::size_t maxLen) noexcept { +constexpr auto strnlen(const char *str1, std::size_t maxLen) noexcept { std::size_t len = 0; for (; len < maxLen && str1[len]; len++); return len; @@ -33,7 +35,7 @@ constexpr auto ox_strnlen(const char *str1, std::size_t maxLen) noexcept { template [[nodiscard]] -constexpr auto ox_strlen(T const&str1) noexcept { +constexpr auto strlen(T const&str1) noexcept { std::size_t len = 0; for (; str1[len]; len++); return len; @@ -41,7 +43,7 @@ constexpr auto ox_strlen(T const&str1) noexcept { template [[nodiscard]] -constexpr int ox_strcmp(const T1 &str1, const T2 &str2) noexcept { +constexpr int strcmp(const T1 &str1, const T2 &str2) noexcept { auto retval = 0; auto i = 0u; while (str1[i] || str2[i]) { @@ -59,7 +61,7 @@ constexpr int ox_strcmp(const T1 &str1, const T2 &str2) noexcept { template [[nodiscard]] -constexpr int ox_strncmp(T1 const&str1, T2 const&str2, const std::size_t maxLen) noexcept { +constexpr int strncmp(T1 const&str1, T2 const&str2, const std::size_t maxLen) noexcept { auto retval = 0; std::size_t i = 0; while (i < maxLen && (str1[i] || str2[i])) { @@ -76,7 +78,7 @@ constexpr int ox_strncmp(T1 const&str1, T2 const&str2, const std::size_t maxLen) } [[nodiscard]] -constexpr const char *ox_strchr(const char *str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept { +constexpr const char *strchr(const char *str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept { for (std::size_t i = 0; i <= maxLen; i++) { if (str[i] == character) { return &str[i]; @@ -88,7 +90,7 @@ constexpr const char *ox_strchr(const char *str, int character, std::size_t maxL } [[nodiscard]] -constexpr char *ox_strchr(char *str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept { +constexpr char *strchr(char *str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept { for (std::size_t i = 0; i < maxLen; i++) { if (str[i] == character) { return &str[i]; @@ -100,7 +102,7 @@ constexpr char *ox_strchr(char *str, int character, std::size_t maxLen = 0xFFFFF } [[nodiscard]] -constexpr int ox_lastIndexOf(const auto &str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept { +constexpr int lastIndexOf(const auto &str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept { int retval = -1; for (std::size_t i = 0; i < maxLen && str[i]; i++) { if (str[i] == character) { @@ -111,7 +113,7 @@ constexpr int ox_lastIndexOf(const auto &str, int character, std::size_t maxLen } template -constexpr T ox_itoa(Integer v, T str) noexcept { +constexpr T itoa(Integer v, T str) noexcept { if (v) { ox::ResizedInt_t mod = 1000000000000000000; ox::ResizedInt_t val = v; @@ -143,3 +145,5 @@ constexpr T ox_itoa(Integer v, T str) noexcept { } return str; } + +} diff --git a/deps/ox/src/ox/std/fmt.hpp b/deps/ox/src/ox/std/fmt.hpp index a3717045..b650bbef 100644 --- a/deps/ox/src/ox/std/fmt.hpp +++ b/deps/ox/src/ox/std/fmt.hpp @@ -81,7 +81,7 @@ class FmtArg { if constexpr(is_bool_v) { return v ? "true" : "false"; } else if constexpr(is_integer_v) { - return ox_itoa(v, dataStr); + return ox::itoa(v, dataStr); } else { return toStringView(v); } @@ -126,11 +126,11 @@ struct FmtSegment { unsigned length = 0; constexpr bool operator==(const FmtSegment &o) const noexcept { - return length == o.length && ox_strncmp(str, o.str, length) == 0; + return length == o.length && ox::strncmp(str, o.str, length) == 0; } constexpr bool operator!=(const FmtSegment &o) const noexcept { - return length != o.length || ox_strncmp(str, o.str, length) != 0; + return length != o.length || ox::strncmp(str, o.str, length) != 0; } }; diff --git a/deps/ox/src/ox/std/hashmap.hpp b/deps/ox/src/ox/std/hashmap.hpp index 868f5ae5..37658149 100644 --- a/deps/ox/src/ox/std/hashmap.hpp +++ b/deps/ox/src/ox/std/hashmap.hpp @@ -45,16 +45,16 @@ class HashMap { constexpr HashMap &operator=(HashMap &&other) noexcept; - constexpr T &operator[](MaybeSV_t const&key); + constexpr T &operator[](MaybeView_t const&key); - constexpr Result at(MaybeSV_t const&key) noexcept; + constexpr Result at(MaybeView_t const&key) noexcept; - constexpr Result at(MaybeSV_t const&key) const noexcept; + constexpr Result at(MaybeView_t const&key) const noexcept; - constexpr void erase(MaybeSV_t const&key); + constexpr void erase(MaybeView_t const&key); [[nodiscard]] - constexpr bool contains(MaybeSV_t const&key) const noexcept; + constexpr bool contains(MaybeView_t const&key) const noexcept; [[nodiscard]] constexpr std::size_t size() const noexcept; @@ -134,7 +134,7 @@ constexpr HashMap &HashMap::operator=(HashMap &&other) noexcep } template -constexpr T &HashMap::operator[](MaybeSV_t const&k) { +constexpr T &HashMap::operator[](MaybeView_t const&k) { auto &p = access(m_pairs, k); if (p == nullptr) { if (static_cast(m_pairs.size()) * 0.7 < @@ -149,7 +149,7 @@ constexpr T &HashMap::operator[](MaybeSV_t const&k) { } template -constexpr Result HashMap::at(MaybeSV_t const&k) noexcept { +constexpr Result HashMap::at(MaybeView_t const&k) noexcept { auto p = access(m_pairs, k); if (!p) { return {nullptr, OxError(1, "value not found for given key")}; @@ -158,7 +158,7 @@ constexpr Result HashMap::at(MaybeSV_t const&k) noexcept { } template -constexpr Result HashMap::at(MaybeSV_t const&k) const noexcept { +constexpr Result HashMap::at(MaybeView_t const&k) const noexcept { auto p = access(m_pairs, k); if (!p) { return {nullptr, OxError(1, "value not found for given key")}; @@ -167,7 +167,7 @@ constexpr Result HashMap::at(MaybeSV_t const&k) const noexcep } template -constexpr void HashMap::erase(MaybeSV_t const&k) { +constexpr void HashMap::erase(MaybeView_t const&k) { if (!contains(k)) { return; } @@ -185,7 +185,7 @@ constexpr void HashMap::erase(MaybeSV_t const&k) { } template -constexpr bool HashMap::contains(MaybeSV_t const&k) const noexcept { +constexpr bool HashMap::contains(MaybeView_t const&k) const noexcept { return access(m_pairs, k) != nullptr; } diff --git a/deps/ox/src/ox/std/maybeview.hpp b/deps/ox/src/ox/std/maybeview.hpp new file mode 100644 index 00000000..e9992391 --- /dev/null +++ b/deps/ox/src/ox/std/maybeview.hpp @@ -0,0 +1,26 @@ +/* + * Copyright 2015 - 2022 gary@drinkingtea.net + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include "typetraits.hpp" + +namespace ox { + +// Maybe StringView. If T is a string type, MaybeType::type/MaybeView_t is a +// StringView. This avoids creating unnecessary Strings when taking a +// StringView or C string as a function argument. +template> +struct MaybeView { + using type = T; +}; + +template +using MaybeView_t = typename MaybeView::type; + +} diff --git a/deps/ox/src/ox/std/std.hpp b/deps/ox/src/ox/std/std.hpp index 0cf050d3..9f55f501 100644 --- a/deps/ox/src/ox/std/std.hpp +++ b/deps/ox/src/ox/std/std.hpp @@ -28,6 +28,7 @@ #include "heapmgr.hpp" #include "iterator.hpp" #include "math.hpp" +#include "maybeview.hpp" #include "memops.hpp" #include "memory.hpp" #include "new.hpp" diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index 772932de..e2604310 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -315,7 +315,7 @@ constexpr BasicString &BasicString::operat template constexpr BasicString &BasicString::operator=(int64_t i) noexcept { ox::Array str{}; - ox_itoa(i, str.data()); + ox::itoa(i, str.data()); set(str.data()); return *this; } @@ -323,7 +323,7 @@ constexpr BasicString &BasicString::operat template constexpr BasicString &BasicString::operator=(uint64_t i) noexcept { ox::Array str{}; - ox_itoa(i, str.data()); + ox::itoa(i, str.data()); set(str.data()); return *this; } @@ -352,7 +352,7 @@ constexpr BasicString &BasicString::operat template constexpr BasicString &BasicString::operator+=(const char *str) noexcept { - std::size_t strLen = ox_strlen(str); + std::size_t strLen = ox::strlen(str); oxIgnoreError(append(str, strLen)); return *this; } @@ -371,7 +371,7 @@ constexpr BasicString &BasicString::operat template constexpr BasicString &BasicString::operator+=(Integer_c auto i) noexcept { char str[65] = {}; - ox_itoa(i, str); + ox::itoa(i, str); return this->operator+=(str); } @@ -390,7 +390,7 @@ constexpr BasicString &BasicString::operat template constexpr BasicString BasicString::operator+(const char *str) const noexcept { - const std::size_t strLen = ox_strlen(str); + const std::size_t strLen = ox::strlen(str); const auto currentLen = len(); BasicString cpy(currentLen + strLen); cpy.m_buff.resize(m_buff.size() + strLen); @@ -415,7 +415,7 @@ constexpr BasicString BasicString::operato template constexpr BasicString BasicString::operator+(Integer_c auto i) const noexcept { char str[65] = {}; - ox_itoa(i, str); + ox::itoa(i, str); return *this + str; } @@ -470,22 +470,22 @@ constexpr bool BasicString::operator!=(OxString_c auto const& template constexpr bool BasicString::operator<(BasicString const&other) const noexcept { - return ox_strcmp(c_str(), other.c_str()) < 0; + return ox::strcmp(c_str(), other.c_str()) < 0; } template constexpr bool BasicString::operator>(BasicString const&other) const noexcept { - return ox_strcmp(c_str(), other.c_str()) > 0; + return ox::strcmp(c_str(), other.c_str()) > 0; } template constexpr bool BasicString::operator<=(BasicString const&other) const noexcept { - return ox_strcmp(c_str(), other.c_str()) < 1; + return ox::strcmp(c_str(), other.c_str()) < 1; } template constexpr bool BasicString::operator>=(BasicString const&other) const noexcept { - return ox_strcmp(c_str(), other.c_str()) > -1; + return ox::strcmp(c_str(), other.c_str()) > -1; } template @@ -548,7 +548,7 @@ constexpr void BasicString::set(CRStringView str) noexcept { template constexpr void BasicString::set(const char8_t *str) noexcept { - std::size_t strBytes = ox_strlen(str) + 1; + std::size_t strBytes = ox::strlen(str) + 1; m_buff.resize(strBytes); memcpy(m_buff.data(), str, strBytes); *m_buff.back().value = 0; diff --git a/deps/ox/src/ox/std/stringview.hpp b/deps/ox/src/ox/std/stringview.hpp index 46ff5b46..06ca90fa 100644 --- a/deps/ox/src/ox/std/stringview.hpp +++ b/deps/ox/src/ox/std/stringview.hpp @@ -14,6 +14,7 @@ #include "basestringview.hpp" #include "cstrops.hpp" +#include "maybeview.hpp" #include "writer.hpp" namespace ox { @@ -63,7 +64,7 @@ constexpr auto operator==(CRStringView s1, CRStringView s2) noexcept { if (s2.len() != s1.len()) { return false; } - return ox_strncmp(s1.data(), s2.data(), s1.len()) == 0; + return ox::strncmp(s1.data(), s2.data(), s1.len()) == 0; } constexpr auto operator<=>(CRStringView s1, CRStringView s2) noexcept { @@ -97,24 +98,12 @@ constexpr auto toStdStringView(CRStringView sv) noexcept { #endif -// Maybe StringView. If T is a string type, MaybeType::type/MaybeSV_t is a -// StringView. This avoids creating unnecessary Strings when taking a -// StringView or C string as a function argument. -template> -struct MaybeSV { - using type = T; -}; template -struct MaybeSV { +struct MaybeView { using type = ox::StringView; }; -template -using MaybeSV_t = typename MaybeSV::type; - -} - -constexpr ox::Result ox_atoi(ox::CRStringView str) noexcept { +constexpr ox::Result atoi(ox::CRStringView str) noexcept { int total = 0; int multiplier = 1; for (auto i = static_cast(str.len()) - 1; i != -1; --i) { @@ -129,3 +118,6 @@ constexpr ox::Result ox_atoi(ox::CRStringView str) noexcept { return total; } + +} + diff --git a/deps/ox/src/ox/std/strops.cpp b/deps/ox/src/ox/std/strops.cpp index 532c3bfe..6396ddf4 100644 --- a/deps/ox/src/ox/std/strops.cpp +++ b/deps/ox/src/ox/std/strops.cpp @@ -8,26 +8,26 @@ #include "strops.hpp" -static_assert(ox_strcmp("asdf", "hijk") < 0, "asdf < hijk"); -static_assert(ox_strcmp("hijk", "asdf") > 0, "hijk > asdf"); -static_assert(ox_strcmp("resize", "read") > 0, "resize > read"); -static_assert(ox_strcmp("read", "resize") < 0, "read < resize"); -static_assert(ox_strcmp("resize", "resize") == 0, "resize == resize"); -static_assert(ox_strcmp("", "") == 0, "\"\" == \"\""); +static_assert(ox::strcmp("asdf", "hijk") < 0, "asdf < hijk"); +static_assert(ox::strcmp("hijk", "asdf") > 0, "hijk > asdf"); +static_assert(ox::strcmp("resize", "read") > 0, "resize > read"); +static_assert(ox::strcmp("read", "resize") < 0, "read < resize"); +static_assert(ox::strcmp("resize", "resize") == 0, "resize == resize"); +static_assert(ox::strcmp("", "") == 0, "\"\" == \"\""); static_assert([] { auto testStr = "asdf"; - return ox_strchr(testStr, 0, 4) == &testStr[4]; -}(), "ox_strchr 0"); + return ox::strchr(testStr, 0, 4) == &testStr[4]; +}(), "ox::strchr 0"); static_assert([] { int retval = 0; auto testStr = "aaaa"; - // test the const and non-const versions of ox_lastIndexOf - retval |= !(ox_lastIndexOf(const_cast(testStr), 'a', ox_strlen(testStr)) == 3); - retval |= !(ox_lastIndexOf(testStr, 'a', ox_strlen(testStr)) == 3); + // test the const and non-const versions of ox::lastIndexOf + retval |= !(ox::lastIndexOf(const_cast(testStr), 'a', ox::strlen(testStr)) == 3); + retval |= !(ox::lastIndexOf(testStr, 'a', ox::strlen(testStr)) == 3); return retval == 0; -}(), "ox_lastIndexOf aaaa a"); +}(), "ox::lastIndexOf aaaa a"); #ifndef OX_USE_STDLIB diff --git a/deps/ox/src/ox/std/strops.hpp b/deps/ox/src/ox/std/strops.hpp index d3236d87..84be901f 100644 --- a/deps/ox/src/ox/std/strops.hpp +++ b/deps/ox/src/ox/std/strops.hpp @@ -37,7 +37,7 @@ constexpr ox::StringView substr(Str const&str, std::size_t start, std::size_t en } template -constexpr ox::Error itoa(Integer v, ox::Writer_c auto &writer) noexcept { +constexpr ox::Error writeItoa(Integer v, ox::Writer_c auto &writer) noexcept { if (v) { ox::ResizedInt_t mod = 1000000000000000000; ox::ResizedInt_t val = v; @@ -71,13 +71,13 @@ constexpr ox::Error itoa(Integer v, ox::Writer_c auto &writer) noexcept { [[nodiscard]] constexpr bool beginsWith(CRStringView base, CRStringView beginning) noexcept { const auto beginningLen = ox::min(beginning.len(), base.len()); - return base.len() >= beginning.len() && ox_strncmp(base.data(), beginning, beginningLen) == 0; + return base.len() >= beginning.len() && ox::strncmp(base.data(), beginning, beginningLen) == 0; } [[nodiscard]] constexpr bool endsWith(CRStringView base, CRStringView ending) noexcept { const auto endingLen = ending.len(); - return base.len() >= endingLen && ox_strcmp(base.data() + (base.len() - endingLen), ending) == 0; + return base.len() >= endingLen && ox::strcmp(base.data() + (base.len() - endingLen), ending) == 0; } constexpr std::size_t find(CRStringView str, char search) noexcept { diff --git a/deps/ox/src/ox/std/test/tests.cpp b/deps/ox/src/ox/std/test/tests.cpp index b04ddf3e..3aeb015a 100644 --- a/deps/ox/src/ox/std/test/tests.cpp +++ b/deps/ox/src/ox/std/test/tests.cpp @@ -30,13 +30,13 @@ static std::map tests = { []() { ox::Array buff; ox::CharBuffWriter bw(buff); - oxAssert(ox::itoa(5, bw), "ox::itoa returned Error"); + oxAssert(ox::writeItoa(5, bw), "ox::writeItoa returned Error"); oxExpect(ox::StringView(buff.data()), ox::StringView("5")); oxReturnError(bw.seekp(0)); - oxAssert(ox::itoa(50, bw), "ox::itoa returned Error"); + oxAssert(ox::writeItoa(50, bw), "ox::writeItoa returned Error"); oxExpect(ox::StringView(buff.data()), ox::StringView("50")); oxReturnError(bw.seekp(0)); - oxAssert(ox::itoa(500, bw), "ox::itoa returned Error"); + oxAssert(ox::writeItoa(500, bw), "ox::writeItoa returned Error"); oxExpect(ox::StringView(buff.data()), ox::StringView("500")); return ox::Error{}; } diff --git a/deps/ox/src/ox/std/tracehook.cpp b/deps/ox/src/ox/std/tracehook.cpp index 7498954d..a0a0528d 100644 --- a/deps/ox/src/ox/std/tracehook.cpp +++ b/deps/ox/src/ox/std/tracehook.cpp @@ -42,7 +42,7 @@ enum LogChan { template static void log(ox::CRStringView str) { const auto sz = ox::min(0x100, str.bytes()); - ox_strncpy(REG_MGBA_DEBUG_STRING, str.data(), sz); + ox::strncpy(REG_MGBA_DEBUG_STRING, str.data(), sz); REG_MGBA_DEBUG_FLAGS = chan | 0x100; } @@ -69,30 +69,30 @@ void oxTraceHook([[maybe_unused]] const char *file, [[maybe_unused]] int line, std::cout << std::setw(53) << std::left << ch << "| "; std::cout << std::setw(65) << std::left << msg << '|'; std::cout << " " << file << ':' << line << "\n"; - } else if (ox_strcmp(ch, "debug") == 0 || ox_strcmp(ch, "info") == 0) { + } else if (ox::strcmp(ch, "debug") == 0 || ox::strcmp(ch, "info") == 0) { printf("%s\n", msg); fflush(stdout); - } else if (ox_strcmp(ch, "stdout") == 0) { + } else if (ox::strcmp(ch, "stdout") == 0) { printf("%s", msg); fflush(stdout); - } else if (ox_strcmp(ch, "stderr") == 0) { + } else if (ox::strcmp(ch, "stderr") == 0) { printf("%s", msg); fflush(stdout); - } else if (ox_strcmp(ch, "error") == 0) { + } else if (ox::strcmp(ch, "error") == 0) { //std::cerr << "\033[31;1;1mERROR:\033[0m (" << file << ':' << line << "): " << msg << '\n'; fprintf(stderr, "\033[31;1;1mERROR:\033[0m (%s:%d): %s\n", file, line, msg); fflush(stderr); } #else - if (ox_strcmp(ch, "info") == 0) { + if (ox::strcmp(ch, "info") == 0) { infoLog(msg); - } else if (ox_strcmp(ch, "debug") == 0) { + } else if (ox::strcmp(ch, "debug") == 0) { debugLog(msg); - } else if (ox_strcmp(ch, "stdout") == 0) { + } else if (ox::strcmp(ch, "stdout") == 0) { infoLog(msg); - } else if (ox_strcmp(ch, "stderr") == 0) { + } else if (ox::strcmp(ch, "stderr") == 0) { errorLog(msg); - } else if (ox_strcmp(ch, "error") == 0) { + } else if (ox::strcmp(ch, "error") == 0) { errorLog(msg); } #endif diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index 9ccd9aba..05f377ab 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -263,12 +263,12 @@ class Vector: detail::VectorAllocator { } [[nodiscard]] - constexpr bool contains(MaybeSV_t const&) const noexcept(useNoexcept); + constexpr bool contains(MaybeView_t const&) const noexcept(useNoexcept); constexpr iterator insert( - std::size_t pos, std::size_t cnt, MaybeSV_t const&val) noexcept(useNoexcept); + std::size_t pos, std::size_t cnt, MaybeView_t const&val) noexcept(useNoexcept); - constexpr iterator insert(std::size_t pos, MaybeSV_t const&val) noexcept(useNoexcept); + constexpr iterator insert(std::size_t pos, MaybeView_t const&val) noexcept(useNoexcept); template constexpr iterator emplace(std::size_t pos, Args&&... args) noexcept(useNoexcept); @@ -278,7 +278,7 @@ class Vector: detail::VectorAllocator { constexpr void push_back(T &&item) noexcept(useNoexcept); - constexpr void push_back(MaybeSV_t const&item) noexcept(useNoexcept); + constexpr void push_back(MaybeView_t const&item) noexcept(useNoexcept); constexpr void pop_back() noexcept(useNoexcept); @@ -516,7 +516,7 @@ constexpr void Vector::resize(std::size_t size) n } template -constexpr bool Vector::contains(MaybeSV_t const&v) const noexcept(useNoexcept) { +constexpr bool Vector::contains(MaybeView_t const&v) const noexcept(useNoexcept) { for (std::size_t i = 0; i < m_size; i++) { if (m_items[i] == v) { return true; @@ -528,7 +528,7 @@ constexpr bool Vector::contains(MaybeSV_t cons template constexpr typename Vector::template iterator Vector::insert( - std::size_t pos, std::size_t cnt, MaybeSV_t const&val) noexcept(useNoexcept) { + std::size_t pos, std::size_t cnt, MaybeView_t const&val) noexcept(useNoexcept) { if (m_size + cnt > m_cap) { reserveInsert(m_cap ? m_size + cnt : initialCap, pos, cnt); if (pos < m_size) { @@ -556,7 +556,7 @@ Vector::insert( template constexpr typename Vector::template iterator -Vector::insert(std::size_t pos, MaybeSV_t const&val) noexcept(useNoexcept) { +Vector::insert(std::size_t pos, MaybeView_t const&val) noexcept(useNoexcept) { if (m_size == m_cap) { reserveInsert(m_cap ? m_cap * 2 : initialCap, pos); if (pos < m_size) { @@ -622,7 +622,7 @@ constexpr void Vector::push_back(T &&item) noexce } template -constexpr void Vector::push_back(MaybeSV_t const&item) noexcept(useNoexcept) { +constexpr void Vector::push_back(MaybeView_t const&item) noexcept(useNoexcept) { if (m_size == m_cap) { reserve(m_cap ? m_cap * 2 : initialCap); } From 3b8d13dce37fb42f89593d02e98fafaf7adafeb5 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 18 Apr 2024 19:25:07 -0500 Subject: [PATCH 10/16] [nostalgia,olympic] Fixes for Ox update --- .../core/src/studio/paletteeditor/paletteeditor-imgui.cpp | 2 +- .../src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp | 4 ++-- src/olympic/keel/src/media.cpp | 8 ++++---- src/olympic/studio/modlib/src/filedialog_nfd.cpp | 4 ++-- src/olympic/turbine/src/gba/turbine.cpp | 4 ++-- src/olympic/turbine/src/glfw/clipboard.cpp | 2 +- src/olympic/turbine/src/glfw/gfx.cpp | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp index 37377a9a..76665047 100644 --- a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp +++ b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp @@ -70,7 +70,7 @@ void PaletteEditorImGui::drawColumn(ox::CStringView txt) noexcept { void PaletteEditorImGui::drawColumn(uint64_t i) noexcept { ox::Array numStr; - ox_itoa(i, numStr.data()); + ox::itoa(i, numStr.data()); drawColumn(numStr.data()); } diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp index 3d3a0687..ecfe4398 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp @@ -410,12 +410,12 @@ void TileSheetEditorImGui::drawPaletteSelector() noexcept { if (pages > 1) { ImGui::Indent(20); ox::Array numStr; - ox_itoa(m_model.palettePage() + 1, numStr.data()); + ox::itoa(m_model.palettePage() + 1, numStr.data()); ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - comboWidthSub); if (ImGui::BeginCombo("Page", numStr.data(), 0)) { for (auto n = 0u; n < pages; ++n) { auto const selected = (m_model.palettePage() == n); - ox_itoa(n + 1, numStr.data()); + ox::itoa(n + 1, numStr.data()); if (ImGui::Selectable(numStr.data(), selected) && m_model.palettePage() != n) { m_model.setPalettePage(n); } diff --git a/src/olympic/keel/src/media.cpp b/src/olympic/keel/src/media.cpp index 4bf4b029..03a7e718 100644 --- a/src/olympic/keel/src/media.cpp +++ b/src/olympic/keel/src/media.cpp @@ -134,12 +134,12 @@ ox::Result loadRom(ox::CRStringView) noexcept { // media section constexpr auto headerP2 = "R_______________"; constexpr auto headerP1 = "KEEL_MEDIA_HEADE"; - constexpr auto headerP1Len = ox_strlen(headerP2); - constexpr auto headerP2Len = ox_strlen(headerP1); + constexpr auto headerP1Len = ox::strlen(headerP2); + constexpr auto headerP2Len = ox::strlen(headerP1); constexpr auto headerLen = headerP1Len + headerP2Len; for (auto current = MEM_ROM; current < reinterpret_cast(0x0a000000); current += headerLen) { - if (ox_memcmp(current, headerP1, headerP1Len) == 0 && - ox_memcmp(current + headerP1Len, headerP2, headerP2Len) == 0) { + if (ox::memcmp(current, headerP1, headerP1Len) == 0 && + ox::memcmp(current + headerP1Len, headerP2, headerP2Len) == 0) { return current + headerLen; } } diff --git a/src/olympic/studio/modlib/src/filedialog_nfd.cpp b/src/olympic/studio/modlib/src/filedialog_nfd.cpp index 61de8bef..dfdcd92a 100644 --- a/src/olympic/studio/modlib/src/filedialog_nfd.cpp +++ b/src/olympic/studio/modlib/src/filedialog_nfd.cpp @@ -13,9 +13,9 @@ namespace studio { FDFilterItem::FDFilterItem(ox::CRStringView pName, ox::CRStringView pSpec) noexcept { name.resize(pName.len() + 1); - ox_strncpy(name.data(), pName.data(), pName.len()); + ox::strncpy(name.data(), pName.data(), pName.len()); spec.resize(pSpec.len() + 1); - ox_strncpy(spec.data(), pSpec.data(), pSpec.len()); + ox::strncpy(spec.data(), pSpec.data(), pSpec.len()); } static ox::Result toResult(nfdresult_t r, NFD::UniquePathN const&path) noexcept { diff --git a/src/olympic/turbine/src/gba/turbine.cpp b/src/olympic/turbine/src/gba/turbine.cpp index 2d1fe7d6..d67e9d4a 100644 --- a/src/olympic/turbine/src/gba/turbine.cpp +++ b/src/olympic/turbine/src/gba/turbine.cpp @@ -43,8 +43,8 @@ static ox::Result findPreloadSection() noexcept { // media section constexpr auto headerP2 = "DER_____________"; constexpr auto headerP1 = "KEEL_PRELOAD_HEA"; - constexpr auto headerP1Len = ox_strlen(headerP2); - constexpr auto headerP2Len = ox_strlen(headerP1); + constexpr auto headerP1Len = ox::strlen(headerP2); + constexpr auto headerP2Len = ox::strlen(headerP1); constexpr auto headerLen = headerP1Len + headerP2Len; for (auto current = MEM_ROM; current < reinterpret_cast(0x0a000000); current += headerLen) { if (memcmp(current, headerP1, headerP1Len) == 0 && diff --git a/src/olympic/turbine/src/glfw/clipboard.cpp b/src/olympic/turbine/src/glfw/clipboard.cpp index b2efdf1f..e23d3ce7 100644 --- a/src/olympic/turbine/src/glfw/clipboard.cpp +++ b/src/olympic/turbine/src/glfw/clipboard.cpp @@ -18,7 +18,7 @@ ox::String getClipboardText(Context &ctx) noexcept { void setClipboardText(Context &ctx, ox::CRStringView text) noexcept { auto cstr = ox_malloca(text.bytes() + 1, char); - ox_strncpy(cstr.get(), text.data(), text.bytes()); + ox::strncpy(cstr.get(), text.data(), text.bytes()); glfwSetClipboardString(ctx.window, cstr.get()); } diff --git a/src/olympic/turbine/src/glfw/gfx.cpp b/src/olympic/turbine/src/glfw/gfx.cpp index b4d7b0ae..c6e531f6 100644 --- a/src/olympic/turbine/src/glfw/gfx.cpp +++ b/src/olympic/turbine/src/glfw/gfx.cpp @@ -221,7 +221,7 @@ ox::Error initGfx(Context &ctx) noexcept { void setWindowTitle(Context &ctx, ox::CRStringView title) noexcept { auto cstr = ox_malloca(title.bytes() + 1, char); - ox_strncpy(cstr.get(), title.data(), title.bytes()); + ox::strncpy(cstr.get(), title.data(), title.bytes()); glfwSetWindowTitle(ctx.window, cstr.get()); } From 922323833c93fc23c294d6abd4d8175cf1826cde Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 18 Apr 2024 23:32:54 -0500 Subject: [PATCH 11/16] [ox] Cleanup --- deps/ox/src/ox/claw/read.cpp | 2 +- .../src/ox/fs/filestore/filestoretemplate.hpp | 8 +-- deps/ox/src/ox/fs/filesystem/filelocation.cpp | 2 +- deps/ox/src/ox/fs/filesystem/filelocation.hpp | 7 +- deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp | 12 ++-- deps/ox/src/ox/logconn/circularbuff.hpp | 10 +-- deps/ox/src/ox/mc/intops.hpp | 10 +-- deps/ox/src/ox/model/modelvalue.hpp | 12 ++-- deps/ox/src/ox/std/bit.hpp | 2 +- deps/ox/src/ox/std/bstring.hpp | 4 +- deps/ox/src/ox/std/buffer.hpp | 2 +- deps/ox/src/ox/std/memops.cpp | 53 +++++++++++++-- deps/ox/src/ox/std/memops.hpp | 68 +++++-------------- deps/ox/src/ox/std/string.hpp | 2 +- deps/ox/src/ox/std/test/tests.cpp | 8 +-- 15 files changed, 103 insertions(+), 99 deletions(-) diff --git a/deps/ox/src/ox/claw/read.cpp b/deps/ox/src/ox/claw/read.cpp index 9506cead..c4e43c1d 100644 --- a/deps/ox/src/ox/claw/read.cpp +++ b/deps/ox/src/ox/claw/read.cpp @@ -64,7 +64,7 @@ Result readClawHeader(const ox::Buffer &buff) noexcept { Result stripClawHeader(const char *buff, std::size_t buffLen) noexcept { oxRequire(header, readClawHeader(buff, buffLen)); Buffer out(header.dataSize); - ox_memcpy(out.data(), header.data, out.size()); + ox::listcpy(out.data(), header.data, out.size()); return out; } diff --git a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp index 6c00005c..ef4706c1 100644 --- a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp +++ b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp @@ -279,7 +279,7 @@ Error FileStoreTemplate::write(uint64_t id64, const void *data, FsSize_t oxAssert(destData.size() == dataSize, "Allocation size does not match data."); // write data if any was provided if (data != nullptr) { - ox_memcpy(destData, data, dest->size()); + ox::memcpy(destData, data, dest->size()); oxTrace("ox.fs.FileStoreTemplate.write", "Data written"); } auto fsData = fileStoreData(); @@ -336,7 +336,7 @@ Error FileStoreTemplate::read(uint64_t id, void *out, FsSize_t outSize, return OxError(1); } - ox_memcpy(out, srcData, srcData.size()); + ox::memcpy(out, srcData, srcData.size()); if (size) { *size = src.size(); } @@ -367,7 +367,7 @@ Error FileStoreTemplate::read(uint64_t id, FsSize_t readStart, FsSize_t return OxError(1); } - ox_memcpy(out, srcData.get() + readStart, readSize); + ox::memcpy(out, srcData.get() + readStart, readSize); if (size) { *size = src.size(); } @@ -400,7 +400,7 @@ Error FileStoreTemplate::read(uint64_t id, FsSize_t readStart, return OxError(1); } - ox_memcpy(out, srcData.get() + readStart, readSize); + ox::memcpy(out, srcData.get() + readStart, readSize); if (size) { *size = src.size(); } diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.cpp b/deps/ox/src/ox/fs/filesystem/filelocation.cpp index 72238e01..15cd529f 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.cpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.cpp @@ -48,7 +48,7 @@ FileAddress &FileAddress::operator=(const FileAddress &other) noexcept { if (other.m_data.path) { auto strSize = ox::strlen(other.m_data.path) + 1; m_data.path = new char[strSize]; - ox_memcpy(m_data.path, other.m_data.path, strSize); + ox::memcpy(m_data.path, other.m_data.path, strSize); } else { m_data.constPath = ""; m_type = FileAddressType::ConstPath; diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.hpp b/deps/ox/src/ox/fs/filesystem/filelocation.hpp index ded301e9..436d6cf1 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.hpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.hpp @@ -44,13 +44,10 @@ class FileAddress { protected: FileAddressType m_type = FileAddressType::None; - Data m_data; + Data m_data{}; public: - constexpr FileAddress() noexcept { - m_data.inode = 0; - m_type = FileAddressType::None; - } + constexpr FileAddress() noexcept = default; FileAddress(const FileAddress &other) noexcept; diff --git a/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp b/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp index c9bce570..e5ba4dd6 100644 --- a/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp +++ b/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp @@ -173,15 +173,15 @@ class OX_PACKED NodeBuffer { template NodeBuffer::NodeBuffer(std::size_t size) noexcept { m_header.size = static_cast(size); - ox_memset(this + 1, 0, size - sizeof(*this)); + ox::memset(this + 1, 0, size - sizeof(*this)); oxTracef("ox.NodeBuffer.constructor", "{}", m_header.firstItem.get()); } template NodeBuffer::NodeBuffer(const NodeBuffer &other, std::size_t size) noexcept { oxTracef("ox.ptrarith.NodeBuffer.copy", "other.m_header.firstItem: {}", other.m_header.firstItem.get()); - ox_memset(this + 1, 0, size - sizeof(*this)); - ox_memcpy(this, &other, size); + ox::memset(this + 1, 0, size - sizeof(*this)); + ox::memcpy(this, &other, size); } template @@ -291,7 +291,7 @@ Result::ItemPtr> NodeBuffer::mal oxTrace("ox.ptrarith.NodeBuffer.malloc.fail", "Unknown"); return OxError(1, "NodeBuffer::malloc: unknown failure"); } - ox_memset(out, 0, fullSize); + ox::memset(out, 0, fullSize); new (out) Item; out->setSize(sz); @@ -367,7 +367,7 @@ Error NodeBuffer::setSize(std::size_t size) noexcept { } else { m_header.size = static_cast(size); auto data = reinterpret_cast(this) + end; - ox_memset(data, 0, size - end); + ox::memset(data, 0, size - end); return OxError(0); } } @@ -405,7 +405,7 @@ Error NodeBuffer::compact(F cb) noexcept { return OxError(2); } // move node - ox_memcpy(dest, src, src->fullSize()); + ox::memcpy(dest, src, src->fullSize()); oxReturnError(cb(src, dest)); // update surrounding nodes auto prev = ptr(dest->prev); diff --git a/deps/ox/src/ox/logconn/circularbuff.hpp b/deps/ox/src/ox/logconn/circularbuff.hpp index 69a2c32f..32c0a3ee 100644 --- a/deps/ox/src/ox/logconn/circularbuff.hpp +++ b/deps/ox/src/ox/logconn/circularbuff.hpp @@ -47,13 +47,13 @@ class CirculerBuffer { } // write seg 1 const auto seg1Sz = ox::min(sz, m_buff.size() - m_writePt); - ox_memcpy(&m_buff[m_writePt], &buff[0], seg1Sz); + ox::listcpy(&m_buff[m_writePt], &buff[0], seg1Sz); m_writePt += sz; if (seg1Sz != sz) { m_writePt -= m_buff.size(); // write seg 2 const auto seg2Sz = sz - seg1Sz; - ox_memcpy(&m_buff[0], &buff[seg1Sz], seg2Sz); + ox::listcpy(&m_buff[0], &buff[seg1Sz], seg2Sz); oxAssert(m_buff[0] == buff[seg1Sz], "break"); } return {}; @@ -70,7 +70,7 @@ class CirculerBuffer { return {}; } - constexpr ox::Error seekp(int, ios_base::seekdir) { + constexpr ox::Error seekp(int, ios_base::seekdir) noexcept { return OxError(1, "Unimplemented"); } @@ -84,13 +84,13 @@ class CirculerBuffer { const auto bytesRead = ox::min(outSize, m_buff.size() - avail()); // read seg 1 const auto seg1Sz = ox::min(bytesRead, m_buff.size() - m_readPt); - ox_memcpy(&out[0], &m_buff[m_readPt], seg1Sz); + ox::listcpy(&out[0], &m_buff[m_readPt], seg1Sz); m_readPt += bytesRead; if (seg1Sz != bytesRead) { m_readPt -= m_buff.size(); // read seg 2 const auto seg2Sz = bytesRead - seg1Sz; - ox_memcpy(&out[seg1Sz], &m_buff[0], seg2Sz); + ox::listcpy(&out[seg1Sz], &m_buff[0], seg2Sz); } return bytesRead; } diff --git a/deps/ox/src/ox/mc/intops.hpp b/deps/ox/src/ox/mc/intops.hpp index b9c6bd92..7f369ef6 100644 --- a/deps/ox/src/ox/mc/intops.hpp +++ b/deps/ox/src/ox/mc/intops.hpp @@ -71,7 +71,7 @@ constexpr McInt encodeInteger(I pInput) noexcept { // move input to uint64_t to allow consistent bit manipulation, and to avoid // overflow concerns uint64_t val = 0; - ox_memcpy(&val, &input, sizeof(input)); + ox::memcpy(&val, &input, sizeof(input)); if (val) { // bits needed to represent number factoring in space possibly // needed for signed bit @@ -94,7 +94,7 @@ constexpr McInt encodeInteger(I pInput) noexcept { } if (bytes == 9) { out.data[0] = bytesIndicator; - ox_memcpy(&out.data[1], &leVal, 8); + ox::memcpy(&out.data[1], &leVal, 8); if (inputNegative) { out.data[1] |= 0b1000'0000; } @@ -104,7 +104,7 @@ constexpr McInt encodeInteger(I pInput) noexcept { auto intermediate = static_cast(leVal.raw() | (negBit << (valBits - 1))) << bytes | static_cast(bytesIndicator); - ox_memcpy(out.data, &intermediate, sizeof(intermediate)); + ox::memcpy(out.data, &intermediate, sizeof(intermediate)); } out.length = bytes; } @@ -160,7 +160,7 @@ constexpr Result decodeInteger(Reader_c auto&rdr, std::size_t *bytesRead) noe ox::Array d = {}; //d[0] = decoded & 0xffff'ffff; //d[1] = decoded >> 32; - ox_memcpy(d.data(), &decoded, sizeof(decoded)); + ox::memcpy(d.data(), &decoded, sizeof(decoded)); auto bit = negBit; for (; bit < ox::min(Bits, 32); ++bit) { d[0] |= 1 << bit; @@ -175,7 +175,7 @@ constexpr Result decodeInteger(Reader_c auto&rdr, std::size_t *bytesRead) noe d[0] = d[1]; d[1] = d0Tmp; } - ox_memcpy(&out, d.data(), sizeof(out)); + ox::memcpy(&out, d.data(), sizeof(out)); return out; } } diff --git a/deps/ox/src/ox/model/modelvalue.hpp b/deps/ox/src/ox/model/modelvalue.hpp index 53130335..416c894f 100644 --- a/deps/ox/src/ox/model/modelvalue.hpp +++ b/deps/ox/src/ox/model/modelvalue.hpp @@ -995,7 +995,7 @@ constexpr ModelValue::ModelValue(const ModelValue &other) noexcept { case Type::SignedInteger16: case Type::SignedInteger32: case Type::SignedInteger64: - ox_memcpy(&m_data, &other.m_data, sizeof(m_data)); + ox::memcpy(&m_data, &other.m_data, sizeof(m_data)); break; case Type::String: m_data.str = new String(other.get()); @@ -1028,8 +1028,8 @@ constexpr ModelValue::ModelValue(ModelValue &&other) noexcept { case Type::SignedInteger16: case Type::SignedInteger32: case Type::SignedInteger64: - ox_memcpy(&m_data, &other.m_data, sizeof(m_data)); - ox_memset(&other.m_data, 0, sizeof(m_data)); + ox::memcpy(&m_data, &other.m_data, sizeof(m_data)); + ox::memset(&other.m_data, 0, sizeof(m_data)); break; case Type::String: m_data.str = other.m_data.str; @@ -1220,7 +1220,7 @@ constexpr ModelValue &ModelValue::operator=(const ModelValue &other) noexcept { case Type::SignedInteger16: case Type::SignedInteger32: case Type::SignedInteger64: - ox_memcpy(&m_data, &other.m_data, sizeof(m_data)); + ox::memcpy(&m_data, &other.m_data, sizeof(m_data)); break; case Type::String: m_data.str = new String(other.get()); @@ -1258,8 +1258,8 @@ constexpr ModelValue &ModelValue::operator=(ModelValue &&other) noexcept { case Type::SignedInteger16: case Type::SignedInteger32: case Type::SignedInteger64: - ox_memcpy(&m_data, &other.m_data, sizeof(m_data)); - ox_memset(&other.m_data, 0, sizeof(m_data)); + ox::memcpy(&m_data, &other.m_data, sizeof(m_data)); + ox::memset(&other.m_data, 0, sizeof(m_data)); break; case Type::String: m_data.str = other.m_data.str; diff --git a/deps/ox/src/ox/std/bit.hpp b/deps/ox/src/ox/std/bit.hpp index d8be957f..f4b8d55d 100644 --- a/deps/ox/src/ox/std/bit.hpp +++ b/deps/ox/src/ox/std/bit.hpp @@ -34,7 +34,7 @@ namespace ox { template constexpr typename enable_if::type cbit_cast(From src) noexcept { To dst = {}; - ox_memcpy(&dst, &src, sizeof(src)); + ox::memcpy(&dst, &src, sizeof(src)); return dst; } diff --git a/deps/ox/src/ox/std/bstring.hpp b/deps/ox/src/ox/std/bstring.hpp index 7a450ede..d73585e7 100644 --- a/deps/ox/src/ox/std/bstring.hpp +++ b/deps/ox/src/ox/std/bstring.hpp @@ -119,7 +119,7 @@ constexpr BString &BString::operator=(ox::CRStringView str) noexcept if (cap() < strLen) { strLen = cap(); } - ox_memcpy(m_buff, str.data(), strLen); + ox::memcpy(m_buff, str.data(), strLen); // make sure last element is a null terminator m_buff[strLen] = 0; return *this; @@ -131,7 +131,7 @@ constexpr BString &BString::operator=(const char *str) noexcept { if (cap() < strLen) { strLen = cap(); } - ox_memcpy(m_buff, str, strLen); + ox::memcpy(m_buff, str, strLen); // make sure last element is a null terminator m_buff[cap()] = 0; return *this; diff --git a/deps/ox/src/ox/std/buffer.hpp b/deps/ox/src/ox/std/buffer.hpp index 2753c821..674a4a20 100644 --- a/deps/ox/src/ox/std/buffer.hpp +++ b/deps/ox/src/ox/std/buffer.hpp @@ -192,7 +192,7 @@ class BufferReader { if (m_it + sz > m_size) [[unlikely]] { return OxError(1, "Read failed: Buffer overrun"); } - ox_memcpy(v, &m_buff[m_it], sz); + ox::memcpy(v, &m_buff[m_it], sz); m_it += sz; return sz; } diff --git a/deps/ox/src/ox/std/memops.cpp b/deps/ox/src/ox/std/memops.cpp index 16c71ca1..c76dbf11 100644 --- a/deps/ox/src/ox/std/memops.cpp +++ b/deps/ox/src/ox/std/memops.cpp @@ -6,6 +6,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "defines.hpp" #include "types.hpp" #include "memops.hpp" @@ -16,15 +17,15 @@ extern "C" { void *ox_inhibit_loop_to_libcall memcpy(void *dest, const void *src, std::size_t size) { - return ox_memcpy(dest, src, size); + return ox::memcpy(dest, src, size); } void *ox_inhibit_loop_to_libcall memmove(void *dest, const void *src, std::size_t size) { - return ox_memcpy(dest, src, size); + return ox::memcpy(dest, src, size); } void *ox_inhibit_loop_to_libcall memset(void *ptr, int val, std::size_t size) { - return ox_memset(ptr, val, size); + return ox::memset(ptr, val, size); } @@ -50,6 +51,48 @@ int ox_inhibit_loop_to_libcall memcmp(const void *ptr1, const void *ptr2, std::s #endif -int ox_memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept { - return memcmp(ptr1, ptr2, size); +namespace ox { + +void *memmove(void *dest, const void *src, std::size_t size) noexcept { + if constexpr(!ox::defines::UseStdLib) { + auto srcBuf = static_cast(src); + auto dstBuf = static_cast(dest); + for (std::size_t i = 0; i < size; ++i) { + dstBuf[i] = static_cast(srcBuf[i]); + } + return dest; + } else { + return ::memmove(dest, src, size); + } +} + +void *memset(void *ptr, int val, std::size_t size) noexcept { + if constexpr(!ox::defines::UseStdLib) { + auto buf = static_cast(ptr); + for (std::size_t i = 0; i < size; ++i) { + buf[i] = static_cast(val); + } + return ptr; + } else { + return ::memset(ptr, val, size); + } +} + +void *memcpy(void *dest, const void *src, std::size_t size) noexcept { + if constexpr(!ox::defines::UseStdLib) { + auto srcBuf = static_cast(src); + auto dstBuf = static_cast(dest); + for (std::size_t i = 0; i < size; ++i) { + dstBuf[i] = static_cast(srcBuf[i]); + } + return dest; + } else { + return ::memcpy(dest, src, size); + } +} + +int memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept { + return ::memcmp(ptr1, ptr2, size); +} + } diff --git a/deps/ox/src/ox/std/memops.hpp b/deps/ox/src/ox/std/memops.hpp index 56bd7d11..470eebe5 100644 --- a/deps/ox/src/ox/std/memops.hpp +++ b/deps/ox/src/ox/std/memops.hpp @@ -8,7 +8,6 @@ #pragma once -#include "defines.hpp" #include "types.hpp" #include "typetraits.hpp" @@ -28,63 +27,28 @@ int memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept; } #endif -int ox_memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept; - -constexpr void *ox_memcpy(void *dest, const void *src, std::size_t size) noexcept { - if (std::is_constant_evaluated() || !ox::defines::UseStdLib) { - auto srcBuf = static_cast(src); - auto dstBuf = static_cast(dest); - for (std::size_t i = 0; i < size; ++i) { - dstBuf[i] = static_cast(srcBuf[i]); - } - return dest; - } else { - return memcpy(dest, src, size); - } -} - -constexpr void *ox_memmove(void *dest, const void *src, std::size_t size) noexcept { - if (std::is_constant_evaluated() || !ox::defines::UseStdLib) { - auto srcBuf = static_cast(src); - auto dstBuf = static_cast(dest); - for (std::size_t i = 0; i < size; ++i) { - dstBuf[i] = static_cast(srcBuf[i]); - } - return dest; - } else { - return memmove(dest, src, size); - } -} - -constexpr void *ox_memset(void *ptr, int val, std::size_t size) noexcept { - if (std::is_constant_evaluated() || !ox::defines::UseStdLib) { - auto buf = static_cast(ptr); - for (std::size_t i = 0; i < size; ++i) { - buf[i] = static_cast(val); - } - return ptr; - } else { - return memset(ptr, val, size); - } -} - namespace ox { -constexpr void *memmove(void *dest, const void *src, std::size_t size) { - return ox_memmove(dest, src, size); +template +constexpr T1 *listcpy(T1 *dest, T2 *src, std::size_t maxLen) noexcept { + using T1Type = typename ox::remove_reference::type; + std::size_t i = 0; + while (i < maxLen && src[i]) { + dest[i] = static_cast(src[i]); + ++i; + } + // set null terminator + dest[i] = 0; + return dest; } -constexpr void *memset(void *ptr, int val, std::size_t size) noexcept { - return ox_memset(ptr, val, size); -} +void *memmove(void *dest, const void *src, std::size_t size) noexcept; -constexpr void *memcpy(void *dest, const void *src, std::size_t size) noexcept { - return ox_memcpy(dest, src, size); -} +void *memset(void *ptr, int val, std::size_t size) noexcept; -inline int memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept { - return ox_memcmp(ptr1, ptr2, size); -} +void *memcpy(void *dest, const void *src, std::size_t size) noexcept; + +int memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept; template void *memsetElements(T *ptr, T val, std::size_t elements) noexcept { diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index e2604310..8b930920 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -167,7 +167,7 @@ class BasicString { constexpr Error append(const char *str, std::size_t strLen) noexcept { auto currentLen = len(); m_buff.resize(m_buff.size() + strLen); - ox_memcpy(&m_buff[currentLen], str, strLen); + ox::memcpy(&m_buff[currentLen], str, strLen); // make sure last element is a null terminator m_buff[currentLen + strLen] = 0; // this can't fail, but it returns an Error to match BString::append diff --git a/deps/ox/src/ox/std/test/tests.cpp b/deps/ox/src/ox/std/test/tests.cpp index 3aeb015a..14f8bdcb 100644 --- a/deps/ox/src/ox/std/test/tests.cpp +++ b/deps/ox/src/ox/std/test/tests.cpp @@ -44,25 +44,25 @@ static std::map tests = { { "ABCDEFG != HIJKLMN", []() { - return OxError(ox_memcmp("ABCDEFG", "HIJKLMN", 7) >= 0); + return OxError(ox::memcmp("ABCDEFG", "HIJKLMN", 7) >= 0); } }, { "HIJKLMN != ABCDEFG", []() { - return OxError(ox_memcmp("HIJKLMN", "ABCDEFG", 7) <= 0); + return OxError(ox::memcmp("HIJKLMN", "ABCDEFG", 7) <= 0); } }, { "ABCDEFG == ABCDEFG", []() { - return OxError(ox_memcmp("ABCDEFG", "ABCDEFG", 7) != 0); + return OxError(ox::memcmp("ABCDEFG", "ABCDEFG", 7) != 0); } }, { "ABCDEFGHI == ABCDEFG", []() { - return OxError(ox_memcmp("ABCDEFGHI", "ABCDEFG", 7) != 0); + return OxError(ox::memcmp("ABCDEFGHI", "ABCDEFG", 7) != 0); } }, { From 700d7016d551f4c6f4d8d8ca1840b92b13923e2f Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 18 Apr 2024 23:33:06 -0500 Subject: [PATCH 12/16] [nostalgia] Update for Ox changes --- .../tilesheeteditor/commands/deletetilescommand.cpp | 10 +++++----- .../tilesheeteditor/commands/inserttilescommand.cpp | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/deletetilescommand.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/deletetilescommand.cpp index 96a0bc25..f7ca3611 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/deletetilescommand.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/deletetilescommand.cpp @@ -24,7 +24,7 @@ core::DeleteTilesCommand::DeleteTilesCommand( auto dst = m_deletedPixels.data(); auto src = p.data() + m_deletePos; const auto sz = m_deleteSz * sizeof(decltype(p[0])); - ox_memcpy(dst, src, sz); + ox::memcpy(dst, src, sz); } } @@ -35,8 +35,8 @@ void core::DeleteTilesCommand::redo() noexcept { const auto src = p.data() + srcPos; const auto dst1 = p.data() + m_deletePos; const auto dst2 = p.data() + (p.size() - m_deleteSz); - ox_memmove(dst1, src, p.size() - srcPos); - ox_memset(dst2, 0, m_deleteSz * sizeof(decltype(p[0]))); + ox::memmove(dst1, src, p.size() - srcPos); + ox::memset(dst2, 0, m_deleteSz * sizeof(decltype(p[0]))); } void DeleteTilesCommand::undo() noexcept { @@ -46,8 +46,8 @@ void DeleteTilesCommand::undo() noexcept { const auto dst1 = p.data() + m_deletePos + m_deleteSz; const auto dst2 = src; const auto sz = p.size() - m_deletePos - m_deleteSz; - ox_memmove(dst1, src, sz); - ox_memcpy(dst2, m_deletedPixels.data(), m_deletedPixels.size()); + ox::memmove(dst1, src, sz); + ox::memcpy(dst2, m_deletedPixels.data(), m_deletedPixels.size()); } int DeleteTilesCommand::commandId() const noexcept { diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/inserttilescommand.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/inserttilescommand.cpp index e8e11c18..3ef9618a 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/inserttilescommand.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/inserttilescommand.cpp @@ -24,7 +24,7 @@ core::InsertTilesCommand::InsertTilesCommand( auto dst = m_deletedPixels.data(); auto src = p.data() + p.size() - m_insertCnt; const auto sz = m_insertCnt * sizeof(decltype(p[0])); - ox_memcpy(dst, src, sz); + ox::memcpy(dst, src, sz); } } @@ -34,8 +34,8 @@ void InsertTilesCommand::redo() noexcept { auto dstPos = m_insertPos + m_insertCnt; const auto dst = p.data() + dstPos; const auto src = p.data() + m_insertPos; - ox_memmove(dst, src, p.size() - dstPos); - ox_memset(src, 0, m_insertCnt * sizeof(decltype(p[0]))); + ox::memmove(dst, src, p.size() - dstPos); + ox::memset(src, 0, m_insertCnt * sizeof(decltype(p[0]))); } void InsertTilesCommand::undo() noexcept { @@ -46,8 +46,8 @@ void InsertTilesCommand::undo() noexcept { const auto dst1 = p.data() + m_insertPos; const auto dst2 = p.data() + p.size() - m_insertCnt; const auto sz = p.size() - srcIdx; - ox_memmove(dst1, src, sz); - ox_memcpy(dst2, m_deletedPixels.data(), m_deletedPixels.size()); + ox::memmove(dst1, src, sz); + ox::memcpy(dst2, m_deletedPixels.data(), m_deletedPixels.size()); } int InsertTilesCommand::commandId() const noexcept { From 6bfe184261cdc75a30892e545281c1435b2c2e27 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 18 Apr 2024 23:39:33 -0500 Subject: [PATCH 13/16] [ox/std] Remove unnecessary copying from HashMap::expand --- deps/ox/src/ox/std/hashmap.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/ox/src/ox/std/hashmap.hpp b/deps/ox/src/ox/std/hashmap.hpp index 37658149..f1c05431 100644 --- a/deps/ox/src/ox/std/hashmap.hpp +++ b/deps/ox/src/ox/std/hashmap.hpp @@ -212,7 +212,7 @@ template constexpr void HashMap::expand() { Vector r; for (std::size_t i = 0; i < m_keys.size(); ++i) { - K k{m_keys[i]}; + auto const&k = m_keys[i]; access(r, k) = std::move(access(m_pairs, k)); } m_pairs = std::move(r); From dfd27afd6725e720b7ea4078f4470fde9e2c2e10 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 18 Apr 2024 23:59:47 -0500 Subject: [PATCH 14/16] [ox/std] Add implicit String constructor for str literals Also make String more constexpr friendly --- deps/ox/src/ox/std/string.hpp | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index 8b930920..1c6f7651 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -32,6 +32,9 @@ class BasicString { constexpr explicit BasicString(std::size_t cap) noexcept; + template + constexpr BasicString(char const (&str)[Sz]) noexcept; + constexpr explicit BasicString(const char *str) noexcept; constexpr explicit BasicString(const char8_t *str) noexcept; @@ -167,7 +170,7 @@ class BasicString { constexpr Error append(const char *str, std::size_t strLen) noexcept { auto currentLen = len(); m_buff.resize(m_buff.size() + strLen); - ox::memcpy(&m_buff[currentLen], str, strLen); + ox::listcpy(&m_buff[currentLen], str, strLen); // make sure last element is a null terminator m_buff[currentLen + strLen] = 0; // this can't fail, but it returns an Error to match BString::append @@ -247,6 +250,14 @@ constexpr BasicString::BasicString(std::size_t cap) noexcept: } } +template +template +constexpr BasicString::BasicString(char const (&str)[Sz]) noexcept { + m_buff.resize(Sz + 1); + ox::listcpy(m_buff.data(), str, Sz); + m_buff[Sz] = 0; +} + template constexpr BasicString::BasicString(const char *str) noexcept { if (!m_buff.empty()) { @@ -270,7 +281,7 @@ constexpr BasicString::BasicString(const char8_t *str) noexce template constexpr BasicString::BasicString(const char *str, std::size_t size) noexcept { m_buff.resize(size + 1); - memcpy(m_buff.data(), str, size); + ox::listcpy(m_buff.data(), str, size); m_buff[size] = 0; } @@ -394,8 +405,8 @@ constexpr BasicString BasicString::operato const auto currentLen = len(); BasicString cpy(currentLen + strLen); cpy.m_buff.resize(m_buff.size() + strLen); - memcpy(&cpy.m_buff[0], m_buff.data(), currentLen); - memcpy(&cpy.m_buff[currentLen], str, strLen); + ox::listcpy(&cpy.m_buff[0], m_buff.data(), currentLen); + ox::listcpy(&cpy.m_buff[currentLen], str, strLen); // make sure last element is a null terminator cpy.m_buff[currentLen + strLen] = 0; return cpy; @@ -425,8 +436,8 @@ constexpr BasicString BasicString::operato const auto currentLen = len(); BasicString cpy(currentLen + strLen); cpy.m_buff.resize(m_buff.size() + strLen); - memcpy(&cpy.m_buff[0], m_buff.data(), currentLen); - memcpy(&cpy.m_buff[currentLen], src.data(), strLen + 1); + ox::listcpy(&cpy.m_buff[0], m_buff.data(), currentLen); + ox::listcpy(&cpy.m_buff[currentLen], src.data(), strLen + 1); return cpy; } @@ -436,8 +447,8 @@ constexpr BasicString BasicString::operato const auto currentLen = len(); BasicString cpy(currentLen + strLen); cpy.m_buff.resize(m_buff.size() + strLen); - memcpy(&cpy.m_buff[0], m_buff.data(), currentLen); - memcpy(&cpy.m_buff[currentLen], src.data(), strLen + 1); + ox::listcpy(&cpy.m_buff[0], m_buff.data(), currentLen); + ox::listcpy(&cpy.m_buff[currentLen], src.data(), strLen + 1); return cpy; } @@ -509,7 +520,7 @@ constexpr BasicString BasicString::substr( const auto size = end - begin; BasicString out(size); const auto buff = out.data(); - memcpy(buff, src, size); + ox::listcpy(buff, src, size); buff[size] = 0; return out; } @@ -550,7 +561,7 @@ template constexpr void BasicString::set(const char8_t *str) noexcept { std::size_t strBytes = ox::strlen(str) + 1; m_buff.resize(strBytes); - memcpy(m_buff.data(), str, strBytes); + ox::listcpy(m_buff.data(), str, strBytes); *m_buff.back().value = 0; } From ebf3a6961e17434db6fbb9e0b358a4da0243cb79 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 21 Apr 2024 10:31:26 -0500 Subject: [PATCH 15/16] [ox/std] Add String constructor that takes a StringLiteral --- deps/ox/src/ox/std/string.hpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index 1c6f7651..af137802 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -15,6 +15,7 @@ #include "algorithm.hpp" #include "memops.hpp" #include "serialize.hpp" +#include "stringliteral.hpp" #include "stringview.hpp" #include "strops.hpp" #include "vector.hpp" @@ -41,6 +42,8 @@ class BasicString { constexpr BasicString(const char *str, std::size_t size) noexcept; + constexpr explicit BasicString(StringLiteral const&str) noexcept; + constexpr explicit BasicString(CRStringView str) noexcept; constexpr explicit BasicString(BasicString const&) noexcept; @@ -250,14 +253,6 @@ constexpr BasicString::BasicString(std::size_t cap) noexcept: } } -template -template -constexpr BasicString::BasicString(char const (&str)[Sz]) noexcept { - m_buff.resize(Sz + 1); - ox::listcpy(m_buff.data(), str, Sz); - m_buff[Sz] = 0; -} - template constexpr BasicString::BasicString(const char *str) noexcept { if (!m_buff.empty()) { @@ -285,6 +280,11 @@ constexpr BasicString::BasicString(const char *str, std::size m_buff[size] = 0; } +template +constexpr BasicString::BasicString(StringLiteral const&str) noexcept: + BasicString(StringView{str.data(), str.bytes()}) { +} + template constexpr BasicString::BasicString(CRStringView str) noexcept { if (m_buff.empty()) { From 84205879d46610dfe08098d1265c0398f2215d3d Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 21 Apr 2024 10:32:42 -0500 Subject: [PATCH 16/16] [olympic] Cleanup ItemMaker, remove unnecessary copy --- src/olympic/studio/modlib/include/studio/itemmaker.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/olympic/studio/modlib/include/studio/itemmaker.hpp b/src/olympic/studio/modlib/include/studio/itemmaker.hpp index 1f7bf10d..ee095a79 100644 --- a/src/olympic/studio/modlib/include/studio/itemmaker.hpp +++ b/src/olympic/studio/modlib/include/studio/itemmaker.hpp @@ -19,7 +19,9 @@ class ItemMaker { ox::String const parentDir; ox::String const fileExt; constexpr explicit ItemMaker( - ox::StringView pName, ox::StringView pParentDir, ox::CRStringView pFileExt) noexcept: + ox::StringView pName, + ox::StringView pParentDir, + ox::StringView pFileExt) noexcept: typeName(pName), parentDir(pParentDir), fileExt(pFileExt) { @@ -62,7 +64,7 @@ class ItemMakerT: public ItemMaker { T pItem, ox::ClawFormat pFmt) noexcept: ItemMaker(pDisplayName, pParentDir, fileExt), - m_item(pItem), + m_item(std::move(pItem)), m_fmt(pFmt) { } constexpr ItemMakerT(