From 376ef270b255256b4154eafaea3530ab01e7ccc5 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 29 Apr 2022 20:51:15 +0200 Subject: docs: add factory to basic example --- examples/basic/src/basic_example.cpp | 4 +++- examples/basic/src/bootstrap.cpp | 8 +++++++- examples/basic/src/interfaces/enemy.hpp | 6 ++++++ 3 files changed, 16 insertions(+), 2 deletions(-) (limited to 'examples') 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(); + auto enemy_factory = container.get(); + + 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().to(); container.bind().to(); - container.bind().to(); + + container.bind().to_factory( + []() + { + return std::make_unique(); + } + ); 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 + +#include + class IEnemy { public: @@ -9,3 +13,5 @@ public: virtual void do_damage(int damage) noexcept = 0; }; + +using IEnemyFactory = yacppdic::Factory()>; -- cgit v1.2.3-18-g5258