public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Siva Chandra <sivachandra@google.com>
To: Doug Evans <xdje42@gmail.com>
Cc: Tom Tromey <tromey@redhat.com>, gdb-patches <gdb-patches@sourceware.org>
Subject: Re: [RFC/Patch] Call overloaded operators to perform valid Python operations on struct/class values.
Date: Wed, 18 Dec 2013 23:15:00 -0000	[thread overview]
Message-ID: <CAGyQ6gy9E3J8mbG0xKTTwH8=MYG2naeBhMtwmNb5f=m9Xsmufg@mail.gmail.com> (raw)
In-Reply-To: <CAP9bCMTthY1oJN-res0Cegd_YrMqW77ZehMjnzK+Y9681Of5bQ@mail.gmail.com>

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 :-)]

  reply	other threads:[~2013-12-18 23:15 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-02 19:28 Siva Chandra
2013-12-06  6:25 ` Doug Evans
2013-12-06 14:20   ` Siva Chandra
2013-12-11 20:18     ` Tom Tromey
2013-12-11 20:17   ` Tom Tromey
2013-12-16  7:48     ` Doug Evans
2013-12-16 22:24       ` Siva Chandra
2013-12-18 16:37         ` Doug Evans
2013-12-18 23:15           ` Siva Chandra [this message]
2013-12-19 14:11             ` Doug Evans
2013-12-19 17:50               ` Siva Chandra
2013-12-20 22:29                 ` Siva Chandra
2013-12-21  8:21                   ` Eli Zaretskii
2013-12-30 14:40                     ` Siva Chandra
2013-12-30 17:57                       ` Eli Zaretskii
2014-01-22 21:39                       ` Siva Chandra
2014-01-25 18:45                         ` Doug Evans

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAGyQ6gy9E3J8mbG0xKTTwH8=MYG2naeBhMtwmNb5f=m9Xsmufg@mail.gmail.com' \
    --to=sivachandra@google.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@redhat.com \
    --cc=xdje42@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).