Files
2026-02-27 21:43:04 -05:00

43 lines
903 B
C++

#include <iostream>
#include "typefactory.h"
#include <string>
using namespace std;
int main() {
cout << "Type factory simple example" << endl;
struct Base {
virtual ~Base() = default;
virtual int get() = 0;
};
struct Derived1 : Base {
explicit Derived1 (int start) : m_start (start) {}
int get() override {
return m_start + 1;
}
int m_start = 0;
};
struct Derived2 : Base {
explicit Derived2 (int start) : m_start (start) {}
int get() override {
return m_start + 2;
}
int m_start = 0;
};
TypeFactory<std::string, Base, int> factory;
factory.registerType<Derived1> ("one");
factory.registerType<Derived2> ("two");
auto a = factory.create ("one", 10);
auto b = factory.creator ("two") (10); // advanced API: get creator function
std::cout << a->get() << std::endl;
std::cout << b->get() << std::endl;
return 0;
}