Introduce ref_ptr::new_reference
authorTom Tromey <tom@tromey.com>
Mon, 30 Apr 2018 02:59:21 +0000 (20:59 -0600)
committerTom Tromey <tom@tromey.com>
Mon, 30 Apr 2018 17:33:11 +0000 (11:33 -0600)
I noticed a common pattern with gdb::ref_ptr, where callers would
"incref" and then create a new wrapper object, like:

    Py_INCREF (obj);
    gdbpy_ref<> ref (obj);

The ref_ptr constructor intentionally does not acquire a new
reference, but it seemed to me that it would be reasonable to add a
static member function that does so.

In this patch I chose to call the function "new_reference".  I
considered "acquire_reference" as well, but "new" seemed less
ambiguous than "acquire" to me.

ChangeLog
2018-04-30  Tom Tromey  <tom@tromey.com>

* common/gdb_ref_ptr.h (ref_ptr::new_reference): New static
method.

gdb/ChangeLog
gdb/common/gdb_ref_ptr.h

index 9ca2e8e957437f0079588610f6923d0c269d3653..b65ec81e359ba9d776f5fc9e56a649247c883b8a 100644 (file)
@@ -1,3 +1,8 @@
+2018-04-30  Tom Tromey  <tom@tromey.com>
+
+       * common/gdb_ref_ptr.h (ref_ptr::new_reference): New static
+       method.
+
 2018-04-30  Tom Tromey  <tom@tromey.com>
 
        * jit.c (jit_read_code_entry): Use type_align.
index 57d1db96e6cd6b50789e82ebae52825dc635a84c..768c8136928ce504d72642e7ae7a7e7a587d6833 100644 (file)
@@ -149,6 +149,13 @@ class ref_ptr
     return m_obj;
   }
 
+  /* Acquire a new reference and return a ref_ptr that owns it.  */
+  static ref_ptr<T, Policy> new_reference (T *obj)
+  {
+    Policy::incref (obj);
+    return ref_ptr<T, Policy> (obj);
+  }
+
  private:
 
   T *m_obj;
This page took 0.028737 seconds and 4 git commands to generate.