This repository hosts my failed attempt at a perfect header-only
reflections system for OpenFOAM code while we wait for this
proposal
to make it into the C++ standard. It'll be a while before that happens but
since concepts are now in the language, it's not too far into the future.
Main objectives of this library:
- Provide an automated mechanism to build dictionary skeletons for OpenFOAM classes (And JSON endpoints). A skeleton represents a "standard" OpenFOAM dictionary (or a simple JSON schema) with required keywords (and sub-dicts) to construct an object of the target type.
- Provide an automated mechanism to generate UI widgets for OpenFOAM classes.
Use cases:
- Cheaply generate UIs for OpenFOAM applications (Both in terms of developer effort, and performance overhead).
- Natively generate skeletons (Both OpenFOAM dictionaries, and in JSON format) for use in Unit Testing and Ux generation.
Important
I discourage the use of reflections for any other purpose as they open a giant gate for all kinds of bugs. Think of all the bugs in Game Engines.
Reflections are done at compile-time with (near)zero runtime-cost through the refl-cpp library.
For a quick overview on how this effort started, the general goals and the current state; see
- A reflection system for MeshFreeFoam - Part 1.
- A reflection system for MeshFreeFoam - Part 2.
- A reflection system for MeshFreeFoam - Part 3.
There is an interactiveUI application to illustrate basic usage of the reflection system by providing a JSON endpoint for few dummy OpenFOAM classes. Also, you need C++20 for this to work (not a hard requirement but I like concepts).
- A recent version of OpenCFD OpenFOAM
- C++20 compiler (For the dummy classes and the JSON endpoint generator)
- Golang v1.22+ (For the sample TUI)
All of the requirements are contained in the associated Apptainer container.
To compile the sample application:
git clone https://github.com/FoamScience/openfoam-reflections
cd openfoam-reflections
# Needs OpenFOAM to be sourced
./build.sh # Works on Ubuntu with go installed
./applications/endpoint/endpoint
# in another terminal,
./TUI/tui
For the mechanism to work for your code, you have to follow specific methods:
- A few macros need to be called for your base and child models (See interactiveUI/baseModel as an example)
- Your
::New
function needs to have a dictionary as first argument. - In general, your types need to satisfy concept constraints in uiConcepts.H (Or the other way around, your call)
- If you wrap your class members in a
uiElement<...>
, descriptions, bounds and default values will be picked up by the UI generator. Although this is not fully implemented.
If you don't want to bloat your system but still would like to give this a try:
git clone https://github.com/FoamScience/openfoam-apptainer-packaging /tmp/of_tainers
git clone https://github.com/FoamScience/openfoam-reflections
cd openfoam-reflections
ansible-playbook /tmp/of_tainers/build.yaml --extra-vars="original_dir=$PWD" --extra-vars="@build/config.yaml"
# In one terminal (it's fine if there are some errors)
apptainer run containers/projects/reflections.sif "/opt/openfoam-reflections/endpoint/endpoint"
# In another terminal
apptainer run containers/projects/reflections.sif "/opt/openfoam-reflections/TUI/tui"