Rework of EffectParameter storage, added float value.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-10 21:05:44 +02:00
parent a6ab69e043
commit beb50a60b0
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 31 additions and 19 deletions

View File

@ -5,29 +5,28 @@
#include "Exceptions/CreatureException.hpp" #include "Exceptions/CreatureException.hpp"
namespace CreatureLib::Library { namespace CreatureLib::Library {
ENUM(EffectParameterType, uint8_t, None, Bool, Int, String); ENUM(EffectParameterType, uint8_t, None, Bool, Int, Float, String);
class EffectParameter { class EffectParameter {
private: private:
EffectParameterType _type = EffectParameterType::None; EffectParameterType _type = EffectParameterType::None;
void* _val; union {
bool _b;
int64_t _i;
const std::string _s;
float _f;
};
public: public:
EffectParameter() : _type(EffectParameterType::None), _val(nullptr){}; EffectParameter() : _type(EffectParameterType::None){};
explicit EffectParameter(bool b) : _type(EffectParameterType::Bool), _val((void*)b){}; explicit EffectParameter(bool b) : _type(EffectParameterType::Bool), _b(b){};
explicit EffectParameter(int64_t i) : _type(EffectParameterType::Int), _val((void*)i){}; explicit EffectParameter(int64_t i) : _type(EffectParameterType::Int), _i(i){};
explicit EffectParameter(const std::string& s) explicit EffectParameter(float f) : _type(EffectParameterType::Float), _f(f){};
: _type(EffectParameterType::String), _val(new char[s.size() + 1]) { explicit EffectParameter(const std::string& s) : _type(EffectParameterType::String), _s(s){};
strncpy((char*)_val, s.c_str(), s.size() + 1);
};
EffectParameter(const EffectParameter& other) = delete; EffectParameter(const EffectParameter& other) = delete;
EffectParameter& operator=(const EffectParameter& other) = delete; EffectParameter& operator=(const EffectParameter& other) = delete;
~EffectParameter() { ~EffectParameter() {}
if (_type == EffectParameterType::String) {
delete[](char*) _val;
}
}
EffectParameterType GetType() const noexcept { return _type; } EffectParameterType GetType() const noexcept { return _type; }
bool AsBool() const { bool AsBool() const {
@ -36,7 +35,7 @@ namespace CreatureLib::Library {
ss << "Cast effect parameter to bool, but was " << EffectParameterTypeHelper::ToString(_type); ss << "Cast effect parameter to bool, but was " << EffectParameterTypeHelper::ToString(_type);
throw CreatureException(ss.str()); throw CreatureException(ss.str());
} }
return (bool)_val; return _b;
} }
int64_t AsInt() const { int64_t AsInt() const {
if (_type != EffectParameterType::Int) { if (_type != EffectParameterType::Int) {
@ -44,15 +43,23 @@ namespace CreatureLib::Library {
ss << "Cast effect parameter to int, but was " << EffectParameterTypeHelper::ToString(_type); ss << "Cast effect parameter to int, but was " << EffectParameterTypeHelper::ToString(_type);
throw CreatureException(ss.str()); throw CreatureException(ss.str());
} }
return reinterpret_cast<int64_t>(_val); return _i;
} }
const char* AsString() const { float AsFloat() const {
if (_type != EffectParameterType::Float) {
std::stringstream ss;
ss << "Cast effect parameter to float, but was " << EffectParameterTypeHelper::ToString(_type);
throw CreatureException(ss.str());
}
return _f;
}
const std::string& AsString() const {
if (_type != EffectParameterType::String) { if (_type != EffectParameterType::String) {
std::stringstream ss; std::stringstream ss;
ss << "Cast effect parameter to string, but was " << EffectParameterTypeHelper::ToString(_type); ss << "Cast effect parameter to string, but was " << EffectParameterTypeHelper::ToString(_type);
throw CreatureException(ss.str()); throw CreatureException(ss.str());
} }
return (char*)_val; return _s;
} }
}; };
} }

View File

@ -17,7 +17,12 @@ TEST_CASE("Int EffectParameter", "[Library]") {
TEST_CASE("String EffectParameter", "[Library]") { TEST_CASE("String EffectParameter", "[Library]") {
auto p = EffectParameter(std::string("foobar")); auto p = EffectParameter(std::string("foobar"));
REQUIRE(strcmp(p.AsString(), "foobar") == 0); REQUIRE(p.AsString() == "foobar");
}
TEST_CASE("Float EffectParameter", "[Library]") {
auto p = EffectParameter(1.5f);
REQUIRE(p.AsFloat() == Approx(1.5f));
} }
#endif #endif