#include <list>
#include "Deriveds.hh"
#include "DynObj.hh"
#include "DynTmp.hh"
using namespace NoPtr;
class FactoryFIFO
{
std::list<DynObj<Base>::InValueContainerOpt2> objects;
std::list<DynObj<Base>::InValueContainerOpt2> delivered;
public:
void create(const std::string& type)
{
if (type=="derived1")
{
objects.push_back(DynTmp<Base>(new Derived1));
}
else if (type=="derived2")
{
objects.push_back(DynTmp<Base>(new Derived2));
}
else if (type=="derived3")
{
objects.push_back(DynTmp<Base>(new Derived3));
}
else throw std::logic_error("Unknown type");
}
DynTmp<Base> getObj()
{
DynObj<Base>::InValueContainerOpt2& owner = objects.front();
if (owner.isNull())
throw std::logic_error("No object created");
delivered.splice(delivered.begin(), objects, objects.begin());
return delivered.front().moveToTmp();
}
};
void getObjects(FactoryFIFO& factoryFIFO, int num)
{
std::cout << "Getting FIFO objects" << std::endl;
DynObj<Base> mango;
for (int i=0; i<num; i++)
{
mango = factoryFIFO.getObj();
mango().callVirtual();
}
}
int main()
{
try
{
std::cout << "Using FactoryFIFO again" << std::endl;
FactoryFIFO factoryFIFO;
factoryFIFO.create("derived3");
factoryFIFO.create("derived2");
factoryFIFO.create("derived1");
factoryFIFO.create("derived2");
factoryFIFO.create("derived3");
getObjects(factoryFIFO, 4);
std::cout << "Done FactoryFIFO use" << std::endl;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}