add the RefCount ability to class or RefObject with RefCount and AddRef/Release Support.
ref-count-able ability
add the RefCount ability to any class directly.
the following members will be added to your class. and you should implement
the destroy
method which will be called by release
/free
.
- properties:
- RefCount (integer): the reference count.
- methods:
release()
/free()
: Decrements reference count for this instance. If it is becoming less than 0, the object would be (self) destroyed.addRef()
: Increments the reference count for this instance and returns the new reference count.
refCountable = require 'ref-object/ability' refCountable MyClass : ->consolelog 'destroy' my = myaddRefmyfree # nothing myfree # print the 'destroy' here.
RefObject
The RefObject
is derived from AbstractObject. and add the RefCount
and AddRef/Release
Supports.
- methods:
release()
/free()
: Decrements reference count for this instance. If it is becoming less than 0, the object would be (self) destroyed.addRef()
: Increments the reference count for this instance and returns the new reference count.
Usage:
eventable = require'ref-object/eventable'RefObject = require'ref-object'inherits = require'inherits-ex'createObject = require'inherits-ex/lib/createObject' # if you wanna add the eventable ability to RefObject # RefObject = eventable RefObject # or directly add to MyObject # eventable MyObject inherits MyObjectRefObject : @cache = super : ->@cache=null myObj = createObjectMyObject12 myObjaddRefmyObjfree # it does not free for addRef() before. assertok @cachemyObjfree # free now. assertnotOk @cache # if you do not wanna use `createObject`, you MUST remember this: # even the constructor is empty, you should call the parent's constructor manually. # myObj = new MyObject() inherits MyObjectRefObject : -> # must call super method here: super : # must call super method here for RefObject initialization: super
the javascript:
var RefObject = var inherits = var createObject = //if you do not wanna to use the createObject:var { //super call MyObject__super__constructor;}// or, this MUST use 'AbstractObject.create'var {} MyObjectprototype { //super call MyObject__super__initialize thisa = a thisb = b} var myObj = //or this, must overwrite the constructor and call the super constructor.var myObj = 12myObjmyObj // it does not free for addRef() before.assertmyObj // free now.assert