Sign in to follow this  

[STL] How to store variable Types?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}
};

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this