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

View File

@ -40,19 +40,26 @@ namespace scripting
this->value_ = value; this->value_ = value;
} }
array::array(unsigned int id) array::array(const unsigned int id)
: id_(id) : id_(id)
{
this->add();
}
array::array(const array& other) : array(other.id_)
{ {
} }
array::array() array::array()
{ {
this->id_ = make_array(); this->id_ = make_array();
this->add();
} }
array::array(std::vector<script_value> values) array::array(std::vector<script_value> values)
{ {
this->id_ = make_array(); this->id_ = make_array();
this->add();
for (const auto& value : values) for (const auto& value : values)
{ {
@ -63,6 +70,7 @@ namespace scripting
array::array(std::unordered_map<std::string, script_value> values) array::array(std::unordered_map<std::string, script_value> values)
{ {
this->id_ = make_array(); this->id_ = make_array();
this->add();
for (const auto& value : values) 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> array::get_keys() const
{ {
std::vector<array_key> result; std::vector<array_key> result;
@ -147,6 +200,18 @@ namespace scripting
return value; 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 script_value array::get(const std::string& key) const
{ {
const auto string_value = game::SL_GetString(key.data(), 0); const auto string_value = game::SL_GetString(key.data(), 0);
@ -182,6 +247,18 @@ namespace scripting
return variable; 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 void array::set(const std::string& key, const script_value& _value) const
{ {
const auto value = _value.get_raw(); const auto value = _value.get_raw();

View File

@ -7,15 +7,15 @@ namespace scripting
{ {
bool is_string = false; bool is_string = false;
bool is_integer = false; bool is_integer = false;
int index{}; unsigned int index{};
std::string key{}; std::string key{};
}; };
class array_value : public script_value class array_value : public script_value
{ {
public: public:
array_value(unsigned int parent_id, unsigned int id); array_value(unsigned int, unsigned int);
void operator=(const script_value& value); void operator=(const script_value&);
private: private:
unsigned int id_; unsigned int id_;
unsigned int parent_id_; unsigned int parent_id_;
@ -25,21 +25,35 @@ namespace scripting
{ {
public: public:
array(); array();
array(unsigned int); array(const unsigned int);
array(std::vector<script_value>); array(std::vector<script_value>);
array(std::unordered_map<std::string, script_value>); array(std::unordered_map<std::string, script_value>);
array(const array& other);
array(array&& other) noexcept;
~array();
array& operator=(const array& other);
array& operator=(array&& other) noexcept;
void add() const;
void release() const;
std::vector<array_key> get_keys() const; std::vector<array_key> get_keys() const;
unsigned int size() const; unsigned int size() const;
unsigned int push(script_value) const; unsigned int push(script_value) const;
void erase(const unsigned int index) const; void erase(const unsigned int) const;
void erase(const std::string& key) const; void erase(const std::string&) const;
script_value pop() const; script_value pop() const;
script_value get(const array_key&) const;
script_value get(const std::string&) const; script_value get(const std::string&) const;
script_value get(const unsigned int) const; script_value get(const unsigned int) const;
void set(const array_key&, const script_value&) const;
void set(const std::string&, const script_value&) const; void set(const std::string&, const script_value&) const;
void set(const unsigned int, const script_value&) const; void set(const unsigned int, const script_value&) const;

View File

@ -49,15 +49,15 @@ namespace scripting
return get<T>(); return get<T>();
} }
template <typename T> template <typename T, typename I = int>
T* as_ptr() T* as_ptr()
{ {
if (!this->is<int>()) if (!this->is<I>())
{ {
throw std::runtime_error("Invalid type"); throw std::runtime_error("Invalid type");
} }
const auto value = this->get<int>(); const auto value = this->get<I>();
if (!value) if (!value)
{ {