double UpdateLVars(std::string name) {
float value = 0;
int id = check_named_variable(name.c_str());
value = get_named_variable_value(id);
return value;
}
Peter87: This does not work. I get an compile error when using LIST_OF_MEMBERS in the ReadVars_737 function. Another issue is that I need to have a clean data structure definition.
Using X(name) in the structure is not accepted in other parts of my code that uses the structure.
I guess I have to use my original code as no working solutions have been found.
This does not work. I get an compile error when using LIST_OF_MEMBERS in the ReadVars_737 function.
The code I posted compiles. Without the code, or at least the error messages, I can't say why it doesn't work for you.
Another issue is that I need to have a clean data structure definition.
Using X(name) in the structure is not accepted in other parts of my code that uses the structure.
In that case I'm afraid I see no other way than to just list all the members.
There are plans to add static reflection to C++. I think that would allow you to loop over all the members of a struct (at compile time) and generate the initialization code the way you want. Unfortunately that feature is still several years away.
There is an implementation of clang++ 14.0 that has reflection (it does the Reflection TS), IIRC one has to build the code with that feature turned on.
OK, Thanks for all feeddback. I will just use my code as other solution seems to complicated or not possiblein respect to other parts of my code.
My code works fine, but as a hobby programmer it looks non-professional to have to write several hundred more or less similar code lines when the only difference between all code lines in the ReadLVars function is the structure member name.
Using an array to store the values and having the required individual names as refs to the appropriate array element. The variables then be either accessed via index onto the array or by their name.
I would consider an enum of your 200+ names.
you can use that to access the particular array location, or in a map, or various other ways.
the C way:
enum things{red,green,blue,maxthings};
double many[maxthings]; //you can typedef this if you want or put into struct
...
cin >> many[blue]; //example usage
update(red);// ... etc
note that this bypasses the "" string variable name idea entirely. You asked for efficient, and if its in the code, maybe you can use the variable name instead of a string (faster comparison)? If you must do a string, this will not work: enums are only ints.
if you end up using your 200 variable code anyway:
- you can write a little program on the side to produce the code you need... Ive had to do this to generate larger lookup tables, the code produces cut and paste c++ that goes in the real program.
- and you can use smarter editors, like notepad++, that let you edit 200 lines at once, so if you pasted a column of the variable names, you can then make them all = 0; or whatever with 3 keystrokes, or a macro that you hold down.
This function tells the client software on the other end that there has been changes to the structure, which is then read by the client. The structure in the client is as the structure in my example. That is why I need this specific structure layout.
}
seeplus:
What is the value returned from check_named_variable() ? Is this a simple int in the range 0 to N - 1?
Does SimConnect_SetClientData() simply take a pointer to a "data" buffer and the size of that buffer? If so, you could write a simple function that serializes the std::unordered_map into a sequence of bytes. On the other side, you used a similar function that de-serializes the retrieved bytes into a std::unordered_map again.
Another option would be to "simulate" a key-to-double map like this:
Parameters
hSimConnect
[in] Handle to a SimConnect object.
ClientDataID
[in] Specifies the ID of the client data area.
DefineID
[in] Specifies the ID of the client defined client data definition.
Flags
[in] Null, or one or more of the following flags.
Flag Description
SIMCONNECT_CLIENT_DATA_SET_FLAG_TAGGED The data to be set is being sent in tagged format. If this flag is not set then the entire client data area should be replaced with new data. Refer to the pDataSet parameter and SimConnect_RequestClientData for more details on the tagged format.
dwReserved
[in] Reserved for future use. Set to zero.
cbUnitSize
[in] Specifies the size of the data set in bytes. The server will check that this size matches exactly the size of the data definition provided in the DefineID parameter. An exception will be returned if this is not the case.
pDataSet
[in] Pointer to the data that is to be written. If the data is not in tagged format, this should point to the block of client data. If the data is in tagged format this should point to the first tag name (datumID), which is always four bytes long, which should be followed by the data itself. Any number of tag name/value pairs can be specified this way, the server will use the cbUnitSize parameter to determine how much data has been sent.