how to store different types of data in deque?

J

Jenny

Hi,

I have a class foo which will construct some objects in my code. some
of the objects store int values into the data deque, while others
store float values to the deque.

template <class TYPE>
class foo
{
protected:
std::string name;
std::deque<TYPE> data;
};

After the objects finish storing the data into deques. I need another
deque or something store the objects. I was trying to use
std::deque<foo> foo_list; to store the ojbects. Because class foo is a
template class, I can only construct it as std::deque<foo<int> >
foo_list; or std::deque<foo<float> > foo_list;
However I need a deque to store both foo<float> and foo<int>. Is there
any other way I can use to store the foo objects in a list?
 
K

Karl Heinz Buchegger

Jenny said:
Hi,

I have a class foo which will construct some objects in my code. some
of the objects store int values into the data deque, while others
store float values to the deque.

template <class TYPE>
class foo
{
protected:
std::string name;
std::deque<TYPE> data;
};

After the objects finish storing the data into deques. I need another
deque or something store the objects. I was trying to use
std::deque<foo> foo_list; to store the ojbects. Because class foo is a
template class, I can only construct it as std::deque<foo<int> >
foo_list; or std::deque<foo<float> > foo_list;
However I need a deque to store both foo<float> and foo<int>. Is there
any other way I can use to store the foo objects in a list?

You need polymorphic behaviour, thus
* Derive foo from a common base classs
* store pointers in the deque instead of objects.

class fooBase
{
protected:
std::string name;
};

template <class TYPE>
class foo : public fooBase
{
protected:
std::deque<TYPE> data;
};

std::deque<fooBase*> foo_list;

Attention: Since you are storing pointers, you are now responsible
for deleting the foo objects. The deque won't do it any longer.
You can get around this by using a smart pointer you can get
eg. at www.boost.org
 
J

John Harrison

Jenny said:
Hi,

I have a class foo which will construct some objects in my code. some
of the objects store int values into the data deque, while others
store float values to the deque.

template <class TYPE>
class foo
{
protected:
std::string name;
std::deque<TYPE> data;
};

After the objects finish storing the data into deques. I need another
deque or something store the objects. I was trying to use
std::deque<foo> foo_list; to store the ojbects. Because class foo is a
template class, I can only construct it as std::deque<foo<int> >
foo_list; or std::deque<foo<float> > foo_list;
However I need a deque to store both foo<float> and foo<int>. Is there
any other way I can use to store the foo objects in a list?

Polymorphism (as recommended by Karl) is the usual way to do this. But in
this case you have another choice, store your ints and floats as strings.

class foo
{
protected:
std::string name;
std::deque<std::string> data;
};

It's easy enough to convert from strings to ints and floats and back. Easy
enough to tell if a string is an int or a float. Just a suggestion and a bit
of a hack, but maybe it would be a good idea.

john
 
I

Ivan Vecerina

Jenny said:
I have a class foo which will construct some objects in my code. some
of the objects store int values into the data deque, while others
store float values to the deque. ....
After the objects finish storing the data into deques. I need another
deque or something store the objects. I was trying to use
std::deque<foo> foo_list; to store the ojbects. Because class foo is a
template class, I can only construct it as std::deque<foo<int> >
foo_list; or std::deque<foo<float> > foo_list;
However I need a deque to store both foo<float> and foo<int>. Is there
any other way I can use to store the foo objects in a list?
Previous posters have suggested:
- storing a textual representation into std::string-s
- a container of polymorphic elements (storing a ptr to a base class).
Another option would be to use a C-style union (usually in conjunction
with a typeId field), but let's not insist on this.

However, the problem you are facing is common enough, and encapsulated
solutions already exist: classes that can store a value of one of
several types. Some vendors (MS) call this a Variant, other call
them *discriminated unions* (because they are like C-style unions,
but provide information about the stored type).
http://www.google.com/search?q=discriminated+union+C++

The boost library provides such a class, called "boost::any".
You may want to give it a try:
http://www.boost.org/doc/html/any.html
In particular, take a look at the example:
http://www.boost.org/doc/html/ch02s02.html


Cheers,
Ivan
 
A

Alex Vinokur

John Harrison said:
Polymorphism (as recommended by Karl) is the usual way to do this. But in
this case you have another choice, store your ints and floats as strings.

class foo
{
protected:
std::string name;
std::deque<std::string> data;
};

It's easy enough to convert from strings to ints and floats and back. Easy
enough to tell if a string is an int or a float. Just a suggestion and a bit
of a hack, but maybe it would be a good idea.

john

It would be a good idea not only for ints and floats, but for other types if are using stream_cast.

stream_cast() was discussed by Dietmar Kuehl at
* http://groups.google.com/[email protected]

To be sure that stream_cast conversion is bijective, one could use functions which detect stream_cast bijectivity
* http://groups.google.com/groups?th=8bb967e46c5e3a87
* http://groups.google.com/[email protected]
 
M

Max Vasin

John Harrison said:
Polymorphism (as recommended by Karl) is the usual way to do this. But in
this case you have another choice, store your ints and floats as strings.

class foo
{
protected:
std::string name;
std::deque<std::string> data;
};

It's easy enough to convert from strings to ints and floats and back. Easy
enough to tell if a string is an int or a float. Just a suggestion and a bit
of a hack, but maybe it would be a good idea.
Alternatively you can use boost::any:
class foo
{
protected:
std::string name;
std::deque<boost::any> data;
};

And then you check the type of the object stored in runtime
data.type() == typeid();
int x = boost::any_cast<int>(data);

Or you can use boost::variant:
class foo
{
protected:
std::string name;
std::deque< boost::variant<int, float> > data;
};

And then:
if (int* x = boost::get<int>(data))
do_smth_on_int(*x);
else if (float* x = boost::get<float>(data))
do_smth_on_float(*x);
 
G

Gernot Frisch

Alternatively you can use boost::any:
Or you can use boost::variant:

Doh! I never heared of these two. And wow, boost::any looks really
interesting...
-Gernot
 
T

Tom Widmer

Hi,

I have a class foo which will construct some objects in my code. some
of the objects store int values into the data deque, while others
store float values to the deque.

template <class TYPE>
class foo
{
protected:
std::string name;
std::deque<TYPE> data;
};

After the objects finish storing the data into deques. I need another
deque or something store the objects. I was trying to use
std::deque<foo> foo_list; to store the ojbects. Because class foo is a
template class, I can only construct it as std::deque<foo<int> >
foo_list; or std::deque<foo<float> > foo_list;
However I need a deque to store both foo<float> and foo<int>. Is there
any other way I can use to store the foo objects in a list?

boost::variant lets you do this:

typedef boost::variant<foo<int>, foo<float> > types;
std::deque<types> d;
d.push_back(aFooFloat);
d.push_back(aFooInt);

Then apply_visitor is the best way to get at the values, but you may
also use get:
foo<float>& foo = get<foo<float> >(d[0]);

See http://www.boost.org/doc/html/variant.html.

In your case, a better approach might be to give foo a non-templated
base class, and use a deque<shared_ptr<foo_base> >.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,177
Latest member
OrderGlucea
Top