Skip to content

Commit b60f4b9

Browse files
committed
Add an example for compound types
1 parent ae577d3 commit b60f4b9

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

docs/examples.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ First examples
1717
examples/functions/example.rst
1818
examples/nested-types/example.rst
1919
examples/artificial/example.rst
20+
examples/compound/example.rst
2021
examples/templates/example.rst
2122

2223
Advanced examples

docs/examples/compound/example.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2014-2017 Insight Software Consortium.
2+
// Copyright 2004-2009 Roman Yakovenko.
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// See http://www.boost.org/LICENSE_1_0.txt
5+
6+
int const c1 = 0;
7+
const int c2 = 0;
8+
9+
volatile const int cv1 = 0;
10+
const volatile int cv2 = 0;
11+
12+
const int * const cptr1 = 0;

docs/examples/compound/example.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright 2014-2017 Insight Software Consortium.
2+
# Copyright 2004-2009 Roman Yakovenko.
3+
# Distributed under the Boost Software License, Version 1.0.
4+
# See http://www.boost.org/LICENSE_1_0.txt
5+
6+
from pygccxml import utils
7+
from pygccxml import declarations
8+
from pygccxml import parser
9+
10+
import os
11+
import sys
12+
import warnings
13+
warnings.simplefilter("error", Warning)
14+
# Find out the file location within the sources tree
15+
this_module_dir_path = os.path.abspath(
16+
os.path.dirname(sys.modules[__name__].__file__))
17+
18+
# Find out the c++ parser
19+
generator_path, generator_name = utils.find_xml_generator()
20+
21+
# Configure the xml generator
22+
xml_generator_config = parser.xml_generator_configuration_t(
23+
xml_generator_path=generator_path,
24+
xml_generator=generator_name)
25+
26+
# The c++ file we want to parse
27+
filename = "example.hpp"
28+
filename = this_module_dir_path + "/" + filename
29+
30+
decls = parser.parse([filename], xml_generator_config)
31+
global_namespace = declarations.get_global_namespace(decls)
32+
33+
c1 = global_namespace.variable("c1")
34+
print(str(c1), type(c1))
35+
# > 'c1 [variable]', <class 'pygccxml.declarations.variable.variable_t'>
36+
37+
print(str(c1.decl_type), type(c1.decl_type))
38+
# > 'int const', <class 'pygccxml.declarations.cpptypes.const_t'>
39+
40+
base = declarations.remove_const(c1.decl_type)
41+
print(str(base), type(base))
42+
# > 'int', <class 'pygccxml.declarations.cpptypes.int_t'>
43+
44+
c2 = global_namespace.variable("c2")
45+
print(str(c2.decl_type), type(c2.decl_type))
46+
# > 'int const', <class 'pygccxml.declarations.cpptypes.const_t'>
47+
# Even if the declaration was defined as 'const int', pygccxml will always
48+
# output the const qualifier (and some other qualifiers) on the right hand
49+
# side (by convention).
50+
51+
cv1 = global_namespace.variable("cv1")
52+
print(str(cv1.decl_type), type(cv1.decl_type))
53+
# > 'int const volatile', <class 'pygccxml.declarations.cpptypes.volatile_t'>
54+
55+
# Remove one level:
56+
base = declarations.remove_volatile(cv1.decl_type)
57+
print(str(base), type(base))
58+
# > 'int const', <class 'pygccxml.declarations.cpptypes.const_t'>
59+
60+
# Remove the second level:
61+
base = declarations.remove_const(base)
62+
print(str(base), type(base))
63+
# > 'int', <class 'pygccxml.declarations.cpptypes.int_t'>
64+
65+
# We can also directly do this in one step:
66+
base = declarations.remove_cv(cv1.decl_type)
67+
print(str(base), type(base))
68+
# > 'int', <class 'pygccxml.declarations.cpptypes.int_t'>
69+
70+
# As for consts, the const and volatile are on the right hand side
71+
# (by convention), and always in the same order
72+
cv2 = global_namespace.variable("cv2")
73+
print(str(cv2.decl_type), type(cv2.decl_type))
74+
# > 'int const volatile', <class 'pygccxml.declarations.cpptypes.volatile_t'>
75+
76+
# And a last example with a pointer_t:
77+
cptr1 = global_namespace.variable("cptr1")
78+
print(str(cptr1.decl_type), type(cptr1.decl_type))
79+
# > 'int const * const', <class 'pygccxml.declarations.cpptypes.const_t'>)
80+
81+
base = declarations.remove_const(cptr1.decl_type)
82+
print(str(base), type(base))
83+
# > 'int const *', <class 'pygccxml.declarations.cpptypes.pointer_t'>
84+
85+
base = declarations.remove_pointer(base)
86+
print(str(base), type(base))
87+
# > 'int const', <class 'pygccxml.declarations.cpptypes.const_t'>)
88+
89+
base = declarations.remove_const(base)
90+
print(str(base), type(base))
91+
# > 'int', <class 'pygccxml.declarations.cpptypes.int_t'>)

docs/examples/compound/example.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
==============
2+
Compound types
3+
==============
4+
5+
A type is a compound_t type (in pygccxml) if it is one of the following:
6+
*volatile_t*, *restrict_t*, *const_t*, *pointer_t*, *reference_t*,
7+
*elaborated_t*, *array_t* or *member_variable_type_t*.
8+
9+
The exact c++ definition of compound types embraces more types, but for different
10+
reasons (mostly legacy), the definition in pygccxml is slightly different.
11+
12+
Let's consider the following c++ file:
13+
14+
.. literalinclude:: example.hpp
15+
:language: c++
16+
:lines: 5-
17+
18+
The following code will show what to expect from compound types, how they are
19+
chained, and how their order is defined in pygccxml.
20+
21+
.. literalinclude:: example.py
22+
:language: python
23+
:lines: 6,7,8,17-27,29-

0 commit comments

Comments
 (0)