From 9ed86260671046bf02f17407ba4dd0a1386190a8 Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Sun, 11 Jul 2021 17:11:15 +0200 Subject: [PATCH] Fix array class --- src/game/scripting/array.cpp | 79 ++++++++++++++++++++++++++++- src/game/scripting/array.hpp | 26 +++++++--- src/game/scripting/script_value.hpp | 6 +-- 3 files changed, 101 insertions(+), 10 deletions(-) diff --git a/src/game/scripting/array.cpp b/src/game/scripting/array.cpp index 8c96002..ce30cb4 100644 --- a/src/game/scripting/array.cpp +++ b/src/game/scripting/array.cpp @@ -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 values) { this->id_ = make_array(); + this->add(); for (const auto& value : values) { @@ -63,6 +70,7 @@ namespace scripting array::array(std::unordered_map 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(this->id_)}); + } + } + + void array::release() const + { + if (this->id_) + { + game::RemoveRefToValue(game::SCRIPT_OBJECT, {static_cast(this->id_)}); + } + } + std::vector array::get_keys() const { std::vector 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(); diff --git a/src/game/scripting/array.hpp b/src/game/scripting/array.hpp index ab6f905..f34b9c0 100644 --- a/src/game/scripting/array.hpp +++ b/src/game/scripting/array.hpp @@ -7,15 +7,15 @@ namespace scripting { bool is_string = false; bool is_integer = false; - int index{}; + unsigned int index{}; std::string key{}; }; class array_value : public script_value { public: - array_value(unsigned int parent_id, unsigned int id); - void operator=(const script_value& value); + array_value(unsigned int, unsigned int); + void operator=(const script_value&); private: unsigned int id_; unsigned int parent_id_; @@ -25,21 +25,35 @@ namespace scripting { public: array(); - array(unsigned int); + array(const unsigned int); + array(std::vector); array(std::unordered_map); + 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 get_keys() const; unsigned int size() const; unsigned int push(script_value) const; - void erase(const unsigned int index) const; - void erase(const std::string& key) const; + void erase(const unsigned int) const; + void erase(const std::string&) const; script_value pop() const; + script_value get(const array_key&) const; script_value get(const std::string&) 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 unsigned int, const script_value&) const; diff --git a/src/game/scripting/script_value.hpp b/src/game/scripting/script_value.hpp index 8fb4267..67fd83d 100644 --- a/src/game/scripting/script_value.hpp +++ b/src/game/scripting/script_value.hpp @@ -49,15 +49,15 @@ namespace scripting return get(); } - template + template T* as_ptr() { - if (!this->is()) + if (!this->is()) { throw std::runtime_error("Invalid type"); } - const auto value = this->get(); + const auto value = this->get(); if (!value) {