diff --git a/module/Sqrat/sqratObject.h b/module/Sqrat/sqratObject.h index 798b51c6..b1ad1538 100644 --- a/module/Sqrat/sqratObject.h +++ b/module/Sqrat/sqratObject.h @@ -159,6 +159,31 @@ public: } } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Constructs an Object from a C++ instance only if that instance exists. Otherwise, null is beings used. + /// + /// \param instance Pointer to a C++ class instance that has been bound already + /// \param v VM that the object will exist in + /// + /// \tparam T Type of instance + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + template + Object(T* instance, std::nullptr_t, HSQUIRRELVM vm = SqVM()) : mRelease(true) { + // Preserve the stack state + const StackGuard sg(vm); + // Push the instance on the stack + ClassType::PushInstance(vm, instance, nullptr); + // Attempt to retrieve it + if (SQ_FAILED(sq_getstackobj(vm, -1, &mObj))) { + sq_resetobject(&mObj); + // nothing to release anymore + mRelease = false; + } else { + sq_addref(vm, &mObj); + } + } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Constructs an Object from a C++ type ///