[STL] How to store variable Types?

Started by
3 comments, last by gimp 17 years, 10 months ago
I have to store some rows from a database out in to memory. The database will return, strings, floats and integers. I'd like to populate all these result in to a class and pass that class around. Any idea on how this can be done without me having to resort to storing void*'s and then using a enum to store what type it actually is? Many thanks, Chris
Chris Brodie
Advertisement
You could try a boost::variant<std::string, float, int>
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
You could creat a "row" class that holds a single row of your table. In it you would have your strings, floats and integers. You can then store each "row" and retrive the information when ever you want.

theTroll
Quote:Original post by gimp
I have to store some rows from a database out in to memory. The database will return, strings, floats and integers. I'd like to populate all these result in to a class and pass that class around.

Any idea on how this can be done without me having to resort to storing void*'s and then using a enum to store what type it actually is?

Many thanks,

Chris


For any given column of the database, each row should have the same type of thing in that column. Therefore, you can represent a row of a specific table as a struct where each member is of a data type dictated by the corresponding column.

As a general principle, you don't really want to make "containers of different things", because, very informally, the reason for putting things into a container is so that you can treat them all the same way.

However, when dealing with data that is represented in human-readable format, but where the type isn't known ahead of time, you may seem to be stuck. (This situation occurs commonly when reading in configuration files or implementing a "console" within a program.) In these situations, my usual recommendation is to store everything as a string, and provide accessors to the container which parse the data in addition to looking it up. For example:

class Database {  std::map<std::string, std::string> storage;  template<typename T>  T operator()(const std::string& key) {    std::stringstream ss(storage[key]);    T result = T();    // If the data can't be interpreted as a T, or is absent, we end up    // returning the default-constructed T.    ss >> result;    return result;  }};
Thanks a lot guys. You've given me something to work on. I'd never have come up with a wastful storage structure that stores all types, so thanks for that. Given the fact that this is just returned data I guess there isn't really that much overhead.

The funny thing is I was looking at boost::variant earlier, trying to work out how I could use it to store the column containers, rather than using it to store the individual elements. I even looked at the serialiazation code that converted all values to string form, also as suggested.

Being a silly optimist (as in one who optimises things during design) I probably cut off working solutions by limited thinking, so again thanks.
Chris Brodie

This topic is closed to new replies.

Advertisement