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 --- CMakeLists.txt | 1 + examples/basic/CMakeLists.txt | 47 ++++++++++++++++++++++++++++++++ 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 ++++++++++ 14 files changed, 205 insertions(+) create mode 100644 examples/basic/CMakeLists.txt 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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 25ead17..4aa6d34 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,3 +46,4 @@ target_include_directories(${PROJECT_NAME} add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) add_subdirectory(test) +add_subdirectory(examples/basic) 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 + +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