Fix array class

This commit is contained in:
Federico Cecchetto
2021-07-11 17:11:15 +02:00
parent cb0725bad1
commit 9ed8626067
3 changed files with 101 additions and 10 deletions
+78 -1
View File
@@ -40,19 +40,26 @@ namespace scripting
this->value_ = value;
}
array::array(unsigned int id)
array::array(const unsigned int id)
: id_(id)
{
this->add();
}
array::array(const array& other) : array(other.id_)
{
}
array::array()
{
this->id_ = make_array();
this->add();
}
array::array(std::vector<script_value> values)
{
this->id_ = make_array();
this->add();
for (const auto& value : values)
{
@@ -63,6 +70,7 @@ namespace scripting
array::array(std::unordered_map<std::string, script_value> values)
{
this->id_ = make_array();
this->add();
for (const auto& value : values)
{
@@ -70,6 +78,51 @@ namespace scripting
}
}
array::~array()
{
this->release();
}
array& array::operator=(const array& other)
{
if (&other != this)
{
this->release();
this->id_ = other.id_;
this->add();
}
return *this;
}
array& array::operator=(array&& other) noexcept
{
if (&other != this)
{
this->release();
this->id_ = other.id_;
other.id_ = 0;
}
return *this;
}
void array::add() const
{
if (this->id_)
{
game::AddRefToValue(game::SCRIPT_OBJECT, {static_cast<int>(this->id_)});
}
}
void array::release() const
{
if (this->id_)
{
game::RemoveRefToValue(game::SCRIPT_OBJECT, {static_cast<int>(this->id_)});
}
}
std::vector<array_key> array::get_keys() const
{
std::vector<array_key> result;
@@ -147,6 +200,18 @@ namespace scripting
return value;
}
script_value array::get(const array_key& key) const
{
if (key.is_integer)
{
return this->get(key.index);
}
else
{
return this->get(key.key);
}
}
script_value array::get(const std::string& key) const
{
const auto string_value = game::SL_GetString(key.data(), 0);
@@ -182,6 +247,18 @@ namespace scripting
return variable;
}
void array::set(const array_key& key, const script_value& value) const
{
if (key.is_integer)
{
this->set(key.index, value);
}
else
{
this->set(key.key, value);
}
}
void array::set(const std::string& key, const script_value& _value) const
{
const auto value = _value.get_raw();