Re: Iterator-like interface to a collection of unrelated types?
On 23 Jul., 14:04, Vitaly <vbudov...@gmail.com> wrote:
Hi,
I have a need to provide an iterator-like interface to a collection of
unrelated types.
The first part of the problem is building this sort of collection --
Boost.Any seems to be a good option, as I can create a
std::vector<boost::any> and add int, float or any other type of
objects to it.
The part that I haven't been able to solve, is how to go about
creating a dereference function that will return the correct type of
object. Overloading won't work, as only the return type will change.
The types have a similar interface e.g.
template <class T>
class List
{
public:
T f() {}
private:
T *data;
};
I don't think a common base class approach will work, since only the
subclasses will know the type T.
Does anyone have any ideas?
If you are asking: What is the proper reference type for
my iterator, this can only be boost::any or any other type
that wraps a boost::any or does behave as a boost::any.
Obviously this does not give you the capability to provide
a compile-time mechanism that returns the underlying
type. In fact, this is impossible (as you already assume),
because the underlying type is part of the dynamic nature
of boost::any or similar types.
boost::any gives you a member type() that allows one to
determine the underlying type (std::type_info), though.
Now you have to decide on your own, how you can take
advantage of this feature:
If the possible set of underlying types is known in
advance, you can provide a function that looks at the
type() and switches on possible known types (hard to
manage), but rather quick (possibly also dirty).
Instead of this hard-coded set of known types you
provide an API, that allows registering handler functions
/function objects based on the std::type_info (or
in C++0x: std::typeindex) as the key and the (smart)
pointer to the handler as a value, e.g. given
typedef void(*handler_t)(boost::any&);
std::[unordered_]map<std::typeindex, handler_t> known_handlers;
std::vector<boost::any> my_data;
this allows for the following traversal (I use here the new C++0x
for-range syntax):
for (boost::any& datum : my_data) {
known_handlers.at(datum.type())(datum);
}
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]