diff options
-rw-r--r-- | examples/basic/src/basic_example.cpp | 4 | ||||
-rw-r--r-- | examples/basic/src/bootstrap.cpp | 8 | ||||
-rw-r--r-- | examples/basic/src/interfaces/enemy.hpp | 6 |
3 files changed, 16 insertions, 2 deletions
diff --git a/examples/basic/src/basic_example.cpp b/examples/basic/src/basic_example.cpp index f85ed2d..6f3e15c 100644 --- a/examples/basic/src/basic_example.cpp +++ b/examples/basic/src/basic_example.cpp @@ -12,7 +12,9 @@ int main() std::cout << "Created a hero\n"; - auto enemy = container.get<IEnemy>(); + auto enemy_factory = container.get<IEnemyFactory>(); + + auto enemy = enemy_factory(); std::cout << "Created a enemy with " << enemy->get_health() << " in health\n"; diff --git a/examples/basic/src/bootstrap.cpp b/examples/basic/src/bootstrap.cpp index fe59c5d..38edcc3 100644 --- a/examples/basic/src/bootstrap.cpp +++ b/examples/basic/src/bootstrap.cpp @@ -16,7 +16,13 @@ yacppdic::Container bootstrap() container.bind<IHero>().to<Hero>(); container.bind<IWeapon>().to<Sword>(); - container.bind<IEnemy>().to<Enemy>(); + + container.bind<IEnemyFactory>().to_factory( + []() + { + return std::make_unique<Enemy>(); + } + ); return container; } diff --git a/examples/basic/src/interfaces/enemy.hpp b/examples/basic/src/interfaces/enemy.hpp index 45e004f..40b6818 100644 --- a/examples/basic/src/interfaces/enemy.hpp +++ b/examples/basic/src/interfaces/enemy.hpp @@ -1,5 +1,9 @@ #pragma once +#include <yacppdic/factory.hpp> + +#include <memory> + class IEnemy { public: @@ -9,3 +13,5 @@ public: virtual void do_damage(int damage) noexcept = 0; }; + +using IEnemyFactory = yacppdic::Factory<std::unique_ptr<IEnemy>()>; |