Skip to content

Commit fcd4f9e

Browse files
committed
Add a bunch of simple examples to the doc
1 parent f8d2358 commit fcd4f9e

File tree

10 files changed

+206
-0
lines changed

10 files changed

+206
-0
lines changed

docs/examples.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
========
2+
Examples
3+
========
4+
5+
First examples
6+
--------------
7+
8+
.. toctree::
9+
:maxdepth: 1
10+
11+
examples/first/example.rst
12+
examples/variables/example.rst
13+
examples/caching/example.rst

docs/examples/caching/example.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2014-2015 Insight Software Consortium.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// See http://www.boost.org/LICENSE_1_0.txt
4+
5+
namespace ns{
6+
int a = 1;
7+
}

docs/examples/caching/example.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2014-2015 Insight Software Consortium.
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# See http://www.boost.org/LICENSE_1_0.txt
4+
5+
from pygccxml import utils
6+
from pygccxml import declarations
7+
from pygccxml import parser
8+
9+
# Find out the c++ parser
10+
generator_path, generator_name = utils.find_cpp_parser()
11+
12+
# Configure the xml generator
13+
xml_generator_config = parser.xml_generator_configuration_t(
14+
xml_generator_path=generator_path,
15+
xml_generator=generator_name)
16+
17+
# The c++ file we want to parse
18+
filename = "example.hpp"
19+
20+
file_config = parser.file_configuration_t(
21+
data=filename,
22+
content_type=parser.CONTENT_TYPE.CACHED_SOURCE_FILE)
23+
24+
project_reader = parser.project_reader_t(xml_generator_config)
25+
decls = project_reader.read_files(
26+
[file_config],
27+
compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE)
28+
29+
global_namespace = declarations.get_global_namespace(decls)
30+
31+
value = global_namespace.namespace("ns")
32+
print("My name is: " + value.name)

docs/examples/caching/example.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=======
2+
Caching
3+
=======
4+
5+
This example shows how to use caching. This can be useful
6+
for big projects where you don't want the c++ to be parsed
7+
again and again.
8+
9+
Let's consider the following c++ file:
10+
11+
.. literalinclude:: example.hpp
12+
:language: c++
13+
14+
To enable caching, you can use the following code:
15+
16+
.. literalinclude:: example.py
17+
:language: python
18+
19+
The first time you run this example, the c++ file will be read and a xml
20+
file will be generated:
21+
22+
INFO Creating xml file "example.hpp.xml" from source file "example.hpp" ...
23+
INFO Parsing xml file "example.hpp.xml" ...
24+
My name is: ns
25+
26+
The second time you run the example the xml file will not be regenerated:
27+
28+
INFO Parsing xml file "example.hpp.xml" ...
29+
My name is: ns
30+
31+
Of course the performance gain will be small for this example,
32+
but can be intersting for bigger projects.

docs/examples/parsing/example.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2014-2015 Insight Software Consortium.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// See http://www.boost.org/LICENSE_1_0.txt
4+
5+
namespace ns{
6+
int a = 1;
7+
}

docs/examples/parsing/example.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2014-2015 Insight Software Consortium.
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# See http://www.boost.org/LICENSE_1_0.txt
4+
5+
from pygccxml import utils
6+
from pygccxml import declarations
7+
from pygccxml import parser
8+
9+
# Find the location of the xml generator (castxml or gccxml)
10+
generator_path, generator_name = utils.find_cpp_parser()
11+
12+
# Configure the xml generator
13+
xml_generator_config = parser.xml_generator_configuration_t(
14+
xml_generator_path=generator_path,
15+
xml_generator=generator_name)
16+
17+
# The c++ file we want to parse
18+
filename = "example.hpp"
19+
20+
# Parse the c++ file
21+
decls = parser.parse([filename], xml_generator_config)
22+
23+
# Get access to the global namespace
24+
global_namespace = declarations.get_global_namespace(decls)
25+
26+
# Get access to the 'ns' namespace
27+
ns = global_namespace.namespace("ns")

docs/examples/parsing/example.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
==================
2+
Parsing a c++ file
3+
==================
4+
5+
This example shows how to setup pygccxml to parse a c++ file, and how
6+
to access the declaration tree.
7+
8+
Let's consider the following c++ file (example.cpp):
9+
10+
.. literalinclude:: example.hpp
11+
:language: c++
12+
13+
The following code will show you how to create a configuration for
14+
the xml generator (an external tool, either castxml or gccxml),
15+
and how to parse the c++ file:
16+
17+
.. literalinclude:: example.py
18+
:language: python

docs/examples/variables/example.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2014-2015 Insight Software Consortium.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// See http://www.boost.org/LICENSE_1_0.txt
4+
5+
namespace ns{
6+
int a = 1;
7+
int b = 2;
8+
double c = 3.0;
9+
}

docs/examples/variables/example.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2014-2015 Insight Software Consortium.
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# See http://www.boost.org/LICENSE_1_0.txt
4+
5+
from pygccxml import utils
6+
from pygccxml import declarations
7+
from pygccxml import parser
8+
9+
# Find out the c++ parser
10+
generator_path, generator_name = utils.find_cpp_parser()
11+
12+
# Configure the xml generator
13+
xml_generator_config = parser.xml_generator_configuration_t(
14+
xml_generator_path=generator_path,
15+
xml_generator=generator_name)
16+
17+
# The c++ file we want to parse
18+
filename = "variables.hpp"
19+
20+
decls = parser.parse([filename], xml_generator_config)
21+
global_namespace = declarations.get_global_namespace(decls)
22+
ns = global_namespace.namespace("ns")
23+
24+
# The variables() method will return a list of variables.
25+
# We know that he c variable is the third one in the list:
26+
c = ns.variables()[2]
27+
print("My name is: " + c.name)
28+
print("My type is: " + str(c.type))
29+
print("My value is: " + c.value)
30+
31+
# Of course you can also loop over the list and look for the right name
32+
for var in ns.variables():
33+
if var.name == "c":
34+
print("My name is: " + var.name)
35+
print("My type is: " + str(var.type))
36+
print("My value is: " + var.value)
37+
38+
# One way to get a variable is to use the variable() method and
39+
# a lambda function. This is the most flexible way as you can implement
40+
# your own lambda function to filter out variables following your
41+
# specific criteria.
42+
c = ns.variable(lambda v: v.name == "c")
43+
print("My name is: " + c.name)
44+
print("My type is: " + str(c.type))
45+
print("My value is: " + c.value)

docs/examples/variables/example.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=========
2+
Variables
3+
=========
4+
5+
This example shows how to find variables and find information about them.
6+
7+
Let's consider the following c++ file:
8+
9+
.. literalinclude:: example.hpp
10+
:language: c++
11+
12+
The following code can be use to find the variable named "c" using different
13+
strategies, and to print information about it:
14+
15+
.. literalinclude:: example.py
16+
:language: python

0 commit comments

Comments
 (0)