|
|
Home » U++ Library support » U++ Core » Dynamic Variable Names
|
Re: Dynamic Variable Names [message #13545 is a reply to message #13543] |
Mon, 14 January 2008 17:09   |
mr_ped
Messages: 826 Registered: November 2005 Location: Czech Republic - Praha
|
Experienced Contributor |
|
|
As the C++ is compiled language with sort-of-strong type checking, you can't have fully dynamic variable names.
Yet there's nothing preventing you from making an interpreter wrapper for thing like this.
Basically a standard map container with String index is probably the simplest way to do this. Check
http://www.ultimatepp.org/srcdoc$Core$Tutorial$en-us.html
paragraph 10. VectorMap for basic info.
The code will look something like this:
//map of values
VectorMap<String, Value> m;
//reading variables
for ...reading loop... {
String var_name = read...
Value var_value = read...
m.Add( var_name, var_value );
}
//using variable
Value x = m.Get("the dynamic variable name");
//writing variables
for(int i = 0; i < m.GetCount(); i++) {
write... m.GetKey(i)
write... " = "
write... m[i]
write...end line or whatever you wish;
}
I suggest to encapsulate+hide+wrap this into class, still 'echo $real_var' will need one function call, so it will look like 'echo values.GetValue("real_var")'
(if you mind the verbosity of such line, you can easily make instance "v" of that class, and use () operator function to return value, so it will be 'echo v("real_var")' ... but it looks too cryptic to me. And AFAIK you can't get it better in C++ anyway.
update2:
If you wonder why "Value" for values... because UPP::Value can store different kinds of values, still you should check if it covers all your needs. (It pretty much should, because almost anything can be "String" for a while and String can be stored in Value too ... but it makes more sense to have native Integers and Dates in cases where the data fits into them)
Actually class UPP::Value is nice example of how far you can get with sort-of-dynamic types in C++ and how well the C++ static compilation can be bend in this direction.
I'm not sure what for you need the dynamic variables names anyway, I see you tried to describe something about config files, but I'm missing "the" reason in it.
[Updated on: Mon, 14 January 2008 17:18] Report message to a moderator
|
|
|
|
|
|
Goto Forum:
Current Time: Mon Apr 28 04:46:56 CEST 2025
Total time taken to generate the page: 0.01001 seconds
|
|
|