1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| template<class T>
class SingletonFactory : public Singleton<SingletonFactory>
{
public :
// ...
T::ObjectType* Create()
{
T::ObjectType* pObject = new T::ObjectType();
lObjectList.push_back(pObject);
return pObject;
}
void DeleteObjects()
{
std::vector<T::ObjectType*>::iterator iter;
foreach (iter, lObjectList)
{
delete *iter;
}
lObjectList.clear();
}
protected :
std::vector<T::ObjectType> lObjectList;
};
// Utilisation :
class ModelFactory : public SingletonFactory<ModelFactory>
{
public :
typedef Model ObjectType;
// Si ton objet possède un constructeur avec des arguments,
// tu peux toujours rajouter la fonction Create idoine "à la main" :
Model* Create(std::string sFileName)
{
Model* pModel = new Model(sFileName);
lObjectList.push_back(pModel);
return pModel;
}
// Sinon ben y a rien à faire :)
}; |
Partager