class Window
{
public:
...
void setValue(string value);
string getValue(void);
private:
string mValue; // every windows value
};
mValue can for example be the text in a inputbox, the value of a checkbox or the marked line in a listbox. That does for example mean that the value of a checkbox is either "true" or "false", and an example showing how ugly this is could look like this:
bool soundOn(void)
{
if(mSoundCheckBox.getValue() == "true")
return true;
else if(mSoundCheckBox.getValue() == "false")
return false;
}
Maybe even uglier than this is the way I get the int value from an InputBox;
int platformX = atoi(mInputX.c_str());
So I'm looking for a better way to do this and I was quite happy yesterday when I came up with the idea of using templates. The idea is to store the value as a generic type so it can be whatever it should be. For an inputbox it could be a float or int or string and for a checkbox a bool and for a slidebar int or float. That would allow me to do this instead of the above;
bool soundOn(void)
{
return mSoundCheckBox.getValue(); // mValue is declared as a bool
}
Some code showing how I think;
template<class T>
class Window
{
public:
...
void setValue(T value);
T getValue(void);
private:
T mValue; // every windows value
};
Creation of a InputBox:
InputBox<int> inputBox = new InputBox(mWindowHandler, 500, 500, 100, 20);
It feels like a smart way to do it but ofcourse I encounter problems.. To start with the vector inside WindowHandler doesn't seem able to do what I want:
class WindowHandler
{
...
private:
template<class T>
vector<Window<T>> mWindowList; // compiler crashes
}
I'm not going to show any more code, because I'm starting to feel like there are much better ways to do what I want in. What I'm looking for is basicly a generic way to store different types of values inside different window types. I also need a way to send data with different types in a messaging system.
I'd love to read your opinion about how this should be done.
Cheers,
simpler