C++ Code Generator
Simple and straightforward code generator for creating C++ code. It also could be used for generating code in any programming language. Written in Python, works both with Python 2 and 3
Every C++ element could render its current state to a string that could be evaluated as a legal C++ construction. Some elements could be rendered to a pair of representations (C++ classes and functions declaration and implementation)
Special thanks
Thanks to Eric Reynolds, the idea of this project mainly based on his article published on http://www.codeproject.com/Articles/571645/Really-simple-Cplusplus-code-generation-in-Python
However, this solution has been both simplified and extended compared to the initial idea.
Usage examples
Generate C++ code from Python code
Creating variables
Python code
cpp = CodeFile('example.cpp')
cpp('int i = 0;')
x_variable = CppVariable(name='x', type='int const&', is_static=True, is_constexpr=True, initialization_value='42')
x_variable.render_to_string(cpp)
name_variable = CppVariable(name='name', type='char*', is_extern=True)
name_variable.render_to_string(cpp)
Generated C++ code
int i = 0;
static constexpr int const& x = 42;
extern char* name;
Creating functions
Python code
def handle_to_factorial(self, cpp):
cpp('return n < 1 ? 1 : (n * factorial(n - 1));')
cpp = CodeFile('example.cpp')
factorial_function = CppFunction(name='factorial',
ret_type='int',
is_constexpr=True,
implementation_handle=handle_to_factorial,
documentation='/// Calculates and returns the factorial of \p n.')
factorial_function.add_argument('int n')
factorial_function.render_to_string(cpp)
Generated C++ code
/// Calculates and returns the factorial of \p n.
constexpr int factorial(int n)
{
return n <= 1 ? 1 : (n * factorial(n - 1));
}
Creating classes and structures
Python code
cpp = CppFile('example.cpp')
with cpp.block('class A', ';'):
cpp.label('public:')
cpp('int m_classMember1;')
cpp('double m_classMember2;')
Generated C++ code
class A
{
public:
int m_classMember1;
double m_classMember2;
};
Rendering CppClass objects to C++ declaration and implementation
Python code
cpp_class = CppClass(name = 'MyClass', is_struct = True)
cpp_class.add_variable(CppVariable(name = "m_var",
type = 'size_t',
is_static = True,
is_const = True,
initialization_value = 255))
Generated C++ declaration
struct MyClass
{
static const size_t m_var;
}
Generated C++ implementation
const size_t MyClass::m_var = 255;
Module cpp_generator.py highly depends on parent code_generator.py, as it uses
code generating and formatting primitives implemented there.
The main object referenced from code_generator.py is CppFile,
which is passed as a parameter to render_to_string(cpp) Python method
It could also be used for composing more complicated C++ code,
that does not supported by cpp_generator
Class ANSICodeStyle is responsible for code formatting. Re-implement it if you wish to apply any other formatting style.
It support:
- functional calls:
cpp('int a = 10;')
withsemantic:
with cpp.block('class MyClass', ';')
class_definition(cpp)
- append code to the last string without EOL:
cpp.append(', p = NULL);')
- empty lines:
cpp.newline(2)
Maintainers
Executing unit tests
The following command will execute the unit tests.
python -m unittest cpp_generator_tests.py
Updating unit tests fixed data
After changing a unit test the fixed data needs to be updated to successfully pass the unit tests.
python -c 'from test_cpp_generator import generate_reference_code; generate_reference_code()'
After executing that command, the fixed data under tests/test_assets will be updated and will need to be committed to git.
Metadata
Release files for code-generation 2.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| code_generation-2.3.0.tar.gz | 17.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| code_generation-2.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 38.9 kB
Release files / code_generation-2.3.0.tar.gz
| Download URL | code_generation-2.3.0.tar.gz |
|---|---|
| Size | 17.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8089b05c5d376df355c797e52b0afe9ec815612c77db09ea6b812588b684cb1e
|
|
BLAKE2b-256 checksum How to use checksums |
ffde1f0b9f3dc02b7382ac1202720c675d57eed5bca7d34b3552b3ceb687bc9a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.1
|
Release files / code_generation-2.3.0-py3-none-any.whl
| Download URL | code_generation-2.3.0-py3-none-any.whl |
|---|---|
| Size | 21.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
74b0b422de912c64d7f687c629e20290fe7f70b82831248b971a977bc2b49159
|
|
BLAKE2b-256 checksum How to use checksums |
95f388a97665ddc2b949be5b0a49f21245033100feead75ad5c2b17560561dcd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.1
|
Security for code-generation 2.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.