Browse Source

Added a template function for std::map search (#3107)

Thanks to @secretdataz for his help.
Lemongrass3110 7 năm trước cách đây
mục cha
commit
0a84542220
5 tập tin đã thay đổi với 36 bổ sung25 xóa
  1. 15 0
      src/common/utilities.hpp
  2. 5 5
      src/map/mercenary.cpp
  3. 6 10
      src/map/mob.cpp
  4. 5 5
      src/map/pet.cpp
  5. 5 5
      src/map/storage.cpp

+ 15 - 0
src/common/utilities.hpp

@@ -1,6 +1,7 @@
 #pragma once
 #include <memory>
 #include <string>
+#include <map>
 
 // 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 <typename K, typename V> V* map_find( std::map<K,V>& map, K key ){
+			auto it = map.find( key );
+
+			if( it != map.end() ){
+				return &it->second;
+			}else{
+				return nullptr;
+			}
+		}
+	}
+}

+ 5 - 5
src/map/mercenary.cpp

@@ -7,6 +7,8 @@
 #include <map>
 #include <math.h>
 
+#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<uint16, struct s_mercenary_db> mercenary_db_data;
 
 /**
@@ -34,11 +38,7 @@ std::map<uint16, struct s_mercenary_db> 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_ );
 }
 
 /**

+ 6 - 10
src/map/mob.cpp

@@ -7,6 +7,8 @@
 #include <map>
 #include <math.h>
 
+#include "../common/utilities.hpp"
+
 #include "../common/cbasetypes.h"
 #include "../common/timer.h"
 #include "../common/db.h"
@@ -40,6 +42,8 @@
 #include <unordered_map>
 #include <algorithm>
 
+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<uint16, struct mob_db> 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<uint16, std::vector<spawn_info>> mob_spawn_data;
 //Dynamic mob chat database
 std::map<short,struct mob_chat> 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.

+ 5 - 5
src/map/pet.cpp

@@ -7,6 +7,8 @@
 
 #include <stdlib.h>
 
+#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<uint16, struct s_pet_db> 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.

+ 5 - 5
src/map/storage.cpp

@@ -8,6 +8,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#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<int, struct s_storage> 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 );
 }
 
 /**