BsScriptRRefBase.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsScriptEnginePrerequisites.h"
  5. #include "BsScriptObject.h"
  6. #include "Image/BsTexture.h"
  7. #include "Wrappers/BsScriptResource.h"
  8. #include "BsMonoClass.h"
  9. #include "Reflection/BsRTTIType.h"
  10. namespace bs
  11. {
  12. /** @addtogroup ScriptInteropEngine
  13. * @{
  14. */
  15. /** Interop class between C++ & CLR for RRefBase and RRef<T>. */
  16. class BS_SCR_BE_EXPORT ScriptRRefBase : public ScriptObject<ScriptRRefBase>
  17. {
  18. public:
  19. SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "RRefBase")
  20. /** Returns a weak handle to the resource referenced by this object. */
  21. ResourceHandle<Resource> getHandle() const { return mResource; }
  22. /** Returns the managed version of this object. */
  23. MonoObject* getManagedInstance() const;
  24. /**
  25. * Creates a new managed RRefBase for the provided resource.
  26. *
  27. * @param[in] handle Handle to the resource to wrap.
  28. * @param[in] rawType Class of the RRef type to use for wrapping the resource. If null then the resource
  29. * will be wrapped in a non-specific RRefBase object. Otherwise it will be wrapped in a
  30. * templated RRef<T> object. In the latter case caller is responsible for ensuring the
  31. * template parameter of RRef matches the actual resource type.
  32. */
  33. template<class T>
  34. static ScriptRRefBase* create(const ResourceHandle<T>& handle, ::MonoClass* rawType = nullptr)
  35. {
  36. return createInternal(handle, rawType);
  37. }
  38. /** Creates a RRef type with the provided class bound as its template parameter. */
  39. static ::MonoClass* bindGenericParam(::MonoClass* param);
  40. private:
  41. friend class ScriptResourceManager;
  42. ScriptRRefBase(MonoObject* instance, ResourceHandle<Resource> handle);
  43. ~ScriptRRefBase();
  44. /** @copydoc ScriptObjectBase::_clearManagedInstance */
  45. void _clearManagedInstance() override;
  46. /** @copydoc ScriptObjectBase::_onManagedInstanceDeleted */
  47. void _onManagedInstanceDeleted(bool assemblyRefresh) override;
  48. /** Clears the internal cached ScriptResource reference. Should be called if the resource got destroyed. */
  49. void clearResource() { mScriptResource = nullptr; }
  50. /** @copydoc create() */
  51. static ScriptRRefBase* createInternal(const ResourceHandle<Resource>& handle, ::MonoClass* type = nullptr);
  52. ResourceHandle<Resource> mResource;
  53. ScriptResourceBase* mScriptResource = nullptr;
  54. UINT32 mGCHandle;
  55. /************************************************************************/
  56. /* CLR HOOKS */
  57. /************************************************************************/
  58. static bool internal_IsLoaded(ScriptRRefBase* thisPtr);
  59. static MonoObject* internal_GetResource(ScriptRRefBase* thisPtr);
  60. static void internal_GetUUID(ScriptRRefBase* thisPtr, UUID* uuid);
  61. static MonoObject* internal_CastAs(ScriptRRefBase* thisPtr, MonoReflectionType* type);
  62. };
  63. /** @} */
  64. }