PkmnLib/src/ScriptResolving/AngelScript/TypeRegistry/NativeArray.hpp

47 lines
1.8 KiB
C++

#ifndef PKMNLIB_NATIVEARRAY_HPP
#define PKMNLIB_NATIVEARRAY_HPP
#include <Arbutils/Ensure.hpp>
#include <CreatureLib/Defines.hpp>
#include <angelscript.h>
#include <atomic>
template <typename T> class NativeArray {
private:
const T* non_null _array;
std::atomic<u64> _refCount;
void AddRef() { _refCount++; }
void Release() {
if (--_refCount <= 0) {
delete this;
}
}
~NativeArray() = default;
public:
explicit NativeArray(const T* non_null array) : _array(array), _refCount(1) {}
size_t Length() const { return _array->Count(); }
[[nodiscard]] void* nullable At(size_t index) { return _array->At(index).GetRaw(); }
static void Register(asIScriptEngine* non_null engine) {
Ensure(engine->RegisterObjectType("narray<class T>", sizeof(NativeArray), asOBJ_REF | asOBJ_TEMPLATE) >= 0);
Ensure(engine->RegisterObjectBehaviour("narray<T>", asBEHAVE_ADDREF, "void f()", asMETHOD(NativeArray, AddRef),
asCALL_THISCALL));
Ensure(engine->RegisterObjectBehaviour("narray<T>", asBEHAVE_RELEASE, "void f()",
asMETHOD(NativeArray, Release), asCALL_THISCALL));
Ensure(engine->RegisterObjectMethod("narray<T>", "uint64 get_Length() const property",
asMETHOD(NativeArray, Length), asCALL_THISCALL) >= 0);
Ensure(engine->RegisterObjectMethod("narray<T>", "T@ At(uint64 index) const", asMETHOD(NativeArray, At),
asCALL_THISCALL) >= 0);
Ensure(engine->RegisterObjectMethod("narray<T>", "T@ get_opIndex(uint64 index) const property",
asMETHOD(NativeArray, At), asCALL_THISCALL) >= 0);
}
};
#endif // PKMNLIB_NATIVEARRAY_HPP