This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFA 01/14] Introduce py-ref.h


On 11/07/2016 05:47 AM, Tom Tromey wrote:

> +/* An instance of this class holds a reference to a PyObject.  When
> +   the object is destroyed, the PyObject is decref'd.
> +
> +   Normally an instance is constructed using a PyObject*.  This sort
> +   of initialization lets this class manage the lifetime of that
> +   reference.
> +
> +   Assignment and copy construction will make a new reference as
> +   appropriate.  Assignment from a plain PyObject* is disallowed to
> +   avoid confusion about whether this acquires a new reference;
> +   instead use the "reset" method -- which, like the PyObject*
> +   constructor, transfers ownership.

I think this should say something about supporting explicitly
wrapping a NULL PyObject *, and how to check whether there's
a managed object.

> +*/
> +class gdbpy_reference
> +{
> + public:
> +
> +  /* Create a new NULL instance.  */
> +  gdbpy_reference ()
> +    : m_obj (NULL)
> +  {
> +  }
> +
> +  /* Create a new NULL instance.  */
> +  gdbpy_reference (nullptr_t)
> +    : m_obj (NULL)
> +  {
> +  }

I don't think this overload is needed, since there's no
ambiguity without it.  If you don't provide it, initializing
with nullptr selects the one below, with the same result.

Otherwise, shouldn't this overload be explicit too?

> +
> +  /* Create a new instance.  OBJ is a reference, management of which
> +     is now transferred to this class.  */
> +  explicit gdbpy_reference (PyObject *obj)
> +    : m_obj (obj)
> +  {
> +  }

> +  /* Equality.  */
> +  bool operator== (const PyObject *other) const
> +  {
> +    return m_obj == other;
> +  }
> +
> +  /* Inequality.  */
> +  bool operator!= (const PyObject *other) const
> +  {
> +    return m_obj != other;
> +  }

This supports:

  gdbpy_reference == PyObject *

Seems to me we should support these too:

  gdbpy_reference == gdbpy_reference
  PyObject *      == gdbpy_reference

Thanks,
Pedro Alves


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]