Change EffectParameter string to ConstString, changed to Variant.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-15 19:05:24 +02:00
parent cb942c4e89
commit 317f616be2
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 14 additions and 19 deletions

View File

@ -1,7 +1,8 @@
#ifndef CREATURELIB_EFFECTPARAMETER_HPP
#define CREATURELIB_EFFECTPARAMETER_HPP
#include <Arbutils/ConstString.hpp>
#include <Arbutils/Enum.hpp>
#include <cstring>
#include <variant>
#include "Exceptions/CreatureException.hpp"
namespace CreatureLib::Library {
@ -10,24 +11,18 @@ namespace CreatureLib::Library {
class EffectParameter {
private:
EffectParameterType _type = EffectParameterType::None;
union {
bool _b;
int64_t _i;
const std::string _s;
float _f;
};
std::variant<bool, int64_t, float, Arbutils::CaseInsensitiveConstString> _value;
public:
EffectParameter() : _type(EffectParameterType::None){};
explicit EffectParameter(bool b) : _type(EffectParameterType::Bool), _b(b){};
explicit EffectParameter(int64_t i) : _type(EffectParameterType::Int), _i(i){};
explicit EffectParameter(float f) : _type(EffectParameterType::Float), _f(f){};
explicit EffectParameter(const std::string& s) : _type(EffectParameterType::String), _s(s){};
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;
~EffectParameter() {}
EffectParameterType GetType() const noexcept { return _type; }
bool AsBool() const {
if (_type != EffectParameterType::Bool) {
@ -35,7 +30,7 @@ namespace CreatureLib::Library {
ss << "Cast effect parameter to bool, but was " << EffectParameterTypeHelper::ToString(_type);
throw CreatureException(ss.str());
}
return _b;
return std::get<bool>(_value);
}
int64_t AsInt() const {
if (_type != EffectParameterType::Int) {
@ -43,7 +38,7 @@ namespace CreatureLib::Library {
ss << "Cast effect parameter to int, but was " << EffectParameterTypeHelper::ToString(_type);
throw CreatureException(ss.str());
}
return _i;
return std::get<int64_t>(_value);
}
float AsFloat() const {
if (_type != EffectParameterType::Float) {
@ -51,15 +46,15 @@ namespace CreatureLib::Library {
ss << "Cast effect parameter to float, but was " << EffectParameterTypeHelper::ToString(_type);
throw CreatureException(ss.str());
}
return _f;
return std::get<float>(_value);
}
const std::string& AsString() const {
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 _s;
return std::get<Arbutils::CaseInsensitiveConstString>(_value);
}
};
}

View File

@ -16,7 +16,7 @@ TEST_CASE("Int EffectParameter", "[Library]") {
}
TEST_CASE("String EffectParameter", "[Library]") {
auto p = EffectParameter(std::string("foobar"));
auto p = EffectParameter((Arbutils::CaseInsensitiveConstString) "foobar"_cnc);
REQUIRE(p.AsString() == "foobar");
}