Package: code-generation
C++ Code GeneratorSimple 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 3Every 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 thanksThanks to Eric Reynolds, the idea of this project mainly based on his article published onhttp://www.codeproject.com/Articles/571645/Really-simple-Cplusplus-code-generation-in-PythonHowever, this solution has been both simplified and extended compared to the initial idea.Usage examplesGenerate C++ code from Python codeCreating variablesPython codecpp=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++ codeinti=0;staticconstexprintconst&x=42;externchar*name;Creating functionsPython codedefhandle_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.constexprintfactorial(intn){returnn<=1?1:(n*factorial(n-1));}Creating classes and structuresPython codecpp=CppFile('example.cpp')withcpp.block('class A',';'):cpp.label('public:')cpp('int m_classMember1;')cpp('double m_classMember2;')Generated C++ codeclassA{public:intm_classMember1;doublem_classMember2;};RenderingCppClassobjects to C++ declaration and implementationPython codecpp_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++ declarationstructMyClass{staticconstsize_tm_var;}Generated C++ implementationconstsize_tMyClass::m_var=255;Modulecpp_generator.pyhighly depends on parentcode_generator.py, as it uses
code generating and formatting primitives implemented there.The main object referenced fromcode_generator.pyisCppFile,
which is passed as a parameter torender_to_string(cpp)Python methodIt could also be used for composing more complicated C++ code,
that does not supported bycpp_generatorClassANSICodeStyleis 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:withcpp.block('class MyClass',';')class_definition(cpp)append code to the last string without EOL:cpp.append(', p = NULL);')empty lines:cpp.newline(2)MaintainersExecuting unit testsThe following command will execute the unit tests.python-munittestcpp_generator_tests.pyUpdating unit tests fixed dataAfter 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 undertests/test_assetswill be updated and will need to be committed to git.