Keeping track of variable types in a map

This topic is 5066 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm messing around with creating a scripting language and I've seen a good tut on gamedev and worked through just about all of it. Now, the tut I saw used integers as variables (eg. variable[0] or variable[6] ), and I thought that a good way to use actual strings as variable is using a map<char*, int>, but I've run into a little problem because I realised, what if I want to have a float a string as data? Then I came up with having a map<char*, void*>, but this comes with a problem of having to keep track of what kind of variable each one is. That is, unless a map can have a key with two values corresponding to that key, but so far I don't think it can. Can anyone think of a better idea that having two maps? (one for <char*,void*> other for <char*,char*>, the second char* representing "float" or "int". Appreciated :D

Share on other sites
If I understood you correctly, you want to store arbitary variable types in an map and keep track of their type, right?
Instead of storing the variables directly as values, why not wrap them up in struct that also contains the type?

struct ScriptVariable {    enum eVariableType {        SVT_INT,        SVT_FLOAT,        SVT_STRING    };    eVariableType typeId;    std::string   strValue;    float         floatValue;    int           intValue;    ScriptVariable() : typeId(SVT_INT),intValue(0), floatValue(0)    {}};std::map<const char*, ScriptVariable> variableMap;

Using such a construct you can also easily mix types by converting them as required, e.g. in a script '1' * 1.5 becomes
1.5 with the result type set to float after converting '1' from string to float first, because there is no way to multiply strings.

I hope this helps you,
Pat.

Share on other sites
Or you could just have a map of VARIANTs...

Share on other sites
Quote:
 Original post by iMalcOr you could just have a map of VARIANTs...

It's been a while since I used that - isn't that a COM-centric data type?

Share on other sites
I don't think you're supposed to use a raw pointer as a key in a map because of some sorting issues (maybe somebody can explain why cause I'm not sure); however it is safe to use std::string instead.

Share on other sites
std::map<std::string, boost::any>

Enigma

Share on other sites
Quote:
 Original post by fallenang3lI don't think you're supposed to use a raw pointer as a key in a map because of some sorting issues (maybe somebody can explain why cause I'm not sure); however it is safe to use std::string instead.

Actually you are right. String compares are best done using actual strings, not pointers [smile]. For a scripting language I'd suggest converting all keys to lower-case before inserting and querying (case sensitive identifiers in scripts are quite annoying, IMHO[wink]).

Share on other sites
Quote:
 Original post by Enigmastd::mapEnigma

The cleanest solution. I totally forgot about boost...

Share on other sites
Thanks everyone :D

I'll probably be going with a variation of darookie's idea, but I hear some people talking about boost.

What is it? Because I've heard a couple of people talking about it, but I haven't had time to go and look for info about it.

Share on other sites
You can find boost here.
Some words from Microsoft® on boost.

Regards,
Pat.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 11
• 18
• 14
• 9
• 9
• Forum Statistics

• Total Topics
632929
• Total Posts
3009281
• Who's Online (See full list)

There are no registered users currently online

×