I am evaluating the use of code generation for my flight simulation project. More specifically there is a requirement to allow "the average engineer" (no offence I am one myself) to define the differential equations that describe the dynamic system in a more natural syntax than C++ provides. The idea is to devise a abstract descriptor language that can be easily understood and edited to generate C++ code from. This descriptor is supplied by the modelling engineer and used by the ones implementing and maintaining the simulation environment to generate code.
I've got something like this in mind:
model Aircraft has
state x1, x2;
state x3;
input double : u;
input bool : flag1, flag2;
algebraic double : x1x2;
model Engine : tw1, tw2;
model Gear : gear;
model ISA : isa;
trim routine HorizontalFight;
trim routine OnGround, General;
constant double : c1, c2;
constant int : ci1;
begin differential equations
x1' = x1 + 2.*x2;
x2' = x2 + x1x2;
begin algebraic equations
x1x2 = x1*x2 + x1';
end model
It is important to retain the flexibility of the C language thus the descriptor language is meant to only define certain parts of the definition and implementation of the model class. This way one engineer provides the model in from of the descriptor language as exemplified above and the maintenance engineer will add all the code to read parameters from files, start/stop/pause the execution of the simulation and how a concrete object gets instantiated.
My first though is to either generate two files from the descriptor file: one .h file containing declarations and one .cpp file containing the implementation of certain functions. These then need to be #included at appropriate places
[File Aircarft.h]
class Aircraft
{
public:
void Aircraft(..); // hand-written constructor
void ReadParameters(string &file_name); // hand-written
private:
/* more hand wirtten boiler-plate code */
/* generate declarations follow */
#include "Aircraft.generated.decl"
};
[File Aircraft.cpp]
Aircraft::Aircraft(..) { /* hand-written constructor implementation */ }
/* more hand-written implementation code */
/* generated implementation code follows */
#include "Aircraft.generated.impl"
Any thoughts or suggestions?
EDIT1:
I would like to clarify that there exists a framework to express the differential equations and the (real-time) simulation of a dynamic sytem. The problem is that most of the engineers with domain knowledge are very hesitant regarding the use of C++. Indeed we would like to provide a easer way for them to contribute their part (the mathematical formulation) while retaining the flexibility of C++.
There is of course the possibility to use the MEX compiler to generate C-code from MATLAB/Simulink models but we would like to avoid the associated license fees.
Refactoring the whole thing to use a general scripting language is probably out of scope in terms of workforce available. Furthermore this would -- as far as I can grasp it now and on the fly -- require the engineers to learn a scripting language, too. I am not yet convinced that this would be easier than the syntax exemplified above.
EDIT2: Accepted answer
I accept the answer given by Richard Harrison due to sharing his exeprience with custom code-generators. I will also consider the hints regarding scripting interfaces although I am afraind I will not find the time to implement all this as part of my thesis. Still these pointers might help to convince my coworkers to consider a more coherent class heriarchy. Thanks a lot!