#include "Deriveds.hh"
#include "OwnedPtr.hh"
using namespace MangoPtr;
class FactoryOne
{
Owned<Base*> owner;
public:
void create(const std::string& type)
{
if (type=="derived1")
owner.takeOwnership(new Derived1);
else if (type=="derived2")
owner.takeOwnership(new Derived2);
else throw std::logic_error("Unknown type");
}
void getObj( Owned<Base*>& newOwned )
{
std::cout << "FactoryOne: giving object" << std::endl;
if (owner.isNull())
throw std::logic_error("No object created");
newOwned.takeOwnership( owner.giveOwnership() );
}
};
void make(FactoryOne& factory, const std::string& type)
{
factory.create(type);
Owned<Base*> mango;
factory.getObj(mango);
assert(mango.isNotNull());
mango()->callVirtual();
}
int main()
{
try
{
std::cout << "Using FactoryOne" << std::endl;
FactoryOne factory;
make(factory, "derived1");
make(factory, "derived2");
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}