Skip to content

A generic factory that instantiates types based on a unique identifier

License

Notifications You must be signed in to change notification settings

sro5h/factory.hpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

factory.hpp

A factory that instantiates types based on a unique identifier. Arguments of the concrete type's constructor can be forwarded upon registering the type. The factory class is provided as a single header file with no other dependencies besides the c++ standard library.

Example

A simple example, can also be found in example.cpp.

#include "Factory.hpp"

#include <iostream>

struct Scene {
        virtual void printName() = 0;
};

struct SceneA : public Scene {
        explicit SceneA(int a) {
                std::cout << "A is " << a << std::endl;
        }

        void printName() override {
                std::cout << "I'm scene A" << std::endl;
        }
};

struct SceneB : public Scene {
        void printName() override {
                std::cout << "I'm scene B" << std::endl;
        }
};

enum class Scenes {
        A,
        B,
};

int main() {
        sro5h::Factory<Scene, Scenes> factory;
        factory.registerType<SceneA>(Scenes::A, 3);
        factory.registerType<SceneB>(Scenes::B);

        std::unique_ptr<Scene> sceneA = factory.create(Scenes::A);
        std::unique_ptr<Scene> sceneB = factory.create(Scenes::B);

        sceneA->printName();
        sceneB->printName();

        return 0;
}

About

A generic factory that instantiates types based on a unique identifier

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages