Example of how to use the factory to generate objects from configuration files

This is an example of how to use the factory to generate objects from configuration files

The file used by the example is the following one

    [MASTEROBJECT]
    ### No type here, to use factory you have to explicitly choose it
    param1 = UNO
    param3 = DUE
    [MASTEROBJECT/CHILD]
    type = childType1 ## The type for this object
    childParam = My value
    [MASTEROBJECT/LISTOFCHILD:1]
    type = listType1
    myParam = 10
    [MASTEROBJECT/LISTOFCHILD:2]
    type = listType1
    myParam = 13
    [MASTEROBJECT/LISTOFCHILD:3]
    type = listType2
    myParam = 17
    anotherParam = uh?
   

Here's the code for the example

#include "configuration/configurationparameters.h"
#include "configuration/inifilesupport.h"
#include "factory/parametersettable.h"
#include "factory/factory.h"
#include <iostream>
#include <QDebug>
// A class to be instantiated by the factory.
class ChildType1 : public ParameterSettable
{
public:
ChildType1()
{
}
virtual ~ChildType1()
{
}
virtual void configure(const ConfigurationParameters& params, QString prefix)
{
// Printing my parameters
std::cout << "Created object of type ChildType1" << std::endl;
std::cout << "childParam = " << params.getValue(prefix + "childParam").toAscii().data() << std::endl;
std::cout << std::endl;
}
};
// A base class for other classes to be instantiated by the factory
class ListType : public ParameterSettable
{
public:
ListType()
{
}
virtual ~ListType()
{
}
};
// A class to be instantiated by the factory.
class ListType1 : public ListType
{
public:
ListType1()
{
}
virtual ~ListType1()
{
}
virtual void configure(const ConfigurationParameters& params, QString prefix)
{
// Printing my parameters
std::cout << "Created object of type ListType1" << std::endl;
std::cout << "myParam = " << params.getValue(prefix + "myParam").toAscii().data() << std::endl;
std::cout << std::endl;
}
};
// A class to be instantiated by the factory.
class ListType2 : public ListType
{
public:
ListType2()
{
}
virtual ~ListType2()
{
}
virtual void configure(const ConfigurationParameters& params, QString prefix)
{
// Printing my parameters
std::cout << "Created object of type ListType2" << std::endl;
std::cout << "myParam = " << params.getValue(prefix + "myParam").toAscii().data() << std::endl;
std::cout << "anotherParam = " << params.getValue(prefix + "anotherParam").toAscii().data() << std::endl;
std::cout << std::endl;
}
};
// A class to be instantiated by the factory. This also creates other objects
class MasterClass : public ParameterSettable
{
public:
MasterClass()
{
}
virtual ~MasterClass()
{
// Deleting sub-objects
delete m_child;
for (QList<ListType *>::iterator it = m_childrenList.begin(); it != m_childrenList.end(); it++) {
delete *it;
}
}
virtual void configure(const ConfigurationParameters& params, QString prefix)
{
// Printing my parameters
std::cout << "Created object of type MasterClass" << std::endl;
std::cout << "param1 = " << params.getValue(prefix + "param1").toAscii().data() << std::endl;
std::cout << "param3 = " << params.getValue(prefix + "param3").toAscii().data() << std::endl;
std::cout << std::endl;
// Now creating subobjects
m_child = Factory::getInstance().createFromParameter<ChildType1>(params, prefix + QString("CHILD"));
// Creating the list of objects
m_childrenList = Factory::getInstance().createListFromParameter<ListType>(params, prefix, QString("LISTOFCHILD"));
}
private:
ChildType1* m_child;
QList<ListType *> m_childrenList;
};
int main(int argc, char* argv[])
{
// We expect the name of the file to read on the command line
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " <infile> <0|1>" << std::endl;
std::cerr << "where 0 means case insensitive mode, 1 case sensitive mode. File format is guessed from file extension" << std::endl;
return 1;
}
// Before continuing, registering classes to the factory
Factory::getInstance().registerClass<MasterClass>("MasterClass");
Factory::getInstance().registerClass<ChildType1>("ChildType1");
Factory::getInstance().registerClass<ListType1>("ListType1");
Factory::getInstance().registerClass<ListType2>("ListType2");
// Checking whether we have to work in case sensitive or insensitive way
const bool caseSensitive = (argv[4][0] == '0') ? false : true;
// Creating a configuration parameters object
ConfigurationParameters confs(caseSensitive);
// Loading parameters. The kind of file is automatically guessed from extension (you can however explicitly set it)
if (!confs.loadParameters(argv[1])) {
std::cerr << "Loading parameters from file " << argv[1] << " failed" << std::endl;
}
// Creating the main object. This will create child objects
MasterClass *obj = Factory::getInstance().create<MasterClass>("MasterClass", confs, "MASTEROBJECT");
// Destroying everything
delete obj;
return 0;
}