From 3957dfb9361e864f6362e59655c885f52b39371f Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 6 May 2022 18:23:42 +0200 Subject: docs: add tagging to basic example --- examples/basic/CMakeLists.txt | 3 ++- 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 +++++++++++++ 9 files changed, 109 insertions(+), 14 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') 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 @@ -12,14 +12,24 @@ int main() std::cout << "Created a hero\n"; - auto enemy_factory = container.get(); + auto enemy_creator = container.get(); - 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().to(); container.bind().to(); + container.bind().to(); - container.bind().to_factory( - []() - { - return std::make_unique(); - } - ); + container.bind() + .to_factory( + []() + { + return std::make_unique(65); + } + ) + .when_tagged(SMALL_ENEMY_TAG); + + container.bind() + .to_factory( + []() + { + return std::make_unique(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 { 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 EnemyCreator::create_small_enemy() noexcept +{ + return _small_enemy_factory(); +} + +std::unique_ptr 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 +#include + +class EnemyCreator : public IEnemyCreator, + public yacppdic::AutoWirable< + IEnemyCreator, + EnemyCreator, + yacppdic::Tagged, + yacppdic::Tagged> +{ +public: + EnemyCreator( + IEnemyFactory small_enemy_factory, + IEnemyFactory big_enemy_factory + ) noexcept; + + std::unique_ptr create_small_enemy() noexcept override; + + std::unique_ptr 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 +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 + +class IEnemyCreator +{ +public: + virtual ~IEnemyCreator() = default; + + virtual std::unique_ptr create_small_enemy() noexcept = 0; + + virtual std::unique_ptr create_big_enemy() noexcept = 0; +}; -- cgit v1.2.3-18-g5258