Files
PkmnLib/src/ScriptResolving/AngelScript/TypeRegistry/NativeArray.hpp
Deukhoofd accf582a51
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone Build is failing
Style fixes.
2021-09-26 10:53:26 +02:00

45 lines
1.7 KiB
C++

#ifndef PKMNLIB_NATIVEARRAY_HPP
#define PKMNLIB_NATIVEARRAY_HPP
#include <angelscript.h>
#include <atomic>
template <typename T> class NativeArray {
private:
const T* _array;
std::atomic<u64> _refCount;
void AddRef() { _refCount++; }
void Release() {
if (--_refCount <= 0) {
delete this;
}
}
~NativeArray() = default;
public:
explicit NativeArray(const T* array) : _array(array), _refCount(1) {}
size_t Length() const { return _array->Count(); }
[[nodiscard]] void* At(size_t index) { return _array->At(index).GetRaw(); }
static void Register(asIScriptEngine* 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>", "const T@ At(uint64 index) const", asMETHOD(NativeArray, At),
asCALL_THISCALL) >= 0);
Ensure(engine->RegisterObjectMethod("narray<T>", "const T@ get_opIndex(uint64 index) const property",
asMETHOD(NativeArray, At), asCALL_THISCALL) >= 0);
}
};
#endif // PKMNLIB_NATIVEARRAY_HPP