1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-10-18 12:16:41 +00:00

Replace usage of std::distance and std::advance

This commit is contained in:
Evil Eye 2025-08-13 11:43:51 +02:00
parent cffb045767
commit 1ffcd3f213
6 changed files with 36 additions and 45 deletions

View file

@ -82,7 +82,7 @@ message(STATUS "Configuring OpenMW...")
set(OPENMW_VERSION_MAJOR 0) set(OPENMW_VERSION_MAJOR 0)
set(OPENMW_VERSION_MINOR 50) set(OPENMW_VERSION_MINOR 50)
set(OPENMW_VERSION_RELEASE 0) 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_POSTPROCESSING_API_REVISION 3)
set(OPENMW_VERSION_COMMITHASH "") set(OPENMW_VERSION_COMMITHASH "")

View file

@ -47,7 +47,7 @@ namespace MWBase
virtual void clear() = 0; virtual void clear() = 0;
virtual ~Journal() {} virtual ~Journal() = default;
virtual MWDialogue::Quest& getOrStartQuest(const ESM::RefId& id) = 0; 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. ///< 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; virtual TEntryIter end() const = 0;
///< Iterator pointing past the end of the main journal. ///< Iterator pointing past the end of the main journal.
virtual const TEntryContainer& getEntries() const = 0;
virtual TQuestIter questBegin() const = 0; virtual TQuestIter questBegin() const = 0;
///< Iterator pointing to the first quest (sorted by topic ID) ///< Iterator pointing to the first quest (sorted by topic ID)
@ -93,6 +95,8 @@ namespace MWBase
virtual TTopicIter topicEnd() const = 0; virtual TTopicIter topicEnd() const = 0;
///< Iterator pointing past the last topic. ///< Iterator pointing past the last topic.
virtual const TTopicContainer& getTopics() const = 0;
virtual int countSavedGameRecords() const = 0; virtual int countSavedGameRecords() const = 0;
virtual void write(ESM::ESMWriter& writer, Loading::Listener& progress) const = 0; virtual void write(ESM::ESMWriter& writer, Loading::Listener& progress) const = 0;

View file

@ -55,6 +55,8 @@ namespace MWDialogue
TEntryIter end() const override; TEntryIter end() const override;
///< Iterator pointing past the end of the main journal. ///< Iterator pointing past the end of the main journal.
const TEntryContainer& getEntries() const override { return mJournal; }
TQuestIter questBegin() const override; TQuestIter questBegin() const override;
///< Iterator pointing to the first quest (sorted by topic ID) ///< Iterator pointing to the first quest (sorted by topic ID)
@ -69,6 +71,8 @@ namespace MWDialogue
TTopicIter topicEnd() const override; TTopicIter topicEnd() const override;
///< Iterator pointing past the last topic. ///< Iterator pointing past the last topic.
const TTopicContainer& getTopics() const override { return mTopics; }
int countSavedGameRecords() const override; int countSavedGameRecords() const override;
void write(ESM::ESMWriter& writer, Loading::Listener& progress) const override; void write(ESM::ESMWriter& writer, Loading::Listener& progress) const override;

View file

@ -14,8 +14,6 @@ namespace MWDialogue
{ {
} }
Topic::~Topic() {}
bool Topic::addEntry(const JournalEntry& entry) bool Topic::addEntry(const JournalEntry& entry)
{ {
if (entry.mTopic != mTopic) if (entry.mTopic != mTopic)

View file

@ -31,7 +31,7 @@ namespace MWDialogue
Topic(const ESM::RefId& topic); Topic(const ESM::RefId& topic);
virtual ~Topic(); virtual ~Topic() = default;
virtual bool addEntry(const JournalEntry& entry); virtual bool addEntry(const JournalEntry& entry);
///< Add entry ///< Add entry
@ -53,6 +53,10 @@ namespace MWDialogue
TEntryIter end() const; TEntryIter end() const;
///< Iterator pointing past the end of the journal for this topic. ///< 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]; }
}; };
} }

View file

