Add operator[] with debug-only bounds checking to loco_global_array

This commit is contained in:
Justin Gottula 2018-01-21 23:36:57 -08:00
parent dbf1240128
commit 63ef91302c
1 changed files with 13 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <stdexcept>
#include <vector>
#define assert_struct_size(x, y) static_assert(sizeof(x) == (y), "Improper struct size")
@ -156,6 +157,18 @@ namespace openloco::interop
return get();
}
T& operator[](int idx)
{
#ifndef NDEBUG
if (idx < 0 || static_cast<size_t>(idx) >= size())
{
throw std::out_of_range("loco_global_array: bounds check violation!");
}
#endif
return (get())[idx];
}
T* get()
{
return &(addr<TAddress, T>());