From 4b625c70170711bd689c7d12ca724e5efdddcb21 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sat, 12 Dec 2015 12:28:08 +0000 Subject: [PATCH] Refactor loot_db class Move non-template function definitions out of the header file, and remove unnecessary templating. --- CMakeLists.txt | 3 +- src/api/api.cpp | 1 + src/api/loot_db.cpp | 134 ++++++++++++++++++++++++++++++++++++++++++++ src/api/loot_db.h | 120 +++++++-------------------------------- 4 files changed, 157 insertions(+), 101 deletions(-) create mode 100644 src/api/loot_db.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f68669bb..d9c5a988 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -148,7 +148,8 @@ set (LOOT_GUI_HEADERS ${LOOT_HEADERS} "${CMAKE_SOURCE_DIR}/src/gui/resource.h") set (LOOT_API_SRC ${LOOT_SRC} - "${CMAKE_SOURCE_DIR}/src/api/api.cpp") + "${CMAKE_SOURCE_DIR}/src/api/api.cpp" + "${CMAKE_SOURCE_DIR}/src/api/loot_db.cpp") set (LOOT_API_HEADERS ${LOOT_HEADERS} "${CMAKE_SOURCE_DIR}/include/loot/api.h" diff --git a/src/api/api.cpp b/src/api/api.cpp index d4861020..94a6a597 100644 --- a/src/api/api.cpp +++ b/src/api/api.cpp @@ -24,6 +24,7 @@ #include "loot/api.h" #include "loot_db.h" +#include "../backend/error.h" #include "../backend/globals.h" #include "../backend/plugin_sorter.h" diff --git a/src/api/loot_db.cpp b/src/api/loot_db.cpp new file mode 100644 index 00000000..5fea3248 --- /dev/null +++ b/src/api/loot_db.cpp @@ -0,0 +1,134 @@ +/* LOOT + + A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and + Fallout: New Vegas. + + Copyright (C) 2013-2015 WrinklyNinja + + This file is part of LOOT. + + LOOT is free software: you can redistribute + it and/or modify it under the terms of the GNU General Public License + as published by the Free Software Foundation, either version 3 of + the License, or (at your option) any later version. + + LOOT is distributed in the hope that it will + be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LOOT. If not, see + . + */ + +#include "loot_db.h" + +#include "../backend/error.h" + +loot_db::loot_db(const unsigned int clientGame, const std::string& gamePath, const boost::filesystem::path& gameLocalDataPath) + : Game(clientGame) { + this->SetGamePath(gamePath); + this->Init(false, gameLocalDataPath); +} + +const char * loot_db::getRevisionIdString() const { + return revisionId.c_str(); +} + +const char * loot_db::getRevisionDateString() const { + return revisionDate.c_str(); +} + +const std::vector& loot_db::getPluginNames() const { + return cPluginNames; +} + +const std::vector& loot_db::getBashTagMap() const { + return cBashTagMap; +} + +unsigned int loot_db::getBashTagUid(const std::string& name) const { + auto it = bashTagMap.find(name); + if (it != end(bashTagMap)) + return it->second; + + throw loot::error(loot::error::no_tag_map, "The Bash Tag \"" + name + "\" does not exist in the Bash Tag map."); +} + +const std::vector& loot_db::getAddedTagIds() const { + return addedTagIds; +} + +const std::vector& loot_db::getRemovedTagIds() const { + return removedTagIds; +} + +const std::vector& loot_db::getPluginMessages() const { + return cPluginMessages; +} + +void loot_db::setRevisionIdString(const std::string& str) { + revisionId = str; +} + +void loot_db::setRevisionDateString(const std::string& str) { + revisionDate = str; +} + +void loot_db::setAddedTags(const std::set& names) { + for (const auto& name : names) + addedTagIds.push_back(getBashTagUid(name)); +} + +void loot_db::setRemovedTags(const std::set& names) { + for (const auto& name : names) + removedTagIds.push_back(getBashTagUid(name)); +} + +void loot_db::setPluginMessages(const std::list& pluginMessages) { + cPluginMessages.resize(pluginMessages.size()); + pluginMessageStrings.resize(pluginMessages.size()); + + size_t i = 0; + for (const auto& message : pluginMessages) { + pluginMessageStrings[i] = message.ChooseContent(loot::Language::any).Str(); + + cPluginMessages[i].type = message.Type(); + cPluginMessages[i].message = pluginMessageStrings[i].c_str(); + + ++i; + } +} + +void loot_db::addBashTagsToMap(std::set names) { + for (const auto& name : names) { + // Try adding the Bash Tag to the map assuming it's not already in + // there, then use the UID in the returned value, as that will be + // equal to the value in the map, even if the Bash Tag was already + // present. + unsigned int uid = bashTagMap.size(); + // If the tag already exists in the map, do + auto it = bashTagMap.emplace(name, uid).first; + if (it->second == cBashTagMap.size()) + cBashTagMap.push_back(it->first.c_str()); + else + cBashTagMap.at(it->second) = it->first.c_str(); + } +} + +void loot_db::clearBashTagMap() { + bashTagMap.clear(); + cBashTagMap.clear(); +} + +void loot_db::clearArrays() { + pluginNames.clear(); + cPluginNames.clear(); + + addedTagIds.clear(); + removedTagIds.clear(); + + cPluginMessages.clear(); + pluginMessageStrings.clear(); +} diff --git a/src/api/loot_db.h b/src/api/loot_db.h index 17352b6a..34786d87 100644 --- a/src/api/loot_db.h +++ b/src/api/loot_db.h @@ -26,64 +26,34 @@ #define LOOT_API_LOOT_DB_INT_H #include "../backend/game/game.h" -#include "../backend/error.h" +#include "../include/loot/api.h" #include #include struct loot_db : public loot::Game { - loot_db(const unsigned int clientGame, const std::string& gamePath, const boost::filesystem::path& gameLocalDataPath) - : Game(clientGame) { - this->SetGamePath(gamePath); - this->Init(false, gameLocalDataPath); - } + loot_db(const unsigned int clientGame, + const std::string& gamePath, + const boost::filesystem::path& gameLocalDataPath); loot::MetadataList rawUserMetadata; loot::Masterlist rawMetadata; - const char * getRevisionIdString() const { - return revisionId.c_str(); - } + const char * getRevisionIdString() const; + const char * getRevisionDateString() const; - const char * getRevisionDateString() const { - return revisionDate.c_str(); - } + const std::vector& getPluginNames() const; - const std::vector& getPluginNames() const { - return cPluginNames; - } + const std::vector& getBashTagMap() const; + unsigned int getBashTagUid(const std::string& name) const; - const std::vector& getBashTagMap() const { - return cBashTagMap; - } + const std::vector& getAddedTagIds() const; + const std::vector& getRemovedTagIds() const; - unsigned int getBashTagUid(const std::string& name) const { - auto it = bashTagMap.find(name); - if (it != end(bashTagMap)) - return it->second; + const std::vector& getPluginMessages() const; - throw loot::error(loot::error::no_tag_map, "The Bash Tag \"" + name + "\" does not exist in the Bash Tag map."); - } - - const std::vector& getAddedTagIds() const { - return addedTagIds; - } - - const std::vector& getRemovedTagIds() const { - return removedTagIds; - } - - const std::vector& getPluginMessages() const { - return cPluginMessages; - } - - void setRevisionIdString(const std::string& str) { - revisionId = str; - } - - void setRevisionDateString(const std::string& str) { - revisionDate = str; - } + void setRevisionIdString(const std::string& str); + void setRevisionDateString(const std::string& str); template void setPluginNames(const T& plugins) { @@ -106,65 +76,15 @@ struct loot_db : public loot::Game { }); } - template - void setAddedTags(const T& names) { - for (const auto& name : names) - addedTagIds.push_back(getBashTagUid(name)); - } + void setAddedTags(const std::set& names); + void setRemovedTags(const std::set& names); - template - void setRemovedTags(const T& names) { - for (const auto& name : names) - removedTagIds.push_back(getBashTagUid(name)); - } + void setPluginMessages(const std::list& pluginMessages); - template - void setPluginMessages(const T& pluginMessages) { - cPluginMessages.resize(pluginMessages.size()); - pluginMessageStrings.resize(pluginMessages.size()); + void addBashTagsToMap(std::set names); - size_t i = 0; - for (const auto& message : pluginMessages) { - pluginMessageStrings[i] = message.ChooseContent(loot::Language::any).Str(); - - cPluginMessages[i].type = message.Type(); - cPluginMessages[i].message = pluginMessageStrings[i].c_str(); - - ++i; - } - } - - void addBashTagsToMap(std::set names) { - for (const auto& name : names) { - // Try adding the Bash Tag to the map assuming it's not already in - // there, then use the UID in the returned value, as that will be - // equal to the value in the map, even if the Bash Tag was already - // present. - unsigned int uid = bashTagMap.size(); - // If the tag already exists in the map, do - auto it = bashTagMap.emplace(name, uid).first; - if (it->second == cBashTagMap.size()) - cBashTagMap.push_back(it->first.c_str()); - else - cBashTagMap.at(it->second) = it->first.c_str(); - } - } - - void clearBashTagMap() { - bashTagMap.clear(); - cBashTagMap.clear(); - } - - void clearArrays() { - pluginNames.clear(); - cPluginNames.clear(); - - addedTagIds.clear(); - removedTagIds.clear(); - - cPluginMessages.clear(); - pluginMessageStrings.clear(); - } + void clearBashTagMap(); + void clearArrays(); private: std::string revisionId; std::string revisionDate;