aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/basic/CMakeLists.txt3
-rw-r--r--examples/basic/src/basic_example.cpp24
-rw-r--r--examples/basic/src/bootstrap.cpp26
-rw-r--r--examples/basic/src/enemy.cpp2
-rw-r--r--examples/basic/src/enemy.hpp2
-rw-r--r--examples/basic/src/enemy_creator.cpp19
-rw-r--r--examples/basic/src/enemy_creator.hpp29
-rw-r--r--examples/basic/src/interfaces/enemy.hpp3
-rw-r--r--examples/basic/src/interfaces/enemy_creator.hpp15
9 files changed, 109 insertions, 14 deletions
diff --git a/examples/basic/CMakeLists.txt b/examples/basic/CMakeLists.txt
index afe03ee..48d1f05 100644
--- a/examples/basic/CMakeLists.txt
+++ b/examples/basic/CMakeLists.txt
@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.2.0)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
-set(CMAKE_CXX_EXTENSIONS OFF)
+set(CMAKE_CXX_EXTENSIONS ON)
project(basic-example CXX)
@@ -15,6 +15,7 @@ file(
src/hero.cpp
src/sword.cpp
src/enemy.cpp
+ src/enemy_creator.cpp
)
add_executable(${PROJECT_NAME} ${SOURCES})
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;
+};