Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [compiler.services.ReferenceCountedObject<uinteger> = {
  2.     increment = IncRefCount,
  3.     decrement = DecRefCount,
  4.     lifecycle = managed
  5. }] class RefCountedObject
  6.    
  7.     '' Construct the object and initalize the refcount to 1
  8.     constructor(refCount as uinteger ptr)
  9.         if (refCount = null) then
  10.             m_refCount = new uinteger(1)
  11.         else
  12.             m_refCount = refCount
  13.             IncRefCount()
  14.         end if
  15.     end constructor
  16.  
  17.     ''
  18.     '' destructor should only be called when refcount reaches 0
  19.     '' for ref counted objects I think compiler will simply call
  20.     '' DecRefCount when out of scope. Even when explicitly
  21.     '' delete obj ?????
  22.     private destructor
  23.         assert(m_refCount <> null and *m_refCount = 0)
  24.         delete m_refCount
  25.         m_refCount = 0
  26.     end destructor
  27.    
  28.     '' Increment the reference count
  29.     '' include some kind of guard to protect the operation
  30.     sub IncRefCount
  31.         *m_refCount += 1
  32.     end sub
  33.    
  34.     '' decrement the reference count
  35.     '' include some kind of guard to protect the operation
  36.     sub DecRefCount
  37.         *m_refCount -= 1
  38.         ' destroy ?
  39.         if (*m_refCount = 0) then
  40.             delete @this
  41.         end if
  42.     end sub
  43.    
  44.     '' get reference count
  45.     function GetRefCount as uinteger
  46.         assert(m_refCount <> null)
  47.         return *m_refCount
  48.     end function
  49.    
  50.     '' get the reference count ptr
  51.     function GetRefCountPtr as uinteger ptr
  52.         return m_refCount
  53.     end function
  54.  
  55.     '' the reference count variable
  56.     private m_refCount as uinteger ptr
  57.  
  58. end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement