#ifndef PKMNLIB_NATIVEARRAY_HPP #define PKMNLIB_NATIVEARRAY_HPP #include #include #include #include template class NativeArray { private: const T* non_null _array; std::atomic _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", sizeof(NativeArray), asOBJ_REF | asOBJ_TEMPLATE) >= 0); Ensure(engine->RegisterObjectBehaviour("narray", asBEHAVE_ADDREF, "void f()", asMETHOD(NativeArray, AddRef), asCALL_THISCALL)); Ensure(engine->RegisterObjectBehaviour("narray", asBEHAVE_RELEASE, "void f()", asMETHOD(NativeArray, Release), asCALL_THISCALL)); Ensure(engine->RegisterObjectMethod("narray", "uint64 get_Length() const property", asMETHOD(NativeArray, Length), asCALL_THISCALL) >= 0); Ensure(engine->RegisterObjectMethod("narray", "T@ At(uint64 index) const", asMETHOD(NativeArray, At), asCALL_THISCALL) >= 0); Ensure(engine->RegisterObjectMethod("narray", "T@ get_opIndex(uint64 index) const property", asMETHOD(NativeArray, At), asCALL_THISCALL) >= 0); } }; #endif // PKMNLIB_NATIVEARRAY_HPP