diff --git a/src/common/utilities.hpp b/src/common/utilities.hpp index 6473411053..d2f61b4e32 100644 --- a/src/common/utilities.hpp +++ b/src/common/utilities.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include // Class used to perform time measurement class cScopeTimer { @@ -11,3 +12,17 @@ class cScopeTimer { }; int levenshtein( const std::string &s1, const std::string &s2 ); + +namespace rathena { + namespace util { + template V* map_find( std::map& map, K key ){ + auto it = map.find( key ); + + if( it != map.end() ){ + return &it->second; + }else{ + return nullptr; + } + } + } +} diff --git a/src/map/mercenary.cpp b/src/map/mercenary.cpp index dc85e6d781..c0c78b8451 100644 --- a/src/map/mercenary.cpp +++ b/src/map/mercenary.cpp @@ -7,6 +7,8 @@ #include #include +#include "../common/utilities.hpp" + #include "../common/cbasetypes.h" #include "../common/malloc.h" #include "../common/timer.h" @@ -26,6 +28,8 @@ #include "trade.hpp" #include "npc.hpp" +using namespace rathena; + std::map mercenary_db_data; /** @@ -34,11 +38,7 @@ std::map mercenary_db_data; * @return A pointer to the mercenary db entry or nullptr if not found **/ struct s_mercenary_db *mercenary_db( uint16 class_ ){ - if( mercenary_db_data.find(class_) != mercenary_db_data.end() ){ - return &mercenary_db_data.at(class_); - }else{ - return nullptr; - } + return util::map_find( mercenary_db_data, class_ ); } /** diff --git a/src/map/mob.cpp b/src/map/mob.cpp index 22791c68e3..e8a744c9ca 100644 --- a/src/map/mob.cpp +++ b/src/map/mob.cpp @@ -7,6 +7,8 @@ #include #include +#include "../common/utilities.hpp" + #include "../common/cbasetypes.h" #include "../common/timer.h" #include "../common/db.h" @@ -40,6 +42,8 @@ #include #include +using namespace rathena; + #define ACTIVE_AI_RANGE 2 //Distance added on top of 'AREA_SIZE' at which mobs enter active AI mode. #define IDLE_SKILL_INTERVAL 10 //Active idle skills should be triggered every 1 second (1000/MIN_MOBTHINKTIME) @@ -76,11 +80,7 @@ //Dynamic mob database std::map mob_db_data; struct mob_db *mob_db( int mob_id ){ - if( mob_db_data.find( mob_id ) != mob_db_data.end() ){ - return &mob_db_data.at(mob_id); - }else{ - return nullptr; - } + return util::map_find( mob_db_data, (uint16)mob_id ); } // holds Monster Spawn informations @@ -89,11 +89,7 @@ std::unordered_map> mob_spawn_data; //Dynamic mob chat database std::map mob_chat_db; struct mob_chat *mob_chat(short id) { - if( mob_chat_db.find(id) != mob_chat_db.end() ){ - return &mob_chat_db.at(id); - }else{ - return nullptr; - } + return util::map_find( mob_chat_db, id ); } //Dynamic item drop ratio database for per-item drop ratio modifiers overriding global drop ratios. diff --git a/src/map/pet.cpp b/src/map/pet.cpp index 179b46aaad..f6d3290998 100644 --- a/src/map/pet.cpp +++ b/src/map/pet.cpp @@ -7,6 +7,8 @@ #include +#include "../common/utilities.hpp" + #include "../common/db.h" #include "../common/timer.h" #include "../common/nullpo.h" @@ -27,16 +29,14 @@ #include "log.hpp" #include "achievement.hpp" +using namespace rathena; + #define MIN_PETTHINKTIME 100 //Dynamic pet database std::map pet_db_data; struct s_pet_db *pet_db( uint16 pet_id ){ - if( pet_db_data.find(pet_id) != pet_db_data.end() ){ - return &pet_db_data.at(pet_id); - }else{ - return nullptr; - } + return util::map_find( pet_db_data, pet_id ); } static struct eri *item_drop_ers; //For loot drops delay structures. diff --git a/src/map/storage.cpp b/src/map/storage.cpp index fade48bf55..7da5b791ef 100644 --- a/src/map/storage.cpp +++ b/src/map/storage.cpp @@ -8,6 +8,8 @@ #include #include +#include "../common/utilities.hpp" + #include "../common/cbasetypes.h" #include "../common/nullpo.h" #include "../common/malloc.h" @@ -24,6 +26,8 @@ #include "log.hpp" #include "battle.hpp" +using namespace rathena; + ///Databases of guild_storage : int guild_id -> struct guild_storage std::map guild_storage_db; @@ -562,11 +566,7 @@ struct s_storage *guild2storage(int guild_id) * @return s_storage or nullptr */ struct s_storage *guild2storage2(int guild_id){ - if( guild_storage_db.find(guild_id) != guild_storage_db.end() ){ - return &guild_storage_db[guild_id]; - }else{ - return nullptr; - } + return util::map_find( guild_storage_db, guild_id ); } /**