Document StatisticSet, add constraint that its generic type must be an integer type.

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-09-30 18:53:18 +02:00
parent 539a278114
commit 926a3c5dd8
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 40 additions and 3 deletions

View File

@ -49,7 +49,6 @@ elseif (LEVEL_SIZE STREQUAL "32")
elseif (LEVEL_SIZE STREQUAL "64")
add_definitions(-DLEVEL_U64)
message(STATUS "Using level size of 64")
else ()
message(FATAL_ERROR, "Invalid level size was given.")
endif ()
@ -87,7 +86,7 @@ target_link_libraries(CreatureLib PUBLIC ${_LIBRARYLINKS} Threads::Threads)
if (TESTS)
# Create Test executable
file(GLOB_RECURSE TEST_FILES "tests/*.cpp" "tests/*.hpp")
add_executable(CreatureLibTests ${TEST_FILES} extern/doctest.hpp tests/BattleTests/EventHookTests.cpp)
add_executable(CreatureLibTests ${TEST_FILES} extern/doctest.hpp)
target_link_libraries(CreatureLibTests PUBLIC CreatureLib Arbutils)
# Add a definition for the test library

View File

@ -4,7 +4,11 @@
#include "Statistic.hpp"
namespace CreatureLib::Library {
template <class T> class StatisticSet {
template <typename T> concept StatisticSetType = std::is_integral<T>::value;
/// @brief A class to hold all the different stats a creature can have.
/// @tparam T An integer type that defines the kind the different fields in the set can have.
template <StatisticSetType T> class StatisticSet {
protected:
T _health;
T _physicalAttack;
@ -14,19 +18,42 @@ namespace CreatureLib::Library {
T _speed;
public:
/// @brief Initialises the set with its different values.
/// @param health The amount of health given in the set.
/// @param physicalAttack The amount of physical attack given in the set.
/// @param physicalDefense The amount of physical defense given in the set.
/// @param magicalAttack The amount of magical attack given in the set.
/// @param magicalDefense The amount of magical attack given in the set.
/// @param speed The amount of speed given in the set.
StatisticSet(T health, T physicalAttack, T physicalDefense, T magicalAttack, T magicalDefense, T speed) noexcept
: _health(health), _physicalAttack(physicalAttack), _physicalDefense(physicalDefense),
_magicalAttack(magicalAttack), _magicalDefense(magicalDefense), _speed(speed) {}
/// @brief Initialises the set with 0 as every value.
StatisticSet() noexcept
: _health(0), _physicalAttack(0), _physicalDefense(0), _magicalAttack(0), _magicalDefense(0), _speed(0) {}
/// @brief Returns the health in the set.
/// @return The health in the set.
inline T GetHealth() const noexcept { return _health; }
/// @brief Returns the Physical Attack in the set.
/// @return The Physical Attack in the set.
inline T GetPhysicalAttack() const noexcept { return _physicalAttack; }
/// @brief Returns the Physical Defense in the set.
/// @return The Physical Defense in the set.
inline T GetPhysicalDefense() const noexcept { return _physicalDefense; }
/// @brief Returns the Magical Attack in the set.
/// @return The Magical Attack in the set.
inline T GetMagicalAttack() const noexcept { return _magicalAttack; }
/// @brief Returns the Magical Defense in the set.
/// @return The Magical Defense in the set.
inline T GetMagicalDefense() const noexcept { return _magicalDefense; }
/// @brief Returns the Speed in the set.
/// @return The Speed in the set.
inline T GetSpeed() const noexcept { return _speed; }
/// @brief Returns a stat given a Statistic enum.
/// @param stat The desired stat.
/// @return The value of the desired stat.
[[nodiscard]] inline T GetStat(Statistic stat) const {
switch (stat) {
case CreatureLib::Library::Statistic::Health: return _health;
@ -39,6 +66,9 @@ namespace CreatureLib::Library {
}
}
/// @brief Sets a specific stat to a value.
/// @param stat The stat to set.
/// @param value The value to set the stat at.
inline void SetStat(Statistic stat, T value) {
switch (stat) {
case CreatureLib::Library::Statistic::Health: _health = value; break;
@ -51,6 +81,9 @@ namespace CreatureLib::Library {
}
}
/// @brief Increment a stat by a given amount.
/// @param stat The stat to increment.
/// @param amount The value to increment it by.
inline void IncreaseStatBy(Statistic stat, T amount) {
switch (stat) {
case CreatureLib::Library::Statistic::Health: _health += amount; break;
@ -62,6 +95,10 @@ namespace CreatureLib::Library {
default: NOT_REACHABLE;
}
}
/// @brief Decrement a stat by a given amount.
/// @param stat The stat to decrement.
/// @param amount The value to decrement it by.
inline void DecreaseStatBy(Statistic stat, T amount) {
switch (stat) {
case CreatureLib::Library::Statistic::Health: _health -= amount; break;

View File

@ -14,6 +14,7 @@
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>