refcounted-object.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Base class of intrusively reference-counted objects.
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef COMMON_REFCOUNTED_OBJECT_H
  15. #define COMMON_REFCOUNTED_OBJECT_H
  16. /* Base class of intrusively reference-countable objects.
  17. Incrementing and decrementing the reference count is an external
  18. responsibility. */
  19. class refcounted_object
  20. {
  21. public:
  22. refcounted_object () = default;
  23. /* Increase the refcount. */
  24. void incref ()
  25. {
  26. gdb_assert (m_refcount >= 0);
  27. m_refcount++;
  28. }
  29. /* Decrease the refcount. */
  30. void decref ()
  31. {
  32. m_refcount--;
  33. gdb_assert (m_refcount >= 0);
  34. }
  35. int refcount () const { return m_refcount; }
  36. private:
  37. DISABLE_COPY_AND_ASSIGN (refcounted_object);
  38. /* The reference count. */
  39. int m_refcount = 0;
  40. };
  41. /* A policy class to interface gdb::ref_ptr with a
  42. refcounted_object. */
  43. struct refcounted_object_ref_policy
  44. {
  45. static void incref (refcounted_object *ptr)
  46. {
  47. ptr->incref ();
  48. }
  49. static void decref (refcounted_object *ptr)
  50. {
  51. ptr->decref ();
  52. }
  53. };
  54. #endif /* COMMON_REFCOUNTED_OBJECT_H */