Yes, I know dynamic variables aren’t possible in c++. What I’m looking for is a workaround.
The idea is basically this. I’ve got a bunch of mathematical models together. They all have the exact same form, except for
- the mathematical definition of a few functions
- the needed constants: what they’re called, their number, and their values.
The values of the constants may change between experiments, but everything else is a property of the material model itself. The constant parameters of the model are given in the input file in the form
variable_A=2.0
The material models (including all functions) were previously made through generated code, generated from abstract mathematical expressions through Maple, with a nice GUI environment to boot. This is now sadly broken, for multiple reasons. As a result of some other work, the models are now all the exact same in code, except for where they differ in their formal description. Instead of having a whole separate program to generate this code (which is currently broken and what we used to have), I’m looking for a simpler alternative, since now I only need roughly 20 lines throughout a given file to differentiate one material from another. However, the users who would implement these models aren’t expected to have any knowledge of c++. So while I could simply ask them to go through the file and change things in various places, I was looking for something a little more user-friendly: so the user can just change a few lines in one place and define their model that way, without having to look through the rest of the code.
The functions to be defined by the user look like this.
double myfunction(double arg1, double arg2, vector arg3 ) \\just an example
{
...
...
double a=database.find_constant("a_const");
double b=database.find_constant("b_const");
return sqrt(a*3+pow(b,2)-a/b)+...; \\still example pseudocode
}
The top ellipses are fairly ugly (not bad code, just syntax heavy and irrelevant), and so I don’t want the end user to have to deal with them. My problem can probably be better described through examples:
My original plan was to use macros (yes, I know, the horror) at the top, so that this could become something like
#define A database.find_constant("a_const")
#define FUNCTION pow(A-2,3)
...
double myfunction(...)
{
...
return FUNCTION;
}
but then I found out that you can’t nest macros. Which in retrospect is obvious, but I digress. Then I realized that I could just change the #define A statement to make A a global variable. Then i hit myself for even considering global variables. Basically, all my attempts to stick this all in one place where the user doesn’t need much knowledge of c++ are ridiculous and/or horribly insecure.
Normally i hate asking such open ended questions, but I really don’t want to give myself headaches. I’ll only be working on this project for a short time, so designing a UI of some form and/or generating the code isn’t practical. I’m going for ‘proof of concept’ at the moment.
Is there a better way to do this than having them go through the model and change the needed lines in the functions directly?
EDIT: I didn’t write this whole project, nor was it originally intended to work in this way. The original tool we had for producing these files used Maple’s code generation, where the functions and variables were entered as Maple syntax in a nice GUI. Unfortunately the GUI doesn’t seem to work (consistently) anymore, and the code Maple returned stopped producing the right results once we switched versions: we’re not sure what the problem is exactly, and we’re just exploring other avenues right now. The new files are also much more similar than the previous ones (as a result of other work), which is part of the motivation for a new technique. I was hoping that I could get around code generation if I only needed a few lines to work together in a simple way. This isn’t a “fix it for me!” question, I’d just like to get a handle on what those other options might be. Yes I know it’s not a good situation to begin with, and if this was planned from the start it would not be done this way. But this setup will probably be temporary, and as I said is more of a proof of concept than anything else.
Hopefully the question is clearer now. My issue is specifically with grouping everything in one place in a simple to use manner. Thank you for your patience.
Something you can try that ‘acts’ like macros but isn’t, is to encapsulate the various function groups in anonymous structs:
Use a macro to access the functions in this struct:
#define DoStuff GeneralStuffand in your code access it:
If a user needs to change the code, he makes a copy of the struct, apply the changes, and
redefines the macro:
These would be the only changes needed, if I understand your question correctly.