I implemented a factory class based on the following article available here.
I have one problem, however, and I think it’s related to compiler optimisations.
I have a hierarchy of classes where class Node (in Node.h/Node.cpp) is the base class and, for example, BuildingLot (in BuildingLot.h/BuildingLot.cpp) is a subclass.
In both of the source files, declared as static I have a Registrar class.
Node.cpp:
static Registrar<Node> _registrar(“Node”);
BuildingLot.cpp:
static Registrar<BuildingLot> _registrar(“BuildingLot”);
If I try to use the Factory, it works perfectly for Node class. I, however, have to first create an instance of BuildingLot for it to be registered in the factory.
In a unit test I have:
NodePtr node = Factory::Instance()->Create(“Node”);
NodePtr bl = Factory::Instance()->Create(“BuildingLot”);
bl is always nullptr. The Registrar constructor is never executed and, therefore, the Factory class never knows about the BuildingLot class. If I do this:
BuildingLot b;
NodePtr node = Factory::Instance()->Create(“Node”);
NodePtr bl = Factory::Instance()->Create(“BuildingLot”);
Then all works. The Registrar is called, and the Factory creates a BuildingLot.
I think, since the BuildingLot isn’t used in the first example, the compiler optimised and didn’t even compile the BuildingLot.cpp.
Is this possible? How can I solve this?
Edit: The Registrar class is, in fact, a template class. I just forgot to insert the template parameters.
Here's my code as simple as I could make it. I hope I didn't leave anything out. If I uncomment the first line in main(), then it all works.
NodeFactory.h:
class NodeFactory{
private:
/// Map of factory functions
std::map<std::string, std::function<std::shared_ptr<Node>(void)>> mFactoryFunctions;
public:
/// Get Singleton
static NodeFactory* Instance();
/// Register Function.
void Register(const std::string &name, std::function<std::shared_ptr<Node>(void)> factoryFunction);
/// Factory Function.
std::shared_ptr<Node> Create(const std::string &name);
};
NodeFactory.cpp:
/**
* Get Singleton
*/
NodeFactory* NodeFactory::Instance(){
static NodeFactory factory;
return &factory;
}
void NodeFactory::Register(const std::string &name, std::function<std::shared_ptr<Node>(void)> factoryFunction){
mFactoryFunctions[name] = factoryFunction;
}
std::shared_ptr<Node> NodeFactory::Create(const std::string &name){
if(mFactoryFunctions.find(name) == mFactoryFunctions.end())
return nullptr;
std::shared_ptr<Node> n = mFactoryFunctions[name]();
return n;
}
Registrar.h:
#define REGISTER_NODE_TYPE(NODE_TYPE) static NodeRegistrar<NODE_TYPE> _registrar(#NODE_TYPE);
template<class T>
class NodeRegistrar{
private:
public:
NodeRegistrar(const std::string &name){
NodeFactory::Instance()->Register(name,
[](void) -> std::shared_ptr<T> { return std::make_shared<T>(); }
);
}
};
Node.h:
class Node{
private:
/// The node ID.
NodeID mID;
public:
/* ****************************
* Construction & Destruction *
* ***************************/
Node();
Node(const NodeID &nodeID);
virtual ~Node();
};
Node.cpp:
REGISTER_NODE_TYPE(Node);
Node::Node(){
mID = -1;
}
Node::Node(const NodeID &nodeID){
mID = nodeID;
}
Node::~Node(){
}
BuildingLot.h:
class BuildingLot : public Node{
public:
BuildingLot();
BuildingLot(const NodeID &nodeID);
virtual ~BuildingLot();
};
BuildingLot.cpp:
REGISTER_NODE_TYPE(BuildingLot);
BuildingLot::BuildingLot(){
}
BuildingLot::BuildingLot(const NodeID &nodeID):Node(nodeID){
}
BuildingLot::~BuildingLot(){
}
main.cpp:
int main(int argc, const char * argv[]){
// BuildingLot bl; // if I uncomment this, then it works
std::shared_ptr<Node> node = NodeFactory::Instance()->Create("Node");
std::shared_ptr<Node> buildingLot = NodeFactory::Instance()->Create("BuildingLot");
if(node == nullptr)
std::cout << "node is nullptr" << std::endl;
if(buildingLot == nullptr)
std::cout << "buildingLot is nullptr" << std::endl;
return 0;
}