Added some utilities to ScriptValue for reading from arrays and maps.

This commit is contained in:
Patrick 2024-10-25 00:08:36 +02:00
parent c31dbbd093
commit 44aee31747

View File

@ -10,6 +10,7 @@
#include <variant>
#include <vector>
#include "../container/map_view.hpp"
#include "../container/optional.hpp"
#include "../internal/common.hpp"
#include "../util/traits.hpp"
@ -259,6 +260,60 @@ public:
static_assert(mijin::always_false_v<T>, "Cannot convert to this type.");
}
}
[[nodiscard]]
ScriptValue& operator[](std::size_t index) MIJIN_NOEXCEPT
{
MIJIN_ASSERT_FATAL(isArray(), "Called operator[size_t] on a non-array value.");
return std::get<array_t>(base_)[index];
}
[[nodiscard]]
const ScriptValue& operator[](std::size_t index) const MIJIN_NOEXCEPT
{
MIJIN_ASSERT_FATAL(isArray(), "Called operator[size_t] on a non-array value.");
return std::get<array_t>(base_)[index];
}
[[nodiscard]]
ScriptValue& operator[](const std::string& key) MIJIN_NOEXCEPT
{
MIJIN_ASSERT_FATAL(isMap(), "Called operator[string] on a non-map value.");
return std::get<map_t>(base_)[key];
}
[[nodiscard]]
const ScriptValue& operator[](const std::string& key) const MIJIN_NOEXCEPT
{
MIJIN_ASSERT_FATAL(isMap(), "Called operator[string] on a non-map value.");
return std::get<map_t>(base_)[key];
}
[[nodiscard]]
std::span<ScriptValue> arrayView() MIJIN_NOEXCEPT
{
MIJIN_ASSERT_FATAL(isArray(), "Called iterateArray() on a non-array value.");
return std::get<array_t>(base_);
}
[[nodiscard]]
std::span<const ScriptValue> arrayView() const MIJIN_NOEXCEPT
{
MIJIN_ASSERT_FATAL(isArray(), "Called iterateArray() on a non-array value.");
return std::get<array_t>(base_);
}
MapView<std::string, ScriptValue> mapView() MIJIN_NOEXCEPT
{
MIJIN_ASSERT_FATAL(isMap(), "Called iterateMap() on a non-map value.");
return std::get<map_t>(base_);
}
MapView<std::string, const ScriptValue> mapView() const MIJIN_NOEXCEPT
{
MIJIN_ASSERT_FATAL(isMap(), "Called iterateMap() on a non-map value.");
return std::get<map_t>(base_);
}
};
//