Replace void* for script parameter with EffectParameter class.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-04-10 17:54:45 +02:00
parent 1537a5f316
commit e934e13950
4 changed files with 70 additions and 10 deletions

View File

@@ -0,0 +1,53 @@
#ifndef CREATURELIB_EFFECTPARAMETER_HPP
#define CREATURELIB_EFFECTPARAMETER_HPP
#include <Arbutils/Enum.hpp>
#include <cstring>
#include "Exceptions/CreatureException.hpp"
namespace CreatureLib::Library {
ENUM(EffectParameterType, uint8_t, None, Bool, Int, String);
class EffectParameter {
private:
EffectParameterType _type = EffectParameterType::None;
void* _val;
public:
EffectParameter() : _type(EffectParameterType::None), _val(nullptr){};
EffectParameter(bool b) : _type(EffectParameterType::Bool), _val((void*)b){};
EffectParameter(int64_t i) : _type(EffectParameterType::Int), _val((void*)i){};
EffectParameter(const std::string& s) : _type(EffectParameterType::String), _val(new char[s.size() + 1]) {
strncpy((char*)_val, s.c_str(), s.size() + 1);
};
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 (bool)_val;
}
int64_t AsInt() const {
if (_type != EffectParameterType::Int) {
std::stringstream ss;
ss << "Cast effect parameter to int, but was " << EffectParameterTypeHelper::ToString(_type);
throw CreatureException(ss.str());
}
return reinterpret_cast<int64_t>(_val);
}
const char* 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 (char*)_val;
}
};
}
#endif // CREATURELIB_EFFECTPARAMETER_HPP