Use RefCountedPtr<StarSystem> rather than IncRef and DecRef.

master
John Bartholomew 2011-11-15 05:56:55 +00:00
parent 1a54d13870
commit d35da78252
17 changed files with 46 additions and 64 deletions

View File

@ -68,7 +68,7 @@ public:
float ypos = 0;
for (std::list<const Mission*>::const_iterator i = missions.begin(); i != missions.end(); ++i) {
SystemPath path = (*i)->location;
StarSystem *s = StarSystem::GetCached(path);
RefCountedPtr<StarSystem> s = StarSystem::GetCached(path);
l = new Gui::Label((*i)->type);
innerbox->Add(l, 0, ypos);
@ -97,8 +97,6 @@ public:
innerbox->Add(l, 660, ypos);
ypos += YSEP*3;
s->DecRefCount();
}
Add(portal, 20, 20 + YSEP*3);
Add(scroll, 780, 20 + YSEP*3);

View File

@ -52,7 +52,7 @@ static int l_game_meta_index(lua_State *l)
* stable
*/
if (strcmp(key, "system") == 0) {
LuaStarSystem::PushToLua(Pi::currentSystem);
LuaStarSystem::PushToLua(Pi::currentSystem.Get());
return 1;
}

View File

@ -141,10 +141,9 @@ static int l_sbody_attr_parent(lua_State *l)
// sbody->parent is 0 as it was cleared by the acquirer. we need to go
// back to the starsystem proper to get what we need.
StarSystem *s = StarSystem::GetCached(sbody->path);
RefCountedPtr<StarSystem> s = StarSystem::GetCached(sbody->path);
SBody *live_sbody = s->GetBodyByPath(sbody->path);
LuaSBody::PushToLua(live_sbody->parent);
s->DecRefCount();
return 1;
}

View File

@ -254,24 +254,21 @@ static int l_starsystem_get_nearby_systems(lua_State *l)
if (Sector::DistanceBetween(&here_sec, here_idx, &sec, idx) > dist_ly)
continue;
StarSystem *sys = StarSystem::GetCached(SystemPath(x, y, z, idx));
RefCountedPtr<StarSystem> sys = StarSystem::GetCached(SystemPath(x, y, z, idx));
if (filter) {
lua_pushvalue(l, 3);
LuaStarSystem::PushToLua(sys);
LuaStarSystem::PushToLua(sys.Get());
lua_call(l, 1, 1);
if (!lua_toboolean(l, -1)) {
lua_pop(l, 1);
sys->DecRefCount();
continue;
}
lua_pop(l, 1);
}
lua_pushinteger(l, lua_objlen(l, -1)+1);
LuaStarSystem::PushToLua(sys);
LuaStarSystem::PushToLua(sys.Get());
lua_rawset(l, -3);
sys->DecRefCount();
}
}
}

View File

@ -266,9 +266,10 @@ static int l_sbodypath_distance_to(lua_State *l)
static int l_sbodypath_get_star_system(lua_State *l)
{
SystemPath *path = LuaSystemPath::GetFromLua(1);
StarSystem *s = StarSystem::GetCached(path);
LuaStarSystem::PushToLua(s);
s->DecRefCount();
RefCountedPtr<StarSystem> s = StarSystem::GetCached(path);
// LuaStarSystem shares ownership of the StarSystem,
// because LuaAcquirer<LuaStarSystem> uses IncRefCount and DecRefCount
LuaStarSystem::PushToLua(s.Get());
return 1;
}
@ -294,10 +295,9 @@ static int l_sbodypath_get_star_system(lua_State *l)
static int l_sbodypath_get_system_body(lua_State *l)
{
SystemPath *path = LuaSystemPath::GetFromLua(1);
StarSystem *s = StarSystem::GetCached(path);
RefCountedPtr<StarSystem> s = StarSystem::GetCached(path);
SBody *sbody = s->GetBodyByPath(path);
LuaSBody::PushToLua(sbody);
s->DecRefCount();
return 1;
}

View File

