aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-04-28 22:03:07 +0200
committerHampusM <hampus@hampusmat.com>2022-04-28 22:03:34 +0200
commit9fce9dd2c3cd1c0a75a5cfe74ea0484186247cc3 (patch)
tree37184c6a8a5d94863a23e053631756f738b75bee /examples
parent1f97ab2ef846462d7a599fbd3ac9415da8f0953b (diff)
docs: add basic examplev0.1.0
Diffstat (limited to 'examples')
-rw-r--r--examples/basic/CMakeLists.txt47
-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
13 files changed, 204 insertions, 0 deletions
diff --git a/examples/basic/CMakeLists.txt b/examples/basic/CMakeLists.txt
new file mode 100644
index 0000000..930944e
--- /dev/null
+++ b/examples/basic/CMakeLists.txt
@@ -0,0 +1,47 @@
+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)
+
+project(basic-example CXX)
+
+file(
+ GLOB
+ SOURCES
+ src/basic_example.cpp
+ src/bootstrap.cpp
+ src/hero.cpp
+ src/sword.cpp
+ src/enemy.cpp
+)
+
+add_executable(${PROJECT_NAME} ${SOURCES})
+
+target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
+
+target_compile_options(
+ ${PROJECT_NAME}
+ PRIVATE
+ -Wall -Wextra -Wpedantic -Wshadow
+ -Wold-style-cast -Wcast-align -Wno-unused
+ -Wconversion -Wcast-qual -Wctor-dtor-privacy
+ -Wdisabled-optimization -Wformat=2 -Winit-self
+ -Wmissing-declarations -Wmissing-include-dirs
+ -Woverloaded-virtual -Wredundant-decls
+ -Wsign-conversion -Wsign-promo
+ -Wstrict-overflow=5 -Wswitch-default
+ -Wundef -Werror
+ -pedantic -fsanitize=address -fno-exceptions -stdlib=libc++
+)
+
+target_link_options(${PROJECT_NAME} PRIVATE -fsanitize=address -stdlib=libc++)
+
+target_include_directories(${PROJECT_NAME} PRIVATE src)
+
+target_link_libraries(
+ ${PROJECT_NAME}
+ PRIVATE
+ yacppdic::yacppdic
+)
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;
+};