Files
CreatureLib/src/Library/EffectParameter.hpp
Deukhoofd 119e71e86a
All checks were successful
continuous-integration/drone/push Build is passing
Allow AsInt from Float EffectParameter, and AsFloat from Int EffectParameter.
2020-05-03 21:08:27 +02:00

69 lines
3.0 KiB
C++

#ifndef CREATURELIB_EFFECTPARAMETER_HPP
#define CREATURELIB_EFFECTPARAMETER_HPP
#include <Arbutils/ConstString.hpp>
#include <Arbutils/Enum.hpp>
#include <variant>
#include "Exceptions/CreatureException.hpp"
namespace CreatureLib::Library {
ENUM(EffectParameterType, uint8_t, None, Bool, Int, Float, String);
class EffectParameter {
private:
EffectParameterType _type = EffectParameterType::None;
std::variant<bool, int64_t, float, Arbutils::CaseInsensitiveConstString> _value;
public:
EffectParameter() : _type(EffectParameterType::None){};
explicit EffectParameter(bool b) : _type(EffectParameterType::Bool), _value(b){};
explicit EffectParameter(int64_t i) : _type(EffectParameterType::Int), _value(i){};
explicit EffectParameter(float f) : _type(EffectParameterType::Float), _value(f){};
explicit EffectParameter(const Arbutils::CaseInsensitiveConstString& s)
: _type(EffectParameterType::String), _value(s){};
EffectParameter(const EffectParameter& other) = delete;
EffectParameter& operator=(const EffectParameter& other) = delete;
EffectParameterType GetType() const noexcept { return _type; }
bool AsBool() const {
if (_type != EffectParameterType::Bool) {
std::stringstream ss;
ss << "Cast effect parameter to bool, but was " << EffectParameterTypeHelper::ToString(_type);
throw CreatureException(ss.str());
}
return std::get<bool>(_value);
}
int64_t AsInt() const {
if (_type != EffectParameterType::Int) {
if (_type == EffectParameterType::Float) {
return static_cast<int64_t>(std::get<float>(_value));
}
std::stringstream ss;
ss << "Cast effect parameter to int, but was " << EffectParameterTypeHelper::ToString(_type);
throw CreatureException(ss.str());
}
return std::get<int64_t>(_value);
}
float AsFloat() const {
if (_type != EffectParameterType::Float) {
if (_type == EffectParameterType::Int) {
return static_cast<float>(std::get<int64_t>(_value));
}
std::stringstream ss;
ss << "Cast effect parameter to float, but was " << EffectParameterTypeHelper::ToString(_type);
throw CreatureException(ss.str());
}
return std::get<float>(_value);
}
const Arbutils::CaseInsensitiveConstString& AsString() const {
if (_type != EffectParameterType::String) {
std::stringstream ss;
ss << "Cast effect parameter to string, but was " << EffectParameterTypeHelper::ToString(_type);
throw CreatureException(ss.str());
}
return std::get<Arbutils::CaseInsensitiveConstString>(_value);
}
};
}
#endif // CREATURELIB_EFFECTPARAMETER_HPP