Code generation using Phoenix
Phoenix2 provides a code generation framework that allows you to generate code, allowing you to create classes, projects, and other code structures based on templates. This framework is designed to streamline the development process by automating repetitive coding tasks, such as creating a new project, generating data format classes, or hdf5 data classes.
Generating C++ classes
Phoenix2 provides functionality for generating classes from configuration files, enabling the automatic creation of structured code with predefined interfaces and behaviours. This approach reduces boilerplate code and improves development efficiency by standardising common patterns across the codebase.
The pdata extension is used to indicate that a given file should be interpreted as a class generation specification. This allows Phoenix2 to identify and process files intended for code generation.
The main entry point for this mechanism is the PhoenixFileGenerator, which reads configuration files and generates the corresponding source code based on predefined templates.
For example, when creating a Shadok class, several generation strategies are available. Developers can generate empty skeleton files to be completed manually, create a minimal class structure including header and source files with basic functionality, or generate a more complete setup that also includes associated test files.
Source mode
PhoenixFileGenerator features a source mode allowing to generate empty source files for a given class. For the Shadok class, the following command generates empty shadok.h and shadok.cpp files:
Class Mode
PhoenixFileGenerator provides a class generation mode that allows automatic creation of C++ classes, including both header and source files. In this mode, the generator produces a standard class structure with basic boilerplate such as constructors and destructors.
A class can be generated either by directly specifying its name or by providing a configuration file that defines its full structure and parameters.
Additionally, the generator can optionally produce unit tests for the generated class, including tests for common base functions such as getters and setters.
Generating HDF5 data classes
Phoenix2 also provides an HDF5 data class generation mode, allowing developers to create classes that facilitate reading and writing data in the HDF5 format. This is done in PhoenixHDF5 which is a specialized tool for generating HDF5 data classes based on configuration files. It provides an executables that reads a configuration file and generates the corresponding C++ class, including methods for reading and writing HDF5 data.
Generating Projects
PhoenixFileGenerator also provides a project generation mode, enabling developers to scaffold new C++ projects from predefined templates. This feature simplifies the initial setup of Phoenix-based projects by automatically generating the required directory structure, build system configuration, and optional initial source code.
Base C++ project
A minimal C++ project can be generated using the following command:
phoenix_filegenerator project --description MyProjectDescriptionFile --type SimpleCpp
It is also possible to directly include a class definition during project creation:
phoenix_filegenerator project --description MyProjectDescriptionFile --type SimpleCpp --config Shadok.pdata
This generates a project based on the SimpleCpp template, including a standard directory layout, a CMake-based build system, and optional initial source files if a class configuration is provided.
Nanobind project
PhoenixFileGenerator also supports generating projects with Python bindings through Nanobind, a lightweight header-only library for exposing C++ code to Python.
This enables seamless integration between C++ core logic and Python interfaces.
phoenix_filegenerator project --type SimpleNanobind --description MyProjectDescriptionFile --config Shadok.pdata
This command generates a project structure similar to SimpleCpp, but with additional components required to build and expose Python bindings using Nanobind.