to************@gmail.com wrote:
My reason for wanting to create the structure was to read the data from
the file as a structure. However, this doesn't appear to be possible.
I will stick with my original method of reading a char array from the
file and then casting each channel to its appropriate data type.
Conceptually, you don't want a C++ structure. A C++ structure is meant
to be used when you know in advance the format of the data you want to
store. That's not your case. What you want is a container.
You have several ways to store the data in the container. One way would
be to store everything in strings.
typedef std::string Key, Value;
typedef std::map<Key, Value> Structure;
typedef std::vector<Structure> Data;
Data d;
Structure s;
s.insert(std::make_pair("channel1", "1"));
s.insert(std::make_pair("channel2", "2"));
s.insert(std::make_pair("channel3", "3"));
d.push_back(s);
Alternatively, you could use a common base class if you want to use the
exact type.
class Element
{
public:
virtual ~Element() {}
virtual void do_something() = 0;
};
template <class T>
class Channel : public Element
{
public:
Channel(T value)
: val_(value)
{
}
virtual void do_something()
{
}
private:
T val_;
};
typedef std::string Key;
typedef Element* Value;
typedef std::map<Key, Value> Structure;
typedef std::vector<Structure> Data;
Data d;
Structure s;
s.insert(std::make_pair("channel1", new Channel<int>(1)));
s.insert(std::make_pair("channel2", new Channel<float>(2.0f)));
s.insert(std::make_pair("channel3", new Channel<double>(3.0)));
// don't forget to delete them
d.push_back(s);
You then have to devise a way to use these "runtime structures" in a
generic way. That is, try not to use type switch (in whatever forms:
dynamic_cast, if(e.type == channel_type), etc.) but use polymorphism
instead (the do_something() function). This is the most important part
of the design: the problem here is not how you *store* the information,
but how you *use* it.
If you need help with that design, show us the code you've got so far
and we'll do what we can.
Jonathan