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 "Pos.h"
|
||||||
#include "WEvent.h"
|
#include "WEvent.h"
|
||||||
|
|
||||||
void trash(Pos*);
|
template <class T> void trash(T*);
|
||||||
|
|
||||||
class Trash
|
class Trash
|
||||||
{
|
{
|
||||||
std::vector<Pos*> bin;
|
|
||||||
void put_out();
|
|
||||||
int receiveEvent(WEvent* e);
|
|
||||||
friend void trash(Pos*);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static void cleanup();
|
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_
|
#endif // _TRASH_H_
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "../include/GuiCombat.h"
|
#include "../include/GuiCombat.h"
|
||||||
#include "../include/AIPlayer.h"
|
#include "../include/AIPlayer.h"
|
||||||
#include "../include/GameObserver.h"
|
#include "../include/GameObserver.h"
|
||||||
|
#include "../include/Trash.h"
|
||||||
#include "Closest.cpp"
|
#include "Closest.cpp"
|
||||||
|
|
||||||
static const float MARGIN = 70;
|
static const float MARGIN = 70;
|
||||||
@@ -386,7 +387,7 @@ int GuiCombat::receiveEventMinus(WEvent* e)
|
|||||||
AttackerDamaged* d = *it;
|
AttackerDamaged* d = *it;
|
||||||
if (activeAtk == *it) activeAtk = NULL;
|
if (activeAtk == *it) activeAtk = NULL;
|
||||||
attackers.erase(it);
|
attackers.erase(it);
|
||||||
SAFE_DELETE(d);
|
trash(d);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -395,7 +396,7 @@ int GuiCombat::receiveEventMinus(WEvent* e)
|
|||||||
{
|
{
|
||||||
DefenserDamaged* d = *q;
|
DefenserDamaged* d = *q;
|
||||||
(*it)->blockers.erase(q);
|
(*it)->blockers.erase(q);
|
||||||
SAFE_DELETE(d);
|
trash(d);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -408,7 +409,7 @@ int GuiCombat::receiveEventMinus(WEvent* e)
|
|||||||
{
|
{
|
||||||
AttackerDamaged* d = *it;
|
AttackerDamaged* d = *it;
|
||||||
attackers.erase(it);
|
attackers.erase(it);
|
||||||
SAFE_DELETE(d);
|
trash(d);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -422,7 +423,7 @@ int GuiCombat::receiveEventMinus(WEvent* e)
|
|||||||
{
|
{
|
||||||
DefenserDamaged* d = *q;
|
DefenserDamaged* d = *q;
|
||||||
(*it)->blockers.erase(q);
|
(*it)->blockers.erase(q);
|
||||||
SAFE_DELETE(d);
|
trash(d);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+21
-12
@@ -2,28 +2,37 @@
|
|||||||
#include "../include/MTGDefinitions.h"
|
#include "../include/MTGDefinitions.h"
|
||||||
#include "../include/Pos.h"
|
#include "../include/Pos.h"
|
||||||
#include "../include/CardGui.h"
|
#include "../include/CardGui.h"
|
||||||
|
#include "../include/DamagerDamaged.h"
|
||||||
#include "../include/Trash.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)
|
for (typename std::vector<T*>::iterator it = bin.begin(); it != bin.end(); ++it)
|
||||||
{
|
SAFE_DELETE(*it);
|
||||||
if (CardView *c = dynamic_cast<CardView*>(*it))
|
|
||||||
SAFE_DELETE(c);
|
|
||||||
else
|
|
||||||
SAFE_DELETE(*it);
|
|
||||||
}
|
|
||||||
bin.clear();
|
bin.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
static Trash PosTrash;
|
static TrashBin<CardView> CardViewTrash;
|
||||||
|
static TrashBin<DefenserDamaged> DefenserDamagedTrash;
|
||||||
|
static TrashBin<AttackerDamaged> AttackerDamagedTrash;
|
||||||
|
|
||||||
void Trash::cleanup()
|
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