Core/WeatherMgr: singleton converted to namespace with free functions. Note how RAII saves your life and removes the need in explicit delete :)

This commit is contained in:
Spp
2011-09-28 11:38:56 +02:00
parent cc06c41887
commit b93067350c
5 changed files with 52 additions and 70 deletions
+2 -2
View File
@@ -2372,10 +2372,10 @@ bool ChatHandler::HandleChangeWeather(const char *args)
Player* player = m_session->GetPlayer(); Player* player = m_session->GetPlayer();
uint32 zoneid = player->GetZoneId(); uint32 zoneid = player->GetZoneId();
Weather* wth = sWeatherMgr->FindWeather(zoneid); Weather* wth = WeatherMgr::FindWeather(zoneid);
if (!wth) if (!wth)
wth = sWeatherMgr->AddWeather(zoneid); wth = WeatherMgr::AddWeather(zoneid);
if (!wth) if (!wth)
{ {
SendSysMessage(LANG_NO_WEATHER); SendSysMessage(LANG_NO_WEATHER);
+2 -2
View File
@@ -7426,12 +7426,12 @@ void Player::UpdateZone(uint32 newZone, uint32 newArea)
if (sWorld->getBoolConfig(CONFIG_WEATHER)) if (sWorld->getBoolConfig(CONFIG_WEATHER))
{ {
Weather* wth = sWeatherMgr->FindWeather(zone->ID); Weather* wth = WeatherMgr::FindWeather(zone->ID);
if (wth) if (wth)
wth->SendWeatherUpdateToPlayer(this); wth->SendWeatherUpdateToPlayer(this);
else else
{ {
if (!sWeatherMgr->AddWeather(zone->ID)) if (!WeatherMgr::AddWeather(zone->ID))
{ {
// send fine weather packet to remove old zone's weather // send fine weather packet to remove old zone's weather
Weather::SendFineWeatherUpdateToPlayer(this); Weather::SendFineWeatherUpdateToPlayer(this);
+35 -27
View File
@@ -21,66 +21,75 @@
*/ */
#include "WeatherMgr.h" #include "WeatherMgr.h"
#include "Weather.h"
#include "Log.h" #include "Log.h"
#include "ObjectMgr.h" #include "ObjectMgr.h"
#include <ace/Refcounted_Auto_Ptr.h>
WeatherMgr::~WeatherMgr() namespace WeatherMgr
{ {
///- Empty the WeatherMap
for (WeatherMap::const_iterator itr = m_weathers.begin(); itr != m_weathers.end(); ++itr)
delete itr->second;
m_weathers.clear(); namespace
{
typedef UNORDERED_MAP<uint32, ACE_Refcounted_Auto_Ptr<Weather, ACE_Null_Mutex> > WeatherMap;
typedef UNORDERED_MAP<uint32, WeatherData> WeatherZoneMap;
WeatherMap m_weathers;
WeatherZoneMap mWeatherZoneMap;
WeatherData const* GetWeatherData(uint32 zone_id)
{
WeatherZoneMap::const_iterator itr = mWeatherZoneMap.find(zone_id);
return (itr != mWeatherZoneMap.end()) ? &itr->second : NULL;
}
} }
/// Find a Weather object by the given zoneid /// Find a Weather object by the given zoneid
Weather* WeatherMgr::FindWeather(uint32 id) const Weather* FindWeather(uint32 id)
{ {
WeatherMap::const_iterator itr = m_weathers.find(id); WeatherMap::const_iterator itr = m_weathers.find(id);
return (itr != m_weathers.end()) ? itr->second.get() : 0;
if (itr != m_weathers.end())
return itr->second;
else
return 0;
} }
/// Remove a Weather object for the given zoneid /// Remove a Weather object for the given zoneid
void WeatherMgr::RemoveWeather(uint32 id) void RemoveWeather(uint32 id)
{ {
// not called at the moment. Kept for completeness // not called at the moment. Kept for completeness
WeatherMap::iterator itr = m_weathers.find(id); WeatherMap::iterator itr = m_weathers.find(id);
if (itr != m_weathers.end()) if (itr != m_weathers.end())
{
delete itr->second;
m_weathers.erase(itr); m_weathers.erase(itr);
}
} }
/// Add a Weather object to the list /// Add a Weather object to the list
Weather* WeatherMgr::AddWeather(uint32 zone_id) Weather* AddWeather(uint32 zone_id)
{ {
WeatherData const* weatherChances = GetWeatherChances(zone_id); WeatherData const* weatherChances = GetWeatherData(zone_id);
// zone not have weather, ignore // zone does not have weather, ignore
if (!weatherChances) if (!weatherChances)
return NULL; return NULL;
Weather* w = new Weather(zone_id, weatherChances); Weather* w = new Weather(zone_id, weatherChances);
m_weathers[w->GetZone()] = w; m_weathers[w->GetZone()].reset(w);
w->ReGenerate(); w->ReGenerate();
w->UpdateWeather(); w->UpdateWeather();
return w; return w;
} }
void WeatherMgr::LoadWeatherData() void LoadWeatherData()
{ {
uint32 oldMSTime = getMSTime(); uint32 oldMSTime = getMSTime();
uint32 count = 0; uint32 count = 0;
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 QueryResult result = WorldDatabase.Query("SELECT "
QueryResult result = WorldDatabase.Query("SELECT zone, spring_rain_chance, spring_snow_chance, spring_storm_chance, summer_rain_chance, summer_snow_chance, summer_storm_chance, fall_rain_chance, fall_snow_chance, fall_storm_chance, winter_rain_chance, winter_snow_chance, winter_storm_chance, ScriptName FROM game_weather"); "zone, spring_rain_chance, spring_snow_chance, spring_storm_chance,"
"summer_rain_chance, summer_snow_chance, summer_storm_chance,"
"fall_rain_chance, fall_snow_chance, fall_storm_chance,"
"winter_rain_chance, winter_snow_chance, winter_storm_chance,"
"ScriptName FROM game_weather");
if (!result) if (!result)
{ {
@@ -132,7 +141,7 @@ void WeatherMgr::LoadWeatherData()
sLog->outString(); sLog->outString();
} }
void WeatherMgr::Update(uint32 diff) void Update(uint32 diff)
{ {
///- Send an update signal to Weather objects ///- Send an update signal to Weather objects
WeatherMap::iterator itr, next; WeatherMap::iterator itr, next;
@@ -142,11 +151,10 @@ void WeatherMgr::Update(uint32 diff)
++next; ++next;
///- and remove Weather objects for zones with no player ///- and remove Weather objects for zones with no player
//As interval > WorldTick // As interval > WorldTick
if (!itr->second->Update(diff)) if (!itr->second->Update(diff))
{
delete itr->second;
m_weathers.erase(itr); m_weathers.erase(itr);
}
} }
} }
} // namespace
+11 -37
View File
@@ -23,46 +23,20 @@
#ifndef __WEATHERMGR_H #ifndef __WEATHERMGR_H
#define __WEATHERMGR_H #define __WEATHERMGR_H
#include "Common.h" #include "Define.h"
#include "SharedDefines.h"
#include "Timer.h"
#include "Weather.h"
#include <ace/Singleton.h>
class WeatherMgr class Weather;
class Player;
namespace WeatherMgr
{ {
friend class ACE_Singleton<WeatherMgr, ACE_Null_Mutex>; void LoadWeatherData();
WeatherMgr() {}
~WeatherMgr();
public: Weather* FindWeather(uint32 id);
Weather* AddWeather(uint32 zone_id);
void RemoveWeather(uint32 zone_id);
void LoadWeatherData(); void Update(uint32 diff);
}
Weather* FindWeather(uint32 id) const;
Weather* AddWeather(uint32 zone_id);
void RemoveWeather(uint32 zone_id);
WeatherData const* GetWeatherChances(uint32 zone_id) const
{
WeatherZoneMap::const_iterator itr = mWeatherZoneMap.find(zone_id);
if (itr != mWeatherZoneMap.end())
return &itr->second;
else
return NULL;
}
void Update(uint32 diff);
typedef UNORDERED_MAP<uint32, Weather*> WeatherMap;
typedef UNORDERED_MAP<uint32, WeatherData> WeatherZoneMap;
private:
WeatherMap m_weathers;
WeatherZoneMap mWeatherZoneMap;
};
#define sWeatherMgr ACE_Singleton<WeatherMgr, ACE_Null_Mutex>::instance()
#endif #endif
+2 -2
View File
@@ -1399,7 +1399,7 @@ void World::SetInitialWorldSettings()
sObjectMgr->LoadLinkedRespawn(); // must be after LoadCreatures(), LoadGameObjects() sObjectMgr->LoadLinkedRespawn(); // must be after LoadCreatures(), LoadGameObjects()
sLog->outString("Loading Weather Data..."); sLog->outString("Loading Weather Data...");
sWeatherMgr->LoadWeatherData(); WeatherMgr::LoadWeatherData();
sLog->outString("Loading Quests..."); sLog->outString("Loading Quests...");
sObjectMgr->LoadQuests(); // must be loaded after DBCs, creature_template, item_template, gameobject tables sObjectMgr->LoadQuests(); // must be loaded after DBCs, creature_template, item_template, gameobject tables
@@ -1912,7 +1912,7 @@ void World::Update(uint32 diff)
if (m_timers[WUPDATE_WEATHERS].Passed()) if (m_timers[WUPDATE_WEATHERS].Passed())
{ {
m_timers[WUPDATE_WEATHERS].Reset(); m_timers[WUPDATE_WEATHERS].Reset();
sWeatherMgr->Update(uint32(m_timers[WUPDATE_WEATHERS].GetInterval())); WeatherMgr::Update(uint32(m_timers[WUPDATE_WEATHERS].GetInterval()));
} }
/// <li> Update uptime table /// <li> Update uptime table