Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Dynamic Variable Names
Dynamic Variable Names [message #13543] Mon, 14 January 2008 16:33 Go to next message
gonzofish is currently offline  gonzofish
Messages: 9
Registered: January 2008
Location: USA
Promising Member

Is there any facility in U++ that makes it possible to have dynamic variable names and variable types?

I'm parsing a config file and it'd be nice to have the specified config variables be used in the parser. I know in scripting languages, such as PHP, that it is possible to have:

$var = "real_var";
$$var = "foo and bar";
echo $real_var;

and the output would be "foo and bar".

Thanks.
Re: Dynamic Variable Names [message #13545 is a reply to message #13543] Mon, 14 January 2008 17:09 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 825
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 Very Happy 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

Re: Dynamic Variable Names [message #13546 is a reply to message #13545] Mon, 14 January 2008 17:25 Go to previous messageGo to next message
waxblood is currently offline  waxblood
Messages: 95
Registered: January 2007
Member
If it is an interpreter you want you should look at u++ scripting language
http://www.ultimatepp.org/srcdoc$Esc$Esc$en-us.html

Re: Dynamic Variable Names [message #13550 is a reply to message #13543] Mon, 14 January 2008 18:49 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 697
Registered: December 2005
Location: Budapest, Hungary
Contributor
Check About storing configuration part of the documentation. Read the "1) Text configuration" section.

Readig a text configuration file is very simple:
VectorMap<String, String> cfg = LoadIniFile("myapp.cfg");
String recentdir = cfg.Get("RECENTDIR", Null);
int id = ScanInt(cfg.Get("ID", Null));
Re: Dynamic Variable Names [message #13552 is a reply to message #13543] Mon, 14 January 2008 19:05 Go to previous message
gonzofish is currently offline  gonzofish
Messages: 9
Registered: January 2008
Location: USA
Promising Member

Thank you all very much. All replies are helpful to what I'm doing.
Previous Topic: Rect_ operators overloading suggestion
Next Topic: logs in release builds (801.r97, SVN rev 90)
Goto Forum:
  


Current Time: Fri Apr 26 06:42:58 CEST 2024

Total time taken to generate the page: 0.07617 seconds