68 lines
2.9 KiB
C++
68 lines
2.9 KiB
C++
#ifndef CREATURELIB_EFFECTPARAMETER_HPP
|
|
#define CREATURELIB_EFFECTPARAMETER_HPP
|
|
#include <Arbutils/Enum.hpp>
|
|
#include <Arbutils/StringView.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, ArbUt::StringView> _value;
|
|
|
|
public:
|
|
inline EffectParameter() : _type(EffectParameterType::None){};
|
|
inline explicit EffectParameter(bool b) : _type(EffectParameterType::Bool), _value(b){};
|
|
inline explicit EffectParameter(int64_t 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) {
|
|
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 ArbUt::StringView& 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<ArbUt::StringView>(_value);
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif // CREATURELIB_EFFECTPARAMETER_HPP
|