J :
* Fix a segfault case. * Improve the trash system to handle several types more gracefully.
This commit is contained in:
@@ -5,17 +5,21 @@
|
||||
#include "Pos.h"
|
||||
#include "WEvent.h"
|
||||
|
||||
void trash(Pos*);
|
||||
|
||||
template <class T> void trash(T*);
|
||||
class Trash
|
||||
{
|
||||
std::vector<Pos*> bin;
|
||||
void put_out();
|
||||
int receiveEvent(WEvent* e);
|
||||
friend void trash(Pos*);
|
||||
|
||||
public:
|
||||
static void cleanup();
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class TrashBin
|
||||
{
|
||||
std::vector<T*> bin;
|
||||
void put_out();
|
||||
int receiveEvent(WEvent* e);
|
||||
template <class Q> friend void trash(Q*);
|
||||
friend class Trash;
|
||||
};
|
||||
|
||||
#endif // _TRASH_H_
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "../include/GuiCombat.h"
|
||||
#include "../include/AIPlayer.h"
|
||||
#include "../include/GameObserver.h"
|
||||
#include "../include/Trash.h"
|
||||
#include "Closest.cpp"
|
||||
|
||||
static const float MARGIN = 70;
|
||||
@@ -386,7 +387,7 @@ int GuiCombat::receiveEventMinus(WEvent* e)
|
||||
AttackerDamaged* d = *it;
|
||||
if (activeAtk == *it) activeAtk = NULL;
|
||||
attackers.erase(it);
|
||||
SAFE_DELETE(d);
|
||||
trash(d);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
@@ -395,7 +396,7 @@ int GuiCombat::receiveEventMinus(WEvent* e)
|
||||
{
|
||||
DefenserDamaged* d = *q;
|
||||
(*it)->blockers.erase(q);
|
||||
SAFE_DELETE(d);
|
||||
trash(d);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
@@ -408,7 +409,7 @@ int GuiCombat::receiveEventMinus(WEvent* e)
|
||||
{
|
||||
AttackerDamaged* d = *it;
|
||||
attackers.erase(it);
|
||||
SAFE_DELETE(d);
|
||||
trash(d);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
@@ -422,7 +423,7 @@ int GuiCombat::receiveEventMinus(WEvent* e)
|
||||
{
|
||||
DefenserDamaged* d = *q;
|
||||
(*it)->blockers.erase(q);
|
||||
SAFE_DELETE(d);
|
||||
trash(d);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -2,28 +2,37 @@
|
||||
#include "../include/MTGDefinitions.h"
|
||||
#include "../include/Pos.h"
|
||||
#include "../include/CardGui.h"
|
||||
#include "../include/DamagerDamaged.h"
|
||||
#include "../include/Trash.h"
|
||||
|
||||
void Trash::put_out()
|
||||
template <class T>
|
||||
void TrashBin<T>::put_out()
|
||||
{
|
||||
for (std::vector<Pos*>::iterator it = bin.begin(); it != bin.end(); ++it)
|
||||
{
|
||||
if (CardView *c = dynamic_cast<CardView*>(*it))
|
||||
SAFE_DELETE(c);
|
||||
else
|
||||
SAFE_DELETE(*it);
|
||||
}
|
||||
for (typename std::vector<T*>::iterator it = bin.begin(); it != bin.end(); ++it)
|
||||
SAFE_DELETE(*it);
|
||||
bin.clear();
|
||||
}
|
||||
|
||||
static Trash PosTrash;
|
||||
static TrashBin<CardView> CardViewTrash;
|
||||
static TrashBin<DefenserDamaged> DefenserDamagedTrash;
|
||||
static TrashBin<AttackerDamaged> AttackerDamagedTrash;
|
||||
|
||||
void Trash::cleanup()
|
||||
{
|
||||
PosTrash.put_out();
|
||||
CardViewTrash.put_out();
|
||||
DefenserDamagedTrash.put_out();
|
||||
AttackerDamagedTrash.put_out();
|
||||
}
|
||||
|
||||
void trash(Pos* garbage)
|
||||
template<> void trash(CardView* garbage)
|
||||
{
|
||||
PosTrash.bin.push_back(garbage);
|
||||
CardViewTrash.bin.push_back(garbage);
|
||||
}
|
||||
template<> void trash(DefenserDamaged* garbage)
|
||||
{
|
||||
DefenserDamagedTrash.bin.push_back(garbage);
|
||||
}
|
||||
template<> void trash(AttackerDamaged* garbage)
|
||||
{
|
||||
AttackerDamagedTrash.bin.push_back(garbage);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user