// // polymorphic_factory.hpp // resource_factory // // Created by Sam Jaffe on 8/14/16. // #pragma once #include #include /* * An instance prototype factory factory. * The purpose of this object is a prefab for situations where you do not * know, necessarily, at compile time all posible types inheriting from the * base class. An example of this is in a game, you may have a Mob class. * Mob may be instantiated as an Orc, Goblin, Slime, or Knight, but you don't * know the exhaustive list, and the overhead for implementing an override * for every single type is wasteful. With this object, you can initialize * implementations from a string ID, and some parameters required. * For example: * * class Monster { ... }; * class MonsterPrototype : public Monster { * public: Monster operator()(...) const { ... } * }; * using MonsterGenerator = prototype_factory; * MonsterGenerator::bind("Orc", MonsterPrototype("resources/orc.proto")); * * ----- * * This can also be used as a generator for polymorphic types, optionally * through a plugin system. This would allow a library consumer to define * extensions to some polymorphic class, such as a Widget, and use built in * features of the library to instantiate it. For example: * * class Widget { ... }; * class Button : public Widget { ... }; * using WidgetGenerator = prototype_factory, ...>; * WidgetGenerator::bind("Button", * [](...) { return std::make_unique