Is it possible to create a Factory that generates all derived classes (and class names) at runtime, based on all the classes that are specified in a particular file?
For example, given a specific file where users are constantly adding derived classes:
class Laptop: public Computer {
public:
virtual void Run(){mHibernating = false;}
virtual void Stop(){mHibernating = true;}
private:
bool mHibernating; // Whether or not the machine is hibernating
};
class Desktop: public Computer {
public:
virtual void Run(){mOn = true;}
virtual void Stop(){mOn = false;}
private:
bool mOn; // Whether or not the machine has been turned on
};
// ....[more classes derived from Computer]...
Can a Factory generate a list of possible classes to map to, based on their name?
class ComputerFactory {
public:
static Computer *NewComputer(const std::string &description)
{
if(description == "Laptop")
return new Laptop;
} else if (description == "Desktop") {
return new Desktop;
} else if // ...[more comparisons based on the classes defined at runtime]...
} else return NULL;
}
};
You can write a parser and have the class process the input file, but I doubt that's what you want.
You can also use the crtp pattern to your advantage:
source
You'd then derive classes as
This would automatically register your class with
Factory
given its name (you have to implementname
in non-abstract classes because it's pure).Additionally, you can pass a callback to
registerClass
that knows how to create your derived class.