[C++-sig] [Py++] Help with wrapping setter methods

Roman Yakovenko roman.yakovenko at gmail.com
Tue Sep 29 10:27:09 CEST 2009


On Tue, Sep 29, 2009 at 12:44 AM, Christopher Bruns
<cmbruns at stanford.edu> wrote:
> Please help.  I am just a beginner with pyplusplus and am not smart
> enough to see how to create certain complex rules.
>
> How would I create a pyplusplus rule for setting the return policy to
> "return internal reference" for all member functions that meet the
> following criteria:
>  * method name begins with "set"
>  * return type is a reference to the parent class or to one of its base classes
>
> I would sincerely appreciate an example that could do this.  Then
> perhaps I might be able to figure out how to perform other clever
> manipulations.  Thanks in advance for your help.

I suggest you to read the following document:
http://language-binding.net/pygccxml/query_interface.html
It should clarify few things

> #################################
> struct Foo {
>    // example of the sort of method I mean
>    Foo& setBaz(int baz); // returns reference to self
> };
> #################################
> from pyplusplus import module_builder
> from pyplusplus.module_builder.call_policies import *
> mb = module_builder(...)
> # want "something" below...
> mb.decls(<something?>).call_policies = return_internal_reference()
> #################################

from pygccxml import declarations

def is_my_case( f ):
    if not f.name.startswith( 'set' ):
        return False
    rt = f.return_type
    if not rt:
        return False
    parent_ref = declarations.reference_t( declarations.declarated_t(
f.parent ) )
    return declarations.is_same( parent_ref, rt )

mb.member_functions(is_my_case).call_policies = return_internal_reference()

Untested, but you should get the idea. It is also possible to write
this query in more efficient form, but this I will leave to you :-)

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/


More information about the Cplusplus-sig mailing list