@ -121,8 +121,8 @@ SystemView *Pi::systemView;
SystemInfoView *Pi::systemInfoView;
ShipCpanel *Pi::cpan;
LuaConsole *Pi::luaConsole;
StarSystem *Pi::selectedSystem;
StarSystem *Pi::currentSystem;
RefCountedPtr<StarSystem> Pi::selectedSystem;
RefCountedPtr<StarSystem> Pi::currentSystem;
MTRand Pi::rng;
double Pi::gameTime;
float Pi::frameTime;
@ -1057,7 +1057,7 @@ void Pi::UninitGame()
delete Pi::player;
Pi::player = 0;
}
if (Pi::selectedSystem) Pi::selectedSystem->DecRefCount();
Pi::selectedSystem.Reset();
StarSystem::ShrinkCache();
}
@ -1481,14 +1481,14 @@ void Pi::MainLoop()
}
}
StarSystem *Pi::GetSelectedSystem()
RefCountedPtr<StarSystem> Pi::GetSelectedSystem()
{
SystemPath selectedPath = Pi::sectorView->GetSelectedSystem();
if (selectedSystem) {
if (selectedSystem->GetPath().IsSameSystem(selectedPath))
return selectedSystem;
selectedSystem->DecRefCount();
selectedSystem.Reset();
}
selectedSystem = StarSystem::GetCached(selectedPath);
@ -1501,12 +1501,12 @@ void Pi::Serialize(Serializer::Writer &wr)
Serializer::IndexFrames();
Serializer::IndexBodies();
Serializer::IndexSystemBodies(currentSystem);
Serializer::IndexSystemBodies(currentSystem.Get());
section = Serializer::Writer();
section.Double(gameTime);
StarSystem::Serialize(section, selectedSystem);
StarSystem::Serialize(section, currentSystem);
StarSystem::Serialize(section, selectedSystem.Get());
StarSystem::Serialize(section, currentSystem.Get());
wr.WrSection("PiMisc", section.GetData());
section = Serializer::Writer();

View File

@ -143,7 +143,7 @@ public:
static void SetView(View *v);
static View *GetView() { return currentView; }
static StarSystem *GetSelectedSystem();
static RefCountedPtr<StarSystem> GetSelectedSystem();
#if DEVKEYS
static bool showDebugInfo;
@ -160,7 +160,7 @@ public:
static LuaConsole *luaConsole;
static ShipCpanel *cpan;
static GLUquadric *gluQuadric;
static StarSystem *currentSystem;
static RefCountedPtr<StarSystem> currentSystem;
static Sound::MusicPlayer &GetMusicPlayer() { return musicPlayer; }
#if OBJECTVIEWER
@ -185,7 +185,7 @@ private:
* factor between one physics tick and another [0.0-1.0]
*/
static float gameTickAlpha;
static StarSystem *selectedSystem;
static RefCountedPtr<StarSystem> selectedSystem;
static int timeAccelIdx;
static int requestedTimeAccelIdx;
static bool forceTimeAccel;

View File

@ -441,7 +441,7 @@ void SectorView::UpdateSystemLabels(SystemLabels &labels, const SystemPath &path
}
}
StarSystem *sys = StarSystem::GetCached(path);
RefCountedPtr<StarSystem> sys = StarSystem::GetCached(path);
std::string desc;
if (sys->GetNumStars() == 4) {
@ -458,8 +458,6 @@ void SectorView::UpdateSystemLabels(SystemLabels &labels, const SystemPath &path
labels.systemName->SetText(sys->GetName());
labels.shortDesc->SetText(sys->GetShortDescription());
sys->DecRefCount();
if (m_infoBoxVisible)
m_infoBox->ShowAll();
}
@ -525,7 +523,7 @@ void SectorView::DrawSector(int sx, int sy, int sz, const vector3f &playerAbsPos
fabs(m_posMovingTo.z - m_pos.z));
// Ideally, since this takes so f'ing long, it wants to be done as a threaded job but haven't written that yet.
if( !(*i).IsSetInhabited() && diff.x < 0.001f && diff.y < 0.001f && diff.z < 0.001f ) {
StarSystem* pSS = StarSystem::GetCached(current);
RefCountedPtr<StarSystem> pSS = StarSystem::GetCached(current);
if( (!pSS->m_unexplored) && (pSS->m_spaceStations.size()>0) )
{
(*i).SetInhabited(true);
@ -534,7 +532,6 @@ void SectorView::DrawSector(int sx, int sy, int sz, const vector3f &playerAbsPos
{
(*i).SetInhabited(false);
}
pSS->DecRefCount();
}
}

View File

@ -43,7 +43,7 @@ void Init()
void Uninit()
{
delete rootFrame;
if (Pi::currentSystem) Pi::currentSystem->DecRefCount();
Pi::currentSystem.Reset();
}
void Clear()
@ -161,7 +161,7 @@ void Serialize(Serializer::Writer &wr)
void Unserialize(Serializer::Reader &rd)
{
Serializer::IndexSystemBodies(Pi::currentSystem);
Serializer::IndexSystemBodies(Pi::currentSystem.Get());
Serializer::Reader rd2 = rd.RdSection("Frames");
rootFrame = Frame::Unserialize(rd2, 0);
@ -739,7 +739,6 @@ void DoHyperspaceTo(const SystemPath *dest)
const SystemPath psource = Pi::currentSystem ? Pi::currentSystem->GetPath() : SystemPath(0,0,0,0);
const SystemPath pdest = dest->SystemOnly();
if (Pi::currentSystem) Pi::currentSystem->DecRefCount();
Pi::currentSystem = StarSystem::GetCached(dest);
Space::Clear();
Space::BuildSystem();
@ -867,7 +866,6 @@ void DoHyperspaceTo(const SystemPath *dest)
/* called at game start to load the system and put the player in a starport */
void SetupSystemForGameStart(const SystemPath *dest, int starport, int port)
{
if (Pi::currentSystem) Pi::currentSystem->DecRefCount();
Pi::currentSystem = StarSystem::GetCached(dest);
Space::Clear();
Space::BuildSystem();

View File

@ -672,7 +672,7 @@ bool SpaceStation::CanSell(Equip::Type t, bool verbose) const {
return result;
}
bool SpaceStation::DoesSell(Equip::Type t) const {
return Polit::IsCommodityLegal(Pi::currentSystem, t);
return Polit::IsCommodityLegal(Pi::currentSystem.Get(), t);
}
Sint64 SpaceStation::GetPrice(Equip::Type t) const {

View File

@ -2011,7 +2011,7 @@ void StarSystem::Serialize(Serializer::Writer &wr, StarSystem *s)
}
}
StarSystem *StarSystem::Unserialize(Serializer::Reader &rd)
RefCountedPtr<StarSystem> StarSystem::Unserialize(Serializer::Reader &rd)
{
if (rd.Byte()) {
int sec_x = rd.Int32();
@ -2020,14 +2020,14 @@ StarSystem *StarSystem::Unserialize(Serializer::Reader &rd)
int sys_idx = rd.Int32();
return StarSystem::GetCached(SystemPath(sec_x, sec_y, sec_z, sys_idx));
} else {
return 0;
return RefCountedPtr<StarSystem>(0);
}
}
typedef std::map<SystemPath,StarSystem*> SystemCacheMap;
static SystemCacheMap s_cachedSystems;
StarSystem *StarSystem::GetCached(const SystemPath &path)
RefCountedPtr<StarSystem> StarSystem::GetCached(const SystemPath &path)
{
StarSystem *s = 0;
@ -2042,9 +2042,7 @@ StarSystem *StarSystem::GetCached(const SystemPath &path)
s_cachedSystems.insert( SystemCacheMap::value_type(sysPath, s) );
}
// bump up the ref count so that the caller doesn't have to
s->IncRefCount();
return s;
return RefCountedPtr<StarSystem>(s);
}
void StarSystem::ShrinkCache()
@ -2052,8 +2050,9 @@ void StarSystem::ShrinkCache()
std::map<SystemPath,StarSystem*>::iterator i = s_cachedSystems.begin();
while (i != s_cachedSystems.end()) {
StarSystem *s = (*i).second;
assert(s->GetRefCount() >= 1); // sanity check
// if the cache is the only owner, then delete it
if (s->GetRefCount() <= 1) {
if (s->GetRefCount() == 1) {
delete s;
s_cachedSystems.erase(i++);
}

View File

@ -184,14 +184,14 @@ class StarSystem : public DeleteEmitter, public RefCounted {
public:
friend class SBody;
static StarSystem *GetCached(const SystemPath &path);
static RefCountedPtr<StarSystem> GetCached(const SystemPath &path);
static void ShrinkCache();
const std::string &GetName() const { return m_name; }
SystemPath GetPathOf(const SBody *sbody) const;
SBody *GetBodyByPath(const SystemPath &path) const;
static void Serialize(Serializer::Writer &wr, StarSystem *);
static StarSystem *Unserialize(Serializer::Reader &rd);
static RefCountedPtr<StarSystem> Unserialize(Serializer::Reader &rd);
void Dump();
const SystemPath &GetPath() const { return m_path; }
const char *GetShortDescription() const { return m_shortDesc.c_str(); }

View File

@ -13,7 +13,6 @@
SystemInfoView::SystemInfoView()
{
SetTransparency(true);
m_system = 0;
m_refresh = false;
}
@ -107,7 +106,7 @@ void SystemInfoView::OnBodyViewed(SBody *b)
void SystemInfoView::UpdateEconomyTab()
{
/* Economy info page */
StarSystem *s = m_system;
StarSystem *s = m_system.Get();
std::string data;
/* if (s->m_econType) {
@ -222,7 +221,7 @@ void SystemInfoView::OnClickBackground(Gui::MouseButtonEvent *e)
}
}
void SystemInfoView::SystemChanged(StarSystem *s)
void SystemInfoView::SystemChanged(const RefCountedPtr<StarSystem> &s)
{
DeleteAllChildren();
@ -349,13 +348,13 @@ void SystemInfoView::SystemChanged(StarSystem *s)
col2->Add(new Gui::Label(m_system->GetShortDescription()), 0, 0);
col1->Add((new Gui::Label(Lang::GOVERNMENT_TYPE))->Color(1,1,0), 0, YSEP);
col2->Add(new Gui::Label(Polit::GetGovernmentDesc(m_system)), 0, YSEP);
col2->Add(new Gui::Label(Polit::GetGovernmentDesc(m_system.Get())), 0, YSEP);
col1->Add((new Gui::Label(Lang::ECONOMY_TYPE))->Color(1,1,0), 0, 2*YSEP);
col2->Add(new Gui::Label(Polit::GetEconomicDesc(m_system)), 0, 2*YSEP);
col2->Add(new Gui::Label(Polit::GetEconomicDesc(m_system.Get())), 0, 2*YSEP);
col1->Add((new Gui::Label(Lang::ALLEGIANCE))->Color(1,1,0), 0, 3*YSEP);
col2->Add(new Gui::Label(Polit::GetAllegianceDesc(m_system)), 0, 3*YSEP);
col2->Add(new Gui::Label(Polit::GetAllegianceDesc(m_system.Get())), 0, 3*YSEP);
col1->Add((new Gui::Label(Lang::POPULATION))->Color(1,1,0), 0, 4*YSEP);
std::string popmsg;

View File

@ -24,7 +24,7 @@ private:
virtual void Draw();
virtual void OnActivate();
};
void SystemChanged(StarSystem *s);
void SystemChanged(const RefCountedPtr<StarSystem> &s);
void UpdateEconomyTab();
void OnBodyViewed(SBody *b);
void OnBodySelected(SBody *b);
@ -38,7 +38,7 @@ private:
Gui::Label *m_econIllegal;
Gui::Fixed *m_sbodyInfoTab, *m_econInfoTab;
Gui::Tabbed *m_tabs;
StarSystem *m_system;
RefCountedPtr<StarSystem> m_system;
bool m_refresh;
//map is not enough to associate icons as each tab has their own
std::vector<std::pair<std::string, BodyIcon*> > m_bodyIcons;

View File

@ -12,7 +12,6 @@ const double SystemView::PICK_OBJECT_RECT_SIZE = 12.0;
SystemView::SystemView()
{
m_system = 0;
SetTransparency(true);
Gui::Screen::PushFont("OverlayFont");
@ -75,7 +74,6 @@ SystemView::SystemView()
SystemView::~SystemView()
{
if (m_system) m_system->DecRefCount();
m_onMouseButtonDown.disconnect();
}
@ -273,8 +271,7 @@ void SystemView::Draw3D()
SystemPath path = Pi::sectorView->GetSelectedSystem();
if (m_system) {
if (!m_system->GetPath().IsSameSystem(path)) {
m_system->DecRefCount();
m_system = 0;
m_system.Reset();
ResetViewpoint();
}
}

View File

@ -28,7 +28,7 @@ private:
void ResetViewpoint();
void MouseButtonDown(int button, int x, int y);
StarSystem *m_system;
RefCountedPtr<StarSystem> m_system;
SBody *m_selectedObject;
float m_rot_x, m_rot_z;
float m_zoom;

View File

@ -495,13 +495,12 @@ void WorldView::RefreshButtonStateAndVisibility()
#endif
if (const SystemPath *dest = Space::GetHyperspaceDest()) {
StarSystem *s = StarSystem::GetCached(*dest);
RefCountedPtr<StarSystem> s = StarSystem::GetCached(*dest);
m_hudVelocity->SetText(stringf(Lang::IN_TRANSIT_TO_N_X_X_X,
formatarg("system", s->GetName()),
formatarg("x", dest->sectorX),
formatarg("y", dest->sectorY),
formatarg("z", dest->sectorZ)));
s->DecRefCount();
m_hudVelocity->Show();
m_hudTargetDist->Hide();
@ -879,9 +878,8 @@ void WorldView::OnHyperspaceTargetChanged()
const SystemPath path = Pi::sectorView->GetHyperspaceTarget();
StarSystem *system = StarSystem::GetCached(path);
RefCountedPtr<StarSystem> system = StarSystem::GetCached(path);
Pi::cpan->MsgLog()->Message("", stringf(Lang::SET_HYPERSPACE_DESTINATION_TO, formatarg("system", system->GetName())));
system->DecRefCount();
int fuelReqd;
double dur;