From 1ffcd3f213592ad0d2738b19739c7a9d82f00aac Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Wed, 13 Aug 2025 11:43:51 +0200 Subject: [PATCH] Replace usage of std::distance and std::advance --- CMakeLists.txt | 2 +- apps/openmw/mwbase/journal.hpp | 6 ++- apps/openmw/mwdialogue/journalimp.hpp | 4 ++ apps/openmw/mwdialogue/topic.cpp | 2 - apps/openmw/mwdialogue/topic.hpp | 6 ++- apps/openmw/mwlua/types/player.cpp | 61 +++++++++------------------ 6 files changed, 36 insertions(+), 45 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 201cc600c7..b43740b283 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,7 +82,7 @@ message(STATUS "Configuring OpenMW...") set(OPENMW_VERSION_MAJOR 0) set(OPENMW_VERSION_MINOR 50) set(OPENMW_VERSION_RELEASE 0) -set(OPENMW_LUA_API_REVISION 90) +set(OPENMW_LUA_API_REVISION 91) set(OPENMW_POSTPROCESSING_API_REVISION 3) set(OPENMW_VERSION_COMMITHASH "") diff --git a/apps/openmw/mwbase/journal.hpp b/apps/openmw/mwbase/journal.hpp index 662689376b..15b693892a 100644 --- a/apps/openmw/mwbase/journal.hpp +++ b/apps/openmw/mwbase/journal.hpp @@ -47,7 +47,7 @@ namespace MWBase virtual void clear() = 0; - virtual ~Journal() {} + virtual ~Journal() = default; virtual MWDialogue::Quest& getOrStartQuest(const ESM::RefId& id) = 0; ///< Gets the quest requested. Creates it and inserts it in quests if it is not yet started. @@ -79,6 +79,8 @@ namespace MWBase virtual TEntryIter end() const = 0; ///< Iterator pointing past the end of the main journal. + virtual const TEntryContainer& getEntries() const = 0; + virtual TQuestIter questBegin() const = 0; ///< Iterator pointing to the first quest (sorted by topic ID) @@ -93,6 +95,8 @@ namespace MWBase virtual TTopicIter topicEnd() const = 0; ///< Iterator pointing past the last topic. + virtual const TTopicContainer& getTopics() const = 0; + virtual int countSavedGameRecords() const = 0; virtual void write(ESM::ESMWriter& writer, Loading::Listener& progress) const = 0; diff --git a/apps/openmw/mwdialogue/journalimp.hpp b/apps/openmw/mwdialogue/journalimp.hpp index b30c469cb8..8545ac398b 100644 --- a/apps/openmw/mwdialogue/journalimp.hpp +++ b/apps/openmw/mwdialogue/journalimp.hpp @@ -55,6 +55,8 @@ namespace MWDialogue TEntryIter end() const override; ///< Iterator pointing past the end of the main journal. + const TEntryContainer& getEntries() const override { return mJournal; } + TQuestIter questBegin() const override; ///< Iterator pointing to the first quest (sorted by topic ID) @@ -69,6 +71,8 @@ namespace MWDialogue TTopicIter topicEnd() const override; ///< Iterator pointing past the last topic. + const TTopicContainer& getTopics() const override { return mTopics; } + int countSavedGameRecords() const override; void write(ESM::ESMWriter& writer, Loading::Listener& progress) const override; diff --git a/apps/openmw/mwdialogue/topic.cpp b/apps/openmw/mwdialogue/topic.cpp index 356162da62..dd58afa066 100644 --- a/apps/openmw/mwdialogue/topic.cpp +++ b/apps/openmw/mwdialogue/topic.cpp @@ -14,8 +14,6 @@ namespace MWDialogue { } - Topic::~Topic() {} - bool Topic::addEntry(const JournalEntry& entry) { if (entry.mTopic != mTopic) diff --git a/apps/openmw/mwdialogue/topic.hpp b/apps/openmw/mwdialogue/topic.hpp index d95d2470c7..4abd48760b 100644 --- a/apps/openmw/mwdialogue/topic.hpp +++ b/apps/openmw/mwdialogue/topic.hpp @@ -31,7 +31,7 @@ namespace MWDialogue Topic(const ESM::RefId& topic); - virtual ~Topic(); + virtual ~Topic() = default; virtual bool addEntry(const JournalEntry& entry); ///< Add entry @@ -53,6 +53,10 @@ namespace MWDialogue TEntryIter end() const; ///< Iterator pointing past the end of the journal for this topic. + + std::size_t size() const { return mEntries.size(); } + + const Entry& operator[](std::size_t i) const { return mEntries[i]; } }; } diff --git a/apps/openmw/mwlua/types/player.cpp b/apps/openmw/mwlua/types/player.cpp index fa9eea0797..1eba2be37a 100644 --- a/apps/openmw/mwlua/types/player.cpp +++ b/apps/openmw/mwlua/types/player.cpp @@ -104,14 +104,10 @@ namespace const MWDialogue::Topic& getTopicDataOrThrow(const ESM::RefId& topicId, const MWBase::Journal* journal) { - const MWBase::Journal::TTopicIter iterToFoundTopic = std::find_if(journal->topicBegin(), journal->topicEnd(), - [&topicId](const auto& topicKeyAndValue) { return topicKeyAndValue.first == topicId; }); - if (iterToFoundTopic == journal->topicEnd()) - { - throw std::runtime_error( - "Topic id: \"" + topicId.serializeText() + "\" expected to be present in player journal data"); - } - return iterToFoundTopic->second; + const auto it = journal->getTopics().find(topicId); + if (it == journal->topicEnd()) + throw std::runtime_error("Topic " + topicId.toDebugString() + " could not be found in the journal"); + return it->second; } } @@ -133,8 +129,8 @@ namespace MWLua { auto journalBindingsClass = lua.new_usertype("MWDialogue_Journal"); journalBindingsClass[sol::meta_function::to_string] = [](const MWBase::Journal& store) { - const size_t numberOfTopics = std::distance(store.topicBegin(), store.topicEnd()); - const size_t numberOfJournalEntries = std::distance(store.begin(), store.end()); + const size_t numberOfTopics = store.getTopics().size(); + const size_t numberOfJournalEntries = store.getEntries().size(); return "{MWDialogue_Journal: " + std::to_string(numberOfTopics) + " topic entries, " + std::to_string(numberOfJournalEntries) + " journal entries}"; }; @@ -148,21 +144,19 @@ namespace MWLua { auto topicsBindingsClass = lua.new_usertype("MWDialogue_Journal_Topics"); topicsBindingsClass[sol::meta_function::to_string] = [journal](const MWLua::Topics& topicEntriesStore) { - const size_t numberOfTopics = std::distance(journal->topicBegin(), journal->topicEnd()); + const size_t numberOfTopics = journal->getTopics().size(); return "{MWDialogue_Journal_Topics: " + std::to_string(numberOfTopics) + " topics}"; }; topicsBindingsClass[sol::meta_function::index] = [journal]( const MWLua::Topics& topicEntriesStore, std::string_view givenTopicId) -> const MWDialogue::Topic* { - const MWBase::Journal::TTopicIter iterToFoundTopic - = std::find_if(journal->topicBegin(), journal->topicEnd(), - [&givenTopicId](const auto& topicKeyAndValue) { return topicKeyAndValue.first == givenTopicId; }); - - return (iterToFoundTopic != journal->topicEnd()) ? &(iterToFoundTopic->second) : nullptr; - }; - topicsBindingsClass[sol::meta_function::length] = [journal](const MWLua::Topics&) -> size_t { - return std::distance(journal->topicBegin(), journal->topicEnd()); + const auto it = journal->getTopics().find(ESM::RefId::deserializeText(givenTopicId)); + if (it == journal->topicEnd()) + return nullptr; + return &it->second; }; + topicsBindingsClass[sol::meta_function::length] + = [journal](const MWLua::Topics&) -> size_t { return journal->getTopics().size(); }; topicsBindingsClass[sol::meta_function::pairs] = [journal](const MWLua::Topics&) { MWBase::Journal::TTopicIter iterator = journal->topicBegin(); return sol::as_function( @@ -196,28 +190,21 @@ namespace MWLua = lua.new_usertype("MWDialogue_Journal_Topic_WrittenEntries"); topicEntriesBindingsClass[sol::meta_function::to_string] = [journal](const MWLua::TopicEntries& topicEntries) { const MWDialogue::Topic& topic = getTopicDataOrThrow(topicEntries.mTopicId, journal); - const size_t numberOfTopics = std::distance(topic.begin(), topic.end()); return "MWDialogue_Journal_Topic_WrittenEntries for \"" + std::string{ topic.getName() } - + "\": " + std::to_string(numberOfTopics) + " elements"; + + "\": " + std::to_string(topic.size()) + " elements"; }; topicEntriesBindingsClass[sol::meta_function::length] = [journal](const MWLua::TopicEntries& topicEntries) { const MWDialogue::Topic& topic = getTopicDataOrThrow(topicEntries.mTopicId, journal); - return std::distance(topic.begin(), topic.end()); + return topic.size(); }; topicEntriesBindingsClass[sol::meta_function::index] = [journal](const MWLua::TopicEntries& topicEntries, size_t index) -> const MWDialogue::Entry* { const MWDialogue::Topic& topic = getTopicDataOrThrow(topicEntries.mTopicId, journal); - const size_t numberOfTopicEntries = std::distance(topic.begin(), topic.end()); - if (index == 0 || index > numberOfTopicEntries) - { + if (index == 0 || index > topic.size()) return nullptr; - } index = LuaUtil::fromLuaIndex(index); - - MWDialogue::Topic::TEntryIter iter{ topic.begin() }; - std::advance(iter, index); - return &(*iter); + return &topic[index]; }; topicEntriesBindingsClass[sol::meta_function::ipairs] = lua["ipairsForArray"].template get(); topicEntriesBindingsClass[sol::meta_function::pairs] = lua["ipairsForArray"].template get(); @@ -241,23 +228,17 @@ namespace MWLua { auto journalEntriesBindingsClass = lua.new_usertype("MWDialogue_Journal_WrittenEntries"); journalEntriesBindingsClass[sol::meta_function::to_string] = [journal](const MWLua::JournalEntries&) { - const size_t numberOfEntries = std::distance(journal->begin(), journal->end()); + const size_t numberOfEntries = journal->getEntries().size(); return "{MWDialogue_Journal_WrittenEntries: " + std::to_string(numberOfEntries) + " journal text entries}"; }; journalEntriesBindingsClass[sol::meta_function::length] - = [journal](const MWLua::JournalEntries&) { return std::distance(journal->begin(), journal->end()); }; + = [journal](const MWLua::JournalEntries&) { return journal->getEntries().size(); }; journalEntriesBindingsClass[sol::meta_function::index] = [journal](const MWLua::JournalEntries&, size_t index) -> const MWDialogue::StampedJournalEntry* { - const size_t numberOfEntries = std::distance(journal->begin(), journal->end()); - if (index == 0 || index > numberOfEntries) - { + if (index == 0 || index > journal->getEntries().size()) return nullptr; - } index = LuaUtil::fromLuaIndex(index); - - MWBase::Journal::TEntryIter iter{ journal->begin() }; - std::advance(iter, index); - return &(*iter); + return &journal->getEntries()[index]; }; journalEntriesBindingsClass[sol::meta_function::ipairs] = lua["ipairsForArray"].template get(); journalEntriesBindingsClass[sol::meta_function::pairs] = lua["ipairsForArray"].template get();