Counted Pointer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /******************************************************************************/
  2. struct PtrCounter
  3. {
  4. Bool anyPtrs()C {return _ptrs>0;}
  5. PtrCounter() {_ptrs=-1;}
  6. ~PtrCounter();
  7. #if !EE_PRIVATE
  8. private:
  9. #endif
  10. Int _ptrs;
  11. #if EE_PRIVATE
  12. private:
  13. #endif
  14. void decRef(void (*unload)(Ptr elm));
  15. void incRef(Bool (* load)(Ptr elm));
  16. T1(TYPE) friend struct CountedPtr;
  17. };
  18. /******************************************************************************/
  19. T1(TYPE) struct CountedPtr // Counted Element Pointer - can hold a reference to a TYPE object which must be based on 'PtrCounter'
  20. {
  21. // operators
  22. TYPE* operator () ( )C {return T._data ;} // access the data, you can use the returned pointer as long as this 'CountedPtr' object exists and not modified
  23. TYPE* operator -> ( )C {return T._data ;} // access the data, you can use the returned pointer as long as this 'CountedPtr' object exists and not modified
  24. TYPE& operator * ( )C {return *T._data ;} // access the data, you can use the returned reference as long as this 'CountedPtr' object exists and not modified
  25. Bool operator == ( null_t )C {return T._data==null ;} // if pointers are equal
  26. Bool operator != ( null_t )C {return T._data!=null ;} // if pointers are different
  27. Bool operator == (C TYPE *data)C {return T._data==data ;} // if pointers are equal
  28. Bool operator != (C TYPE *data)C {return T._data!=data ;} // if pointers are different
  29. Bool operator == (C CountedPtr &eptr)C {return T._data==eptr._data;} // if pointers are equal
  30. Bool operator != (C CountedPtr &eptr)C {return T._data!=eptr._data;} // if pointers are different
  31. operator Bool( )C {return T._data!=null ;} // if pointer is valid
  32. // operations
  33. CountedPtr& clear ( ); // clear the pointer to null , this automatically decreases the reference count of current data
  34. CountedPtr& operator=( null_t ); // clear the pointer to null , this automatically decreases the reference count of current data
  35. CountedPtr& operator=( TYPE * data); // set pointer to 'data', this automatically decreases the reference count of current data and increases the reference count of the new data
  36. CountedPtr& operator=(C CountedPtr & eptr); // set pointer to 'eptr', this automatically decreases the reference count of current data and increases the reference count of the new data
  37. CountedPtr& operator=( CountedPtr &&eptr); // set pointer to 'eptr', this automatically decreases the reference count of current data and increases the reference count of the new data
  38. // constructors / destructors
  39. CountedPtr( null_t=null ); // initialize the pointer with null
  40. CountedPtr( TYPE * data); // initialize the pointer with 'data', this automatically increases the reference count of the 'data'
  41. CountedPtr(C CountedPtr & eptr); // initialize the pointer with 'eptr', this automatically increases the reference count of the 'eptr'
  42. CountedPtr( CountedPtr &&eptr); // initialize the pointer with 'eptr', this automatically increases the reference count of the 'eptr'
  43. ~CountedPtr( ); // release the pointer , this automatically decreases the reference count of current data
  44. private:
  45. TYPE *_data;
  46. static void DecRef(TYPE *data);
  47. static void IncRef(TYPE *data);
  48. };
  49. /******************************************************************************/
  50. void DelayRemoveTime(Flt time); // set amount of time (in seconds) after which unused elements are removed (<=0 value specifies immediate unloading), default=0
  51. void DelayRemoveNow ( ); // immediately remove all elements marked for delay removal at a later time to free as much memory as possible
  52. #if EE_PRIVATE
  53. void DelayRemoveUpdate();
  54. #endif
  55. /******************************************************************************/