From 3957dfb9361e864f6362e59655c885f52b39371f Mon Sep 17 00:00:00 2001
From: HampusM <hampus@hampusmat.com>
Date: Fri, 6 May 2022 18:23:42 +0200
Subject: docs: add tagging to basic example

---
 examples/basic/src/basic_example.cpp            | 24 ++++++++++++++------
 examples/basic/src/bootstrap.cpp                | 26 +++++++++++++++++-----
 examples/basic/src/enemy.cpp                    |  2 ++
 examples/basic/src/enemy.hpp                    |  2 ++
 examples/basic/src/enemy_creator.cpp            | 19 ++++++++++++++++
 examples/basic/src/enemy_creator.hpp            | 29 +++++++++++++++++++++++++
 examples/basic/src/interfaces/enemy.hpp         |  3 +++
 examples/basic/src/interfaces/enemy_creator.hpp | 15 +++++++++++++
 8 files changed, 107 insertions(+), 13 deletions(-)
 create mode 100644 examples/basic/src/enemy_creator.cpp
 create mode 100644 examples/basic/src/enemy_creator.hpp
 create mode 100644 examples/basic/src/interfaces/enemy_creator.hpp

(limited to 'examples/basic/src')

diff --git a/examples/basic/src/basic_example.cpp b/examples/basic/src/basic_example.cpp
index 6f3e15c..e91235f 100644
--- a/examples/basic/src/basic_example.cpp
+++ b/examples/basic/src/basic_example.cpp
@@ -1,5 +1,5 @@
 #include "bootstrap.hpp"
-#include "interfaces/enemy.hpp"
+#include "interfaces/enemy_creator.hpp"
 #include "interfaces/hero.hpp"
 
 #include <iostream>
@@ -12,14 +12,24 @@ int main()
 
 	std::cout << "Created a hero\n";
 
-	auto enemy_factory = container.get<IEnemyFactory>();
+	auto enemy_creator = container.get<IEnemyCreator>();
 
-	auto enemy = enemy_factory();
+	auto small_enemy = enemy_creator->create_small_enemy();
 
-	std::cout << "Created a enemy with " << enemy->get_health() << " in health\n";
+	std::cout << "Created a small enemy with " << small_enemy->get_health()
+			  << " in health\n";
 
-	hero->fight(*enemy);
+	auto big_enemy = enemy_creator->create_big_enemy();
 
-	std::cout << "The hero hit the enemy! The enemy now has " << enemy->get_health()
-			  << " in health\n";
+	std::cout << "Created a big enemy with " << big_enemy->get_health() << " in health\n";
+
+	hero->fight(*small_enemy);
+
+	std::cout << "The hero hit the small enemy! The small enemy now has "
+			  << small_enemy->get_health() << " in health\n";
+
+	hero->fight(*big_enemy);
+
+	std::cout << "The hero hit the big enemy! The big enemy now has "
+			  << big_enemy->get_health() << " in health\n";
 }
diff --git a/examples/basic/src/bootstrap.cpp b/examples/basic/src/bootstrap.cpp
index 38edcc3..7a78aec 100644
--- a/examples/basic/src/bootstrap.cpp
+++ b/examples/basic/src/bootstrap.cpp
@@ -2,11 +2,13 @@
 
 // Interfaces
 #include "interfaces/enemy.hpp"
+#include "interfaces/enemy_creator.hpp"
 #include "interfaces/hero.hpp"
 #include "interfaces/weapon.hpp"
 
 // Concretes
 #include "enemy.hpp"
+#include "enemy_creator.hpp"
 #include "hero.hpp"
 #include "sword.hpp"
 
@@ -16,13 +18,25 @@ yacppdic::Container bootstrap()
 
 	container.bind<IHero>().to<Hero>();
 	container.bind<IWeapon>().to<Sword>();
+	container.bind<IEnemyCreator>().to<EnemyCreator>();
 
-	container.bind<IEnemyFactory>().to_factory(
-		[]()
-		{
-			return std::make_unique<Enemy>();
-		}
-	);
+	container.bind<IEnemyFactory>()
+		.to_factory(
+			[]()
+			{
+				return std::make_unique<Enemy>(65);
+			}
+		)
+		.when_tagged(SMALL_ENEMY_TAG);
+
+	container.bind<IEnemyFactory>()
+		.to_factory(
+			[]()
+			{
+				return std::make_unique<Enemy>(130);
+			}
+		)
+		.when_tagged(BIG_ENEMY_TAG);
 
 	return container;
 }
diff --git a/examples/basic/src/enemy.cpp b/examples/basic/src/enemy.cpp
index b20a90d..3923794 100644
--- a/examples/basic/src/enemy.cpp
+++ b/examples/basic/src/enemy.cpp
@@ -1,5 +1,7 @@
 #include "enemy.hpp"
 
+Enemy::Enemy(int health) noexcept : _health(health) {}
+
 int Enemy::get_health() const noexcept
 {
 	return _health;
diff --git a/examples/basic/src/enemy.hpp b/examples/basic/src/enemy.hpp
index ebf00c4..b345f7f 100644
--- a/examples/basic/src/enemy.hpp
+++ b/examples/basic/src/enemy.hpp
@@ -7,6 +7,8 @@
 class Enemy : public IEnemy, public yacppdic::AutoWirable<IEnemy, Enemy>
 {
 public:
+	Enemy(int health) noexcept;
+
 	int get_health() const noexcept override;
 
 	void do_damage(int damage) noexcept override;
diff --git a/examples/basic/src/enemy_creator.cpp b/examples/basic/src/enemy_creator.cpp
new file mode 100644
index 0000000..820ce1b
--- /dev/null
+++ b/examples/basic/src/enemy_creator.cpp
@@ -0,0 +1,19 @@
+#include "enemy_creator.hpp"
+
+EnemyCreator::EnemyCreator(
+	IEnemyFactory small_enemy_factory,
+	IEnemyFactory big_enemy_factory
+) noexcept
+	: _small_enemy_factory(small_enemy_factory), _big_enemy_factory(big_enemy_factory)
+{
+}
+
+std::unique_ptr<IEnemy> EnemyCreator::create_small_enemy() noexcept
+{
+	return _small_enemy_factory();
+}
+
+std::unique_ptr<IEnemy> EnemyCreator::create_big_enemy() noexcept
+{
+	return _big_enemy_factory();
+}
diff --git a/examples/basic/src/enemy_creator.hpp b/examples/basic/src/enemy_creator.hpp
new file mode 100644
index 0000000..a220008
--- /dev/null
+++ b/examples/basic/src/enemy_creator.hpp
@@ -0,0 +1,29 @@
+#pragma once
+
+#include "interfaces/enemy.hpp"
+#include "interfaces/enemy_creator.hpp"
+
+#include <yacppdic/auto_wirable.hpp>
+#include <yacppdic/tagged.hpp>
+
+class EnemyCreator : public IEnemyCreator,
+					 public yacppdic::AutoWirable<
+						 IEnemyCreator,
+						 EnemyCreator,
+						 yacppdic::Tagged<IEnemyFactory, SMALL_ENEMY_TAG>,
+						 yacppdic::Tagged<IEnemyFactory, BIG_ENEMY_TAG>>
+{
+public:
+	EnemyCreator(
+		IEnemyFactory small_enemy_factory,
+		IEnemyFactory big_enemy_factory
+	) noexcept;
+
+	std::unique_ptr<IEnemy> create_small_enemy() noexcept override;
+
+	std::unique_ptr<IEnemy> create_big_enemy() noexcept override;
+
+private:
+	IEnemyFactory _small_enemy_factory;
+	IEnemyFactory _big_enemy_factory;
+};
diff --git a/examples/basic/src/interfaces/enemy.hpp b/examples/basic/src/interfaces/enemy.hpp
index 40b6818..5f49ca6 100644
--- a/examples/basic/src/interfaces/enemy.hpp
+++ b/examples/basic/src/interfaces/enemy.hpp
@@ -4,6 +4,9 @@
 
 #include <memory>
 
+constexpr char SMALL_ENEMY_TAG[] = "small";
+constexpr char BIG_ENEMY_TAG[] = "big";
+
 class IEnemy
 {
 public:
diff --git a/examples/basic/src/interfaces/enemy_creator.hpp b/examples/basic/src/interfaces/enemy_creator.hpp
new file mode 100644
index 0000000..820b636
--- /dev/null
+++ b/examples/basic/src/interfaces/enemy_creator.hpp
@@ -0,0 +1,15 @@
+#pragma once
+
+#include "interfaces/enemy.hpp"
+
+#include <memory>
+
+class IEnemyCreator
+{
+public:
+	virtual ~IEnemyCreator() = default;
+
+	virtual std::unique_ptr<IEnemy> create_small_enemy() noexcept = 0;
+
+	virtual std::unique_ptr<IEnemy> create_big_enemy() noexcept = 0;
+};
-- 
cgit v1.2.3-18-g5258