diff options
author | HampusM <hampus@hampusmat.com> | 2022-04-29 20:51:15 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-04-29 20:51:15 +0200 |
commit | 376ef270b255256b4154eafaea3530ab01e7ccc5 (patch) | |
tree | 823d4ffa69890e89b5d887e9751bffa49078702f /examples | |
parent | 41866b0febe36ca2f7d6c02ffce0ffea3f0ce425 (diff) |
docs: add factory to basic examplev0.1.2
Diffstat (limited to 'examples')
-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>()>; |