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

47 lines
1.8 KiB
C++
Raw Normal View History

#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;
2021-09-26 08:45:37 +00:00
std::atomic<u64> _refCount;
2021-09-26 08:53:26 +00:00
void AddRef() { _refCount++; }
void Release() {
2021-09-26 08:53:26 +00:00
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) {
2021-09-26 08:53:26 +00:00
Ensure(engine->RegisterObjectType("narray<class T>", sizeof(NativeArray), asOBJ_REF | asOBJ_TEMPLATE) >= 0);
2021-09-26 08:53:26 +00:00
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));
2021-09-26 08:53:26 +00:00
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",
2021-09-26 08:53:26 +00:00
asMETHOD(NativeArray, At), asCALL_THISCALL) >= 0);
}
};
#endif // PKMNLIB_NATIVEARRAY_HPP