resource.monkey2 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. Namespace std.resource
  2. #Import "native/bbresource.cpp"
  3. #Import "native/bbresource.h"
  4. Extern Private
  5. Class BBResource="bbResource"
  6. Protected
  7. Method OnDiscard() Virtual="onDiscard"
  8. Method OnFinalize() Virtual="onFinalize"
  9. Internal
  10. Method InternalDiscard()="discard"
  11. End
  12. Public
  13. #Rem monkeydoc The Resource class.
  14. The resource class helps with managing finite OS resources in a garbage collected environment.
  15. To implement a resource object, you should extend the Resource class and override the [[OnDiscard]] and/or [[OnFinalize]] methods.
  16. Code to actually discard the resource should be placed in OnDiscard. Discarding a resource might involve closing a file, deleting
  17. a texture handle or other similar actions. 'Last chance' cleanup code should be placed in OnFinalize.
  18. IMPORTANT! There are a number of restrictions on code that may be placed in OnFinalize, please refer to the documentation for [[OnFinalize]]
  19. for more information.
  20. #end
  21. Class Resource Extends BBResource
  22. #rem monkeyoc Discards the resource.
  23. If the resource has not yet been discarded, calling this method will cause any internally managed OS resources to be cleaned up.
  24. If the resource has already been discarded, this method does nothing.
  25. Once discarded, a resource should be consider invalid.
  26. #end
  27. Method Discard()
  28. InternalDiscard()
  29. End
  30. Protected
  31. #rem monkeydoc The OnDiscard method.
  32. This is where subclasses should place their actual discard code.
  33. This method will be invoked the first time Resource.Discard() is invoked.
  34. This method will only ever be invoked at most once during the lifetime of a resource.
  35. #end
  36. Method OnDiscard() Override
  37. End
  38. #rem monkeydoc The OnFinalize method.
  39. This method will be invoked when a resource object's memory is about to be reclaimed and if the resource's Discard() method has
  40. never been called during the lifetime of the object.
  41. This method is intended to be used for the 'last chance' cleanup of criticial OS resources, such as file handles, texture objects etc.
  42. ***** WARNING *****
  43. Code inside this method executes at a critical point in the garbage collection process, and should be kept short and sweet.
  44. Code inside OnFinalize MUST obey the following rules:
  45. * Code MUST NOT read or write any fields of Self containing objects, arrays, or function pointers as there is no guarantee that such fields
  46. are still valid when the finalizer executes.
  47. * Code MUST NOT assign Self to any variable.
  48. Failure to follow these rules *will* lead to eventual disaster!
  49. #end
  50. Method OnFinalize() Override
  51. End
  52. End
  53. #rem monkeydoc @hidden
  54. #end
  55. Class ResourceManager Extends Resource
  56. Method New()
  57. _managers.Push( Self )
  58. End
  59. Method OpenResource:Resource( slug:String )
  60. For Local manager:=Eachin _managers
  61. Local r:=manager._retained[slug]
  62. If Not r Continue
  63. If manager<>Self AddResource( slug,r )
  64. Return r
  65. Next
  66. Return Null
  67. End
  68. Method AddResource( slug:String,r:Resource )
  69. If Not r Or _retained.Contains( slug ) Return
  70. _retained[slug]=r
  71. End
  72. Protected
  73. Method OnDiscard() Override
  74. _managers.Remove( Self )
  75. _retained=Null
  76. End
  77. Private
  78. Global _managers:=New Stack<ResourceManager>
  79. Field _retained:=New StringMap<Resource>
  80. End
  81. #rem monkeydoc Safely discards a resource.
  82. Invoke [[Resource.Discard]] on the given resource `r` only if `r` is non-null.
  83. #end
  84. Function SafeDiscard( r:Resource )
  85. If r r.Discard()
  86. End