Keeping track of variable types in a map

Started by
10 comments, last by Endar 19 years, 4 months ago
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
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
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.
Or you could just have a map of VARIANTs...
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by iMalc
Or 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?
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.
std::map<std::string, boost::any>

Enigma
Quote:Original post by fallenang3l
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.

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]).
Quote:Original post by Enigma
std::map<std::string, boost::any>

Enigma

The cleanest solution. I totally forgot about boost...
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.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
You can find boost here.
Some words from Microsoft® on boost.

Regards,
Pat.

This topic is closed to new replies.

Advertisement