[CastXML] Parsing class with nested struct definition.

Michka Popoff michkapopoff at gmail.com
Sat Mar 26 06:27:26 EDT 2016


Hi

as you discovered, the variables() method will return all the variables found
under a specific node in the declarations tree.

I wrote a small example showing how to access the parent declaration of a variable.
The loops at the end show how you could filter out the variables from the different
classes/structs. The last one shows how to use the recursive=False option to get
variables from the first level only.

I hope this helps.

Michka




from pygccxml import utils
from pygccxml import parser
from pygccxml import declarations

generator_path, generator_name = utils.find_xml_generator()
xml_generator_config = parser.xml_generator_configuration_t(
    xml_generator_path=generator_path,
    xml_generator=generator_name)

code = """class MyClass {
   struct SomeStruct {
      int structmem;
   };

   SomeStruct classmem;
};
        """

decls = parser.parse_string(code, xml_generator_config)
ns = declarations.get_global_namespace(decls)
myclass = ns.classes()[0]
somestruct = ns.classes()[0].classes()[0]

# MyClass [class]
print(myclass)

# MyClass::SomeStruct [struct]
print(somestruct)

# MyClass::SomeStruct::structmem [variable]
print(myclass.classes()[0].variables()[0])

# MyClass::SomeStruct::structmem [variable]
# MyClass::classmem [variable]
for var in myclass.variables():
    if isinstance(var, declarations.variable_t):
        print(var)

# MyClass::SomeStruct [struct]
print(myclass.classes()[0].variables()[0].parent)

# Found: MyClass::SomeStruct::structmem [variable]
for var in myclass.variables():
    if isinstance(var, declarations.variable_t):
        if var.parent == somestruct:
            print("Found: " + str(var))

# Found: MyClass::classmem [variable]
for var in myclass.variables():
    if isinstance(var, declarations.variable_t):
        if var.parent == myclass:
            print("Found: " + str(var))

# Found: MyClass::classmem [variable]
for var in myclass.variables(recursive=False):
    if isinstance(var, declarations.variable_t):
        print("Found: " + str(var))

> On 26 Mar 2016, at 09:13, seshu yamajala <syamajala at gmail.com> wrote:
> 
> I'm trying to use pygccxml to auto-generate some C++. The problem I'm having is if I have a class like:
> 
> class MyClass {
>    struct SomeStruct {
>       int structmem;
>    };
> 
>    SomeStruct classmem;
> };
> 
> When I look at the variables of the MyClass declaration, its listing both structmem and classmem as members. Is there a way to tell that structmem is a member of SomeStruct and that SomeStruct was defined inside of MyClass?
> 
> _______________________________________________
> CastXML mailing list
> CastXML at public.kitware.com
> http://public.kitware.com/mailman/listinfo/castxml



More information about the CastXML mailing list