aboutsummaryrefslogtreecommitdiff
path: root/examples/basic/src
diff options
context:
space:
mode:
Diffstat (limited to 'examples/basic/src')
-rw-r--r--examples/basic/src/basic_example.cpp23
-rw-r--r--examples/basic/src/bootstrap.cpp22
-rw-r--r--examples/basic/src/bootstrap.hpp5
-rw-r--r--examples/basic/src/enemy.cpp11
-rw-r--r--examples/basic/src/enemy.hpp16
-rw-r--r--examples/basic/src/hero.cpp10
-rw-r--r--examples/basic/src/hero.hpp19
-rw-r--r--examples/basic/src/interfaces/enemy.hpp11
-rw-r--r--examples/basic/src/interfaces/hero.hpp11
-rw-r--r--examples/basic/src/interfaces/weapon.hpp9
-rw-r--r--examples/basic/src/sword.cpp6
-rw-r--r--examples/basic/src/sword.hpp14
12 files changed, 157 insertions, 0 deletions
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 <iostream>
+
+int main()
+{
+ auto container = bootstrap();
+
+ auto hero = container.get<IHero>();
+
+ std::cout << "Created a hero\n";
+
+ auto enemy = container.get<IEnemy>();
+
+ 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<IHero>().to<Hero>();
+ container.bind<IWeapon>().to<Sword>();
+ container.bind<IEnemy>().to<Enemy>();
+
+ 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.hpp>
+
+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 <yacppdic/auto_wirable.hpp>
+
+class Enemy : public IEnemy, public yacppdic::AutoWirable<IEnemy, Enemy>
+{
+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 <utility>
+
+Hero::Hero(std::shared_ptr<IWeapon> 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 <yacppdic/auto_wirable.hpp>
+
+#include <memory>
+
+class Hero : public IHero, public yacppdic::AutoWirable<IHero, Hero, IWeapon>
+{
+public:
+ Hero(std::shared_ptr<IWeapon> weapon) noexcept;
+
+ void fight(IEnemy &enemy) noexcept override;
+
+private:
+ std::shared_ptr<IWeapon> _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 <yacppdic/auto_wirable.hpp>
+
+class Sword : public IWeapon, public yacppdic::AutoWirable<IWeapon, Sword>
+{
+public:
+ int get_strength() const noexcept override;
+
+private:
+ int _strength = 30;
+};