#include <list>
#include "Deriveds.hh"
#include "DynObj.hh"
#include "DynTmp.hh"
using namespace NoPtr;
class FactoryFIFO
{
typedef std::list<DynObj<Base>::InValueContainerOpt1> List;
List objects;
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");
}
void getObj( DynObj<Base>& newOwner )
{
std::cout << "FactoryFIFO: giving object from list" << std::endl;
List::value_type& owner = objects.front();
if ( owner.isNull())
throw std::logic_error("No object created yet");
newOwner.acquire( owner );
assert(objects.front().isNull());
objects.erase(objects.begin());
}
};
void getObjects(FactoryFIFO& factoryFIFO, int num)
{
std::cout << "Getting FIFO objects" << std::endl;
DynObj<Base> dynObj;
for (int i=0; i<num; i++)
{
factoryFIFO.getObj( dynObj );
dynObj().callVirtual();
}
}
int main()
{
try
{
std::cout << "Using FactoryFIFO" << std::endl;
FactoryFIFO factoryFIFO;
factoryFIFO.create("derived1");
factoryFIFO.create("derived2");
factoryFIFO.create("derived3");
factoryFIFO.create("derived2");
factoryFIFO.create("derived1");
getObjects(factoryFIFO, 4);
std::cout << "Done FactoryFIFO use" << std::endl;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}