61 lines
2.6 KiB
C++
61 lines
2.6 KiB
C++
#ifndef CREATURELIB_EFFECTPARAMETER_HPP
|
|
#define CREATURELIB_EFFECTPARAMETER_HPP
|
|
#include <Arbutils/Enum.hpp>
|
|
#include <Arbutils/Exception.hpp>
|
|
#include <variant>
|
|
#include "../Defines.hpp"
|
|
#include "Exceptions/CreatureException.hpp"
|
|
|
|
namespace CreatureLib::Library {
|
|
ENUM(EffectParameterType, u8, None, Bool, Int, Float, String);
|
|
|
|
class EffectParameter {
|
|
private:
|
|
EffectParameterType _type = EffectParameterType::None;
|
|
std::variant<bool, i64, float, ArbUt::StringView> _value;
|
|
|
|
public:
|
|
inline EffectParameter() : _type(EffectParameterType::None){};
|
|
inline explicit EffectParameter(bool b) : _type(EffectParameterType::Bool), _value(b){};
|
|
inline explicit EffectParameter(i64 i) : _type(EffectParameterType::Int), _value(i){};
|
|
inline explicit EffectParameter(float f) : _type(EffectParameterType::Float), _value(f){};
|
|
inline explicit EffectParameter(const ArbUt::StringView& s) : _type(EffectParameterType::String), _value(s){};
|
|
EffectParameter(const EffectParameter& other) = delete;
|
|
EffectParameter& operator=(const EffectParameter& other) = delete;
|
|
|
|
inline EffectParameterType GetType() const noexcept { return _type; }
|
|
bool AsBool() const {
|
|
if (_type != EffectParameterType::Bool) {
|
|
THROW("Cast effect parameter to bool, but was ", EffectParameterTypeHelper::ToString(_type));
|
|
}
|
|
return std::get<bool>(_value);
|
|
}
|
|
i64 AsInt() const {
|
|
if (_type != EffectParameterType::Int) {
|
|
if (_type == EffectParameterType::Float) {
|
|
return static_cast<i64>(std::get<float>(_value));
|
|
}
|
|
THROW("Cast effect parameter to int, but was ", EffectParameterTypeHelper::ToString(_type));
|
|
}
|
|
return std::get<i64>(_value);
|
|
}
|
|
float AsFloat() const {
|
|
if (_type != EffectParameterType::Float) {
|
|
if (_type == EffectParameterType::Int) {
|
|
return static_cast<float>(std::get<i64>(_value));
|
|
}
|
|
THROW("Cast effect parameter to float, but was ", EffectParameterTypeHelper::ToString(_type));
|
|
}
|
|
return std::get<float>(_value);
|
|
}
|
|
const ArbUt::StringView& AsString() const {
|
|
if (_type != EffectParameterType::String) {
|
|
THROW("Cast effect parameter to string, but was ", EffectParameterTypeHelper::ToString(_type));
|
|
}
|
|
return std::get<ArbUt::StringView>(_value);
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif // CREATURELIB_EFFECTPARAMETER_HPP
|