xref: /netbsd-src/external/gpl3/gdb.old/dist/gdbsupport/refcounted-object.h (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* Base class of intrusively reference-counted objects.
2*6881a400Schristos    Copyright (C) 2017-2023 Free Software Foundation, Inc.
37d62b00eSchristos 
47d62b00eSchristos    This file is part of GDB.
57d62b00eSchristos 
67d62b00eSchristos    This program is free software; you can redistribute it and/or modify
77d62b00eSchristos    it under the terms of the GNU General Public License as published by
87d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
97d62b00eSchristos    (at your option) any later version.
107d62b00eSchristos 
117d62b00eSchristos    This program is distributed in the hope that it will be useful,
127d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
137d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
147d62b00eSchristos    GNU General Public License for more details.
157d62b00eSchristos 
167d62b00eSchristos    You should have received a copy of the GNU General Public License
177d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
187d62b00eSchristos 
197d62b00eSchristos #ifndef COMMON_REFCOUNTED_OBJECT_H
207d62b00eSchristos #define COMMON_REFCOUNTED_OBJECT_H
217d62b00eSchristos 
227d62b00eSchristos /* Base class of intrusively reference-countable objects.
237d62b00eSchristos    Incrementing and decrementing the reference count is an external
247d62b00eSchristos    responsibility.  */
257d62b00eSchristos 
267d62b00eSchristos class refcounted_object
277d62b00eSchristos {
287d62b00eSchristos public:
297d62b00eSchristos   refcounted_object () = default;
307d62b00eSchristos 
317d62b00eSchristos   /* Increase the refcount.  */
327d62b00eSchristos   void incref ()
337d62b00eSchristos   {
347d62b00eSchristos     gdb_assert (m_refcount >= 0);
357d62b00eSchristos     m_refcount++;
367d62b00eSchristos   }
377d62b00eSchristos 
387d62b00eSchristos   /* Decrease the refcount.  */
397d62b00eSchristos   void decref ()
407d62b00eSchristos   {
417d62b00eSchristos     m_refcount--;
427d62b00eSchristos     gdb_assert (m_refcount >= 0);
437d62b00eSchristos   }
447d62b00eSchristos 
457d62b00eSchristos   int refcount () const { return m_refcount; }
467d62b00eSchristos 
477d62b00eSchristos private:
487d62b00eSchristos   DISABLE_COPY_AND_ASSIGN (refcounted_object);
497d62b00eSchristos 
507d62b00eSchristos   /* The reference count.  */
517d62b00eSchristos   int m_refcount = 0;
527d62b00eSchristos };
537d62b00eSchristos 
547d62b00eSchristos /* A policy class to interface gdb::ref_ptr with a
557d62b00eSchristos    refcounted_object.  */
567d62b00eSchristos 
577d62b00eSchristos struct refcounted_object_ref_policy
587d62b00eSchristos {
597d62b00eSchristos   static void incref (refcounted_object *ptr)
607d62b00eSchristos   {
617d62b00eSchristos     ptr->incref ();
627d62b00eSchristos   }
637d62b00eSchristos 
647d62b00eSchristos   static void decref (refcounted_object *ptr)
657d62b00eSchristos   {
667d62b00eSchristos     ptr->decref ();
677d62b00eSchristos   }
687d62b00eSchristos };
697d62b00eSchristos 
707d62b00eSchristos #endif /* COMMON_REFCOUNTED_OBJECT_H */
71