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: [RFC/Patch] Call overloaded operators to perform valid Python operations on struct/class values.


On Wed, Dec 18, 2013 at 8:37 AM, Doug Evans <xdje42@gmail.com> wrote:
> Not that this could work, but for illustration's sake:
> smart_ptr = gdb.parse_and_eval ("my_smart_ptr");
> dereferenced_smart_ptr = gdb.parse_and_eval("*%V" % smart_ptr)

Should work. May be the '%' syntax would not (I am not sure). But, we
could consider other alternatives [Replace occurrences of $1, $2, etc
for example].

Is this an exhaustive solution or a cool fallback option when no other
Pythonic way works? Consider something more complicated but uses only
valid Python operators:

  C++:
    obj1.method1(obj2_ptr->method2(obj3 + obj4)) * obj5

  This could be done in a Pythonic way as:
    obj1.invoke('method1', obj2_ptr.invoke('method2', obj3 + obj4)) * obj5

  If we do this in a string replacement way:
    gdb.parse_and_eval('$1.method1($2->method2($3 + $4)) * $5',
                        obj1, obj2_ptr, obj3, obj4, obj5)

Above, the values objN in Python are equivalents of their C++ values.
Also, I have cooked up the 'invoke' method on gdb.Value objects but we
could have one for real. Lets see how your example would look like
with this 'invoke' in place:

  C++:
    *my_smart_ptr;

  Python:
    smart_ptr.invoke('operator*')

Or, one could have a debug method named "deref" (assuming debug
methods would soon be a reality :-) to be an equivalent of
"operator*". The Python would then look like this:
    smart_ptr.invoke('deref')

[Side topic: Should smart_ptr.dereference() invoke 'operator*'? The
documentation says this: "... behavior of Value.dereference is
identical to applying the C unary operator * on a given value." And, I
remember writing this myself :-)]


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