@ -104,14 +104,10 @@ namespace
const MWDialogue::Topic& getTopicDataOrThrow(const ESM::RefId& topicId, const MWBase::Journal* journal) const MWDialogue::Topic& getTopicDataOrThrow(const ESM::RefId& topicId, const MWBase::Journal* journal)
{ {
const MWBase::Journal::TTopicIter iterToFoundTopic = std::find_if(journal->topicBegin(), journal->topicEnd(), const auto it = journal->getTopics().find(topicId);
[&topicId](const auto& topicKeyAndValue) { return topicKeyAndValue.first == topicId; }); if (it == journal->topicEnd())
if (iterToFoundTopic == journal->topicEnd()) throw std::runtime_error("Topic " + topicId.toDebugString() + " could not be found in the journal");
{ return it->second;
throw std::runtime_error(
"Topic id: \"" + topicId.serializeText() + "\" expected to be present in player journal data");
}
return iterToFoundTopic->second;
} }
} }
@ -133,8 +129,8 @@ namespace MWLua
{ {
auto journalBindingsClass = lua.new_usertype<MWBase::Journal>("MWDialogue_Journal"); auto journalBindingsClass = lua.new_usertype<MWBase::Journal>("MWDialogue_Journal");
journalBindingsClass[sol::meta_function::to_string] = [](const MWBase::Journal& store) { journalBindingsClass[sol::meta_function::to_string] = [](const MWBase::Journal& store) {
const size_t numberOfTopics = std::distance(store.topicBegin(), store.topicEnd()); const size_t numberOfTopics = store.getTopics().size();
const size_t numberOfJournalEntries = std::distance(store.begin(), store.end()); const size_t numberOfJournalEntries = store.getEntries().size();
return "{MWDialogue_Journal: " + std::to_string(numberOfTopics) + " topic entries, " return "{MWDialogue_Journal: " + std::to_string(numberOfTopics) + " topic entries, "
+ std::to_string(numberOfJournalEntries) + " journal entries}"; + std::to_string(numberOfJournalEntries) + " journal entries}";
}; };
@ -148,21 +144,19 @@ namespace MWLua
{ {
auto topicsBindingsClass = lua.new_usertype<MWLua::Topics>("MWDialogue_Journal_Topics"); auto topicsBindingsClass = lua.new_usertype<MWLua::Topics>("MWDialogue_Journal_Topics");
topicsBindingsClass[sol::meta_function::to_string] = [journal](const MWLua::Topics& topicEntriesStore) { 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}"; return "{MWDialogue_Journal_Topics: " + std::to_string(numberOfTopics) + " topics}";
}; };
topicsBindingsClass[sol::meta_function::index] topicsBindingsClass[sol::meta_function::index]
= [journal]( = [journal](
const MWLua::Topics& topicEntriesStore, std::string_view givenTopicId) -> const MWDialogue::Topic* { const MWLua::Topics& topicEntriesStore, std::string_view givenTopicId) -> const MWDialogue::Topic* {
const MWBase::Journal::TTopicIter iterToFoundTopic const auto it = journal->getTopics().find(ESM::RefId::deserializeText(givenTopicId));
= std::find_if(journal->topicBegin(), journal->topicEnd(), if (it == journal->topicEnd())
[&givenTopicId](const auto& topicKeyAndValue) { return topicKeyAndValue.first == givenTopicId; }); return nullptr;
return &it->second;
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());
}; };
topicsBindingsClass[sol::meta_function::length]
= [journal](const MWLua::Topics&) -> size_t { return journal->getTopics().size(); };
topicsBindingsClass[sol::meta_function::pairs] = [journal](const MWLua::Topics&) { topicsBindingsClass[sol::meta_function::pairs] = [journal](const MWLua::Topics&) {
MWBase::Journal::TTopicIter iterator = journal->topicBegin(); MWBase::Journal::TTopicIter iterator = journal->topicBegin();
return sol::as_function( return sol::as_function(
@ -196,28 +190,21 @@ namespace MWLua
= lua.new_usertype<MWLua::TopicEntries>("MWDialogue_Journal_Topic_WrittenEntries"); = lua.new_usertype<MWLua::TopicEntries>("MWDialogue_Journal_Topic_WrittenEntries");
topicEntriesBindingsClass[sol::meta_function::to_string] = [journal](const MWLua::TopicEntries& topicEntries) { topicEntriesBindingsClass[sol::meta_function::to_string] = [journal](const MWLua::TopicEntries& topicEntries) {
const MWDialogue::Topic& topic = getTopicDataOrThrow(topicEntries.mTopicId, journal); 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() } 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) { topicEntriesBindingsClass[sol::meta_function::length] = [journal](const MWLua::TopicEntries& topicEntries) {
const MWDialogue::Topic& topic = getTopicDataOrThrow(topicEntries.mTopicId, journal); const MWDialogue::Topic& topic = getTopicDataOrThrow(topicEntries.mTopicId, journal);
return std::distance(topic.begin(), topic.end()); return topic.size();
}; };
topicEntriesBindingsClass[sol::meta_function::index] topicEntriesBindingsClass[sol::meta_function::index]
= [journal](const MWLua::TopicEntries& topicEntries, size_t index) -> const MWDialogue::Entry* { = [journal](const MWLua::TopicEntries& topicEntries, size_t index) -> const MWDialogue::Entry* {
const MWDialogue::Topic& topic = getTopicDataOrThrow(topicEntries.mTopicId, journal); const MWDialogue::Topic& topic = getTopicDataOrThrow(topicEntries.mTopicId, journal);
const size_t numberOfTopicEntries = std::distance(topic.begin(), topic.end()); if (index == 0 || index > topic.size())
if (index == 0 || index > numberOfTopicEntries)
{
return nullptr; return nullptr;
}
index = LuaUtil::fromLuaIndex(index); index = LuaUtil::fromLuaIndex(index);
return &topic[index];
MWDialogue::Topic::TEntryIter iter{ topic.begin() };
std::advance(iter, index);
return &(*iter);
}; };
topicEntriesBindingsClass[sol::meta_function::ipairs] = lua["ipairsForArray"].template get<sol::function>(); topicEntriesBindingsClass[sol::meta_function::ipairs] = lua["ipairsForArray"].template get<sol::function>();
topicEntriesBindingsClass[sol::meta_function::pairs] = lua["ipairsForArray"].template get<sol::function>(); topicEntriesBindingsClass[sol::meta_function::pairs] = lua["ipairsForArray"].template get<sol::function>();
@ -241,23 +228,17 @@ namespace MWLua
{ {
auto journalEntriesBindingsClass = lua.new_usertype<MWLua::JournalEntries>("MWDialogue_Journal_WrittenEntries"); auto journalEntriesBindingsClass = lua.new_usertype<MWLua::JournalEntries>("MWDialogue_Journal_WrittenEntries");
journalEntriesBindingsClass[sol::meta_function::to_string] = [journal](const MWLua::JournalEntries&) { 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}"; return "{MWDialogue_Journal_WrittenEntries: " + std::to_string(numberOfEntries) + " journal text entries}";
}; };
journalEntriesBindingsClass[sol::meta_function::length] 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] journalEntriesBindingsClass[sol::meta_function::index]
= [journal](const MWLua::JournalEntries&, size_t index) -> const MWDialogue::StampedJournalEntry* { = [journal](const MWLua::JournalEntries&, size_t index) -> const MWDialogue::StampedJournalEntry* {
const size_t numberOfEntries = std::distance(journal->begin(), journal->end()); if (index == 0 || index > journal->getEntries().size())
if (index == 0 || index > numberOfEntries)
{
return nullptr; return nullptr;
}
index = LuaUtil::fromLuaIndex(index); index = LuaUtil::fromLuaIndex(index);
return &journal->getEntries()[index];
MWBase::Journal::TEntryIter iter{ journal->begin() };
std::advance(iter, index);
return &(*iter);
}; };
journalEntriesBindingsClass[sol::meta_function::ipairs] = lua["ipairsForArray"].template get<sol::function>(); journalEntriesBindingsClass[sol::meta_function::ipairs] = lua["ipairsForArray"].template get<sol::function>();
journalEntriesBindingsClass[sol::meta_function::pairs] = lua["ipairsForArray"].template get<sol::function>(); journalEntriesBindingsClass[sol::meta_function::pairs] = lua["ipairsForArray"].template get<sol::function>();