From 9fce9dd2c3cd1c0a75a5cfe74ea0484186247cc3 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 28 Apr 2022 22:03:07 +0200 Subject: docs: add basic example --- examples/basic/src/basic_example.cpp | 23 +++++++++++++++++++++++ examples/basic/src/bootstrap.cpp | 22 ++++++++++++++++++++++ examples/basic/src/bootstrap.hpp | 5 +++++ examples/basic/src/enemy.cpp | 11 +++++++++++ examples/basic/src/enemy.hpp | 16 ++++++++++++++++ examples/basic/src/hero.cpp | 10 ++++++++++ examples/basic/src/hero.hpp | 19 +++++++++++++++++++ examples/basic/src/interfaces/enemy.hpp | 11 +++++++++++ examples/basic/src/interfaces/hero.hpp | 11 +++++++++++ examples/basic/src/interfaces/weapon.hpp | 9 +++++++++ examples/basic/src/sword.cpp | 6 ++++++ examples/basic/src/sword.hpp | 14 ++++++++++++++ 12 files changed, 157 insertions(+) create mode 100644 examples/basic/src/basic_example.cpp create mode 100644 examples/basic/src/bootstrap.cpp create mode 100644 examples/basic/src/bootstrap.hpp create mode 100644 examples/basic/src/enemy.cpp create mode 100644 examples/basic/src/enemy.hpp create mode 100644 examples/basic/src/hero.cpp create mode 100644 examples/basic/src/hero.hpp create mode 100644 examples/basic/src/interfaces/enemy.hpp create mode 100644 examples/basic/src/interfaces/hero.hpp create mode 100644 examples/basic/src/interfaces/weapon.hpp create mode 100644 examples/basic/src/sword.cpp create mode 100644 examples/basic/src/sword.hpp (limited to 'examples/basic/src') diff --git a/examples/basic/src/basic_example.cpp b/examples/basic/src/basic_example.cpp new file mode 100644 index 0000000..f85ed2d --- /dev/null +++ b/examples/basic/src/basic_example.cpp @@ -0,0 +1,23 @@ +#include "bootstrap.hpp" +#include "interfaces/enemy.hpp" +#include "interfaces/hero.hpp" + +#include + +int main() +{ + auto container = bootstrap(); + + auto hero = container.get(); + + std::cout << "Created a hero\n"; + + auto enemy = container.get(); + + std::cout << "Created a enemy with " << enemy->get_health() << " in health\n"; + + hero->fight(*enemy); + + std::cout << "The hero hit the enemy! The enemy now has " << enemy->get_health() + << " in health\n"; +} diff --git a/examples/basic/src/bootstrap.cpp b/examples/basic/src/bootstrap.cpp new file mode 100644 index 0000000..fe59c5d --- /dev/null +++ b/examples/basic/src/bootstrap.cpp @@ -0,0 +1,22 @@ +#include "bootstrap.hpp" + +// Interfaces +#include "interfaces/enemy.hpp" +#include "interfaces/hero.hpp" +#include "interfaces/weapon.hpp" + +// Concretes +#include "enemy.hpp" +#include "hero.hpp" +#include "sword.hpp" + +yacppdic::Container bootstrap() +{ + auto container = yacppdic::Container(); + + container.bind().to(); + container.bind().to(); + container.bind().to(); + + return container; +} diff --git a/examples/basic/src/bootstrap.hpp b/examples/basic/src/bootstrap.hpp new file mode 100644 index 0000000..104248f --- /dev/null +++ b/examples/basic/src/bootstrap.hpp @@ -0,0 +1,5 @@ +#pragma once + +#include + +yacppdic::Container bootstrap(); diff --git a/examples/basic/src/enemy.cpp b/examples/basic/src/enemy.cpp new file mode 100644 index 0000000..b20a90d --- /dev/null +++ b/examples/basic/src/enemy.cpp @@ -0,0 +1,11 @@ +#include "enemy.hpp" + +int Enemy::get_health() const noexcept +{ + return _health; +} + +void Enemy::do_damage(int damage) noexcept +{ + _health -= damage; +} diff --git a/examples/basic/src/enemy.hpp b/examples/basic/src/enemy.hpp new file mode 100644 index 0000000..ebf00c4 --- /dev/null +++ b/examples/basic/src/enemy.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "interfaces/enemy.hpp" + +#include + +class Enemy : public IEnemy, public yacppdic::AutoWirable +{ +public: + int get_health() const noexcept override; + + void do_damage(int damage) noexcept override; + +private: + int _health = 120; +}; diff --git a/examples/basic/src/hero.cpp b/examples/basic/src/hero.cpp new file mode 100644 index 0000000..7348982 --- /dev/null +++ b/examples/basic/src/hero.cpp @@ -0,0 +1,10 @@ +#include "hero.hpp" + +#include + +Hero::Hero(std::shared_ptr weapon) noexcept : _weapon(std::move(weapon)) {} + +void Hero::fight(IEnemy &enemy) noexcept +{ + enemy.do_damage(_weapon->get_strength()); +} diff --git a/examples/basic/src/hero.hpp b/examples/basic/src/hero.hpp new file mode 100644 index 0000000..e4959d2 --- /dev/null +++ b/examples/basic/src/hero.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "interfaces/hero.hpp" +#include "interfaces/weapon.hpp" + +#include + +#include + +class Hero : public IHero, public yacppdic::AutoWirable +{ +public: + Hero(std::shared_ptr weapon) noexcept; + + void fight(IEnemy &enemy) noexcept override; + +private: + std::shared_ptr _weapon; +}; diff --git a/examples/basic/src/interfaces/enemy.hpp b/examples/basic/src/interfaces/enemy.hpp new file mode 100644 index 0000000..45e004f --- /dev/null +++ b/examples/basic/src/interfaces/enemy.hpp @@ -0,0 +1,11 @@ +#pragma once + +class IEnemy +{ +public: + virtual ~IEnemy() noexcept = default; + + virtual int get_health() const noexcept = 0; + + virtual void do_damage(int damage) noexcept = 0; +}; diff --git a/examples/basic/src/interfaces/hero.hpp b/examples/basic/src/interfaces/hero.hpp new file mode 100644 index 0000000..25e18fc --- /dev/null +++ b/examples/basic/src/interfaces/hero.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include "interfaces/enemy.hpp" + +class IHero +{ +public: + virtual ~IHero() noexcept = default; + + virtual void fight(IEnemy &enemy) noexcept = 0; +}; diff --git a/examples/basic/src/interfaces/weapon.hpp b/examples/basic/src/interfaces/weapon.hpp new file mode 100644 index 0000000..7404115 --- /dev/null +++ b/examples/basic/src/interfaces/weapon.hpp @@ -0,0 +1,9 @@ +#pragma once + +class IWeapon +{ +public: + virtual ~IWeapon() noexcept = default; + + virtual int get_strength() const noexcept = 0; +}; diff --git a/examples/basic/src/sword.cpp b/examples/basic/src/sword.cpp new file mode 100644 index 0000000..5d5ec40 --- /dev/null +++ b/examples/basic/src/sword.cpp @@ -0,0 +1,6 @@ +#include "sword.hpp" + +int Sword::get_strength() const noexcept +{ + return _strength; +} diff --git a/examples/basic/src/sword.hpp b/examples/basic/src/sword.hpp new file mode 100644 index 0000000..25695b7 --- /dev/null +++ b/examples/basic/src/sword.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include "interfaces/weapon.hpp" + +#include + +class Sword : public IWeapon, public yacppdic::AutoWirable +{ +public: + int get_strength() const noexcept override; + +private: + int _strength = 30; +}; -- cgit v1.2.3-18-g5258