Exemple #1
0
def get_scoped_pycppitem(scope, name, type_only=False):
    import _cppyy

    # resolve typedefs/aliases: these may cross namespaces, in which case
    # the lookup must trigger the creation of all necessary scopes
    scoped_name = (scope == gbl) and name or (scope.__cppname__ + '::' + name)
    final_scoped_name = _cppyy._resolve_name(scoped_name)
    if final_scoped_name != scoped_name:
        pycppitem = get_pycppitem(final_scoped_name)
        # also store on the requested scope (effectively a typedef or pointer copy)
        setattr(scope, name, pycppitem)
        return pycppitem

    pycppitem = None

    # scopes (classes and namespaces)
    cppitem = _cppyy._scope_byname(final_scoped_name)
    if cppitem:
        if cppitem.is_namespace():
            pycppitem = make_cppnamespace(scope, name, cppitem)
        else:
            pycppitem = make_cppclass(scope, name, cppitem)

    if type_only:
        return pycppitem

    # templates
    if not cppitem:
        cppitem = _cppyy._is_template(final_scoped_name)
        if cppitem:
            pycppitem = make_cpptemplatetype(scope, name)
            setattr(scope, name, pycppitem)

    # functions
    if not cppitem:
        try:
            cppitem = scope.__cppdecl__.get_overload(name)
            setattr(scope.__class__, name, cppitem)
            pycppitem = getattr(scope, name)  # binds function as needed
        except AttributeError:
            pass

    # data
    if not cppitem:
        try:
            cppdm = scope.__cppdecl__.get_datamember(name)
            setattr(scope, name, cppdm)
            if _cppyy._is_static_data(cppdm):
                setattr(scope.__class__, name, cppdm)
            pycppitem = getattr(scope, name)  # gets actual property value
        except AttributeError:
            pass

    if pycppitem is not None:  # pycppitem could be a bound C++ NULL, so check explicitly for Py_None
        return pycppitem

    raise AttributeError("'%s' has no attribute '%s'" % (str(scope), name))
Exemple #2
0
def get_pycppitem(scope, name):
    import _cppyy

    # resolve typedefs/aliases
    full_name = (scope == gbl) and name or (scope.__name__ + '::' + name)
    true_name = _cppyy._resolve_name(full_name)
    if true_name != full_name:
        return get_pycppclass(true_name)

    pycppitem = None

    # classes
    cppitem = _cppyy._scope_byname(true_name)
    if cppitem:
        if cppitem.is_namespace():
            pycppitem = make_cppnamespace(scope, true_name, cppitem)
            setattr(scope, name, pycppitem)
        else:
            pycppitem = make_pycppclass(scope, true_name, name, cppitem)

    # templates
    if not cppitem:
        cppitem = _cppyy._template_byname(true_name)
        if cppitem:
            pycppitem = make_cpptemplatetype(scope, name)
            setattr(scope, name, pycppitem)

    # functions
    if not cppitem:
        try:
            cppitem = scope._cpp_proxy.get_overload(name)
            pycppitem = make_static_function(name, cppitem)
            setattr(scope.__class__, name, pycppitem)
            pycppitem = getattr(scope, name)  # binds function as needed
        except AttributeError:
            pass

    # data
    if not cppitem:
        try:
            cppdm = scope._cpp_proxy.get_datamember(name)
            setattr(scope, name, cppdm)
            if cppdm.is_static():
                setattr(scope.__class__, name, cppdm)
            pycppitem = getattr(scope, name)  # gets actual property value
        except AttributeError:
            pass

    if pycppitem is not None:  # pycppitem could be a bound C++ NULL, so check explicitly for Py_None
        return pycppitem

    raise AttributeError("'%s' has no attribute '%s'" % (str(scope), name))