refcounted.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* ScummVM Tools
  2. *
  3. * ScummVM Tools is the legal property of its developers, whose
  4. * names are too numerous to list here. Please refer to the
  5. * COPYRIGHT file distributed with this source distribution.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #ifndef REFCOUNTED_H
  22. #define REFCOUNTED_H
  23. class RefCounted;
  24. inline void intrusive_ptr_add_ref(RefCounted *p);
  25. inline void intrusive_ptr_release(RefCounted *p);
  26. /**
  27. * Provides a base implementation of reference counting for use with boost::intrusive_ptr.
  28. */
  29. class RefCounted {
  30. private:
  31. long _refCount; ///< Reference count used for boost::intrusive_ptr.
  32. friend void ::intrusive_ptr_add_ref(RefCounted *p); ///< Allow access by reference counting methods.
  33. friend void ::intrusive_ptr_release(RefCounted *p); ///< Allow access by reference counting methods.
  34. protected:
  35. RefCounted() : _refCount(0) { }
  36. virtual ~RefCounted() { }
  37. };
  38. /**
  39. * Add a reference to a pointer.
  40. */
  41. inline void intrusive_ptr_add_ref(RefCounted *p) {
  42. ++(p->_refCount);
  43. }
  44. /**
  45. * Remove a reference from a pointer.
  46. */
  47. inline void intrusive_ptr_release(RefCounted *p) {
  48. if (--(p->_refCount) == 0)
  49. delete p;
  50. }
  51. #endif