By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Runtime Definition of a struct

P: n/a
I don't think this is possible but, if anyone has comments on a work
around that would be great. Let me explain the reason I would like to
do this. Suppose you are given a file that contains a header defining
a structure for a set of data that is contained in the file. If you do
not know what the structure is before you read the header of the file
how would you work with this in code?

Any comments would be greatly appreciated.

Dec 11 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

to************@gmail.com wrote in message
<11**********************@f14g2000cwb.googlegroups .com>...
I don't think this is possible but, if anyone has comments on a work
around that would be great. Let me explain the reason I would like to
do this.
Do what?
Suppose you are given a file that contains a header defining
a structure for a set of data that is contained in the file. If you do
not know what the structure is before you read the header of the file
how would you work with this in code?

Any comments would be greatly appreciated.


Please go through your post and replace all instances of 'this' with an
actual thought.

--
Bob R
POVrookie
Dec 11 '05 #2

P: n/a
This -> Define a struct at Runtime (According to message.subject)

BobR wrote:
to************@gmail.com wrote in message
<11**********************@f14g2000cwb.googlegroups .com>...
I don't think this is possible but, if anyone has comments on a work
around that would be great. Let me explain the reason I would like to
do this.


Do what?
Suppose you are given a file that contains a header defining
a structure for a set of data that is contained in the file. If you do
not know what the structure is before you read the header of the file
how would you work with this in code?

Any comments would be greatly appreciated.


Please go through your post and replace all instances of 'this' with an
actual thought.

--
Bob R
POVrookie


Dec 11 '05 #3

P: n/a
Ian
to************@gmail.com wrote:
I don't think this is possible but, if anyone has comments on a work
around that would be great. Let me explain the reason I would like to
do this. Suppose you are given a file that contains a header defining
a structure for a set of data that is contained in the file. If you do
not know what the structure is before you read the header of the file
how would you work with this in code?

Any comments would be greatly appreciated.

Assuming you know how to process the datatypes in the file, you could
read it into a std::map<Type,std::string> and process the values based
on their type.

Ian
Dec 11 '05 #4

P: n/a

to************@gmail.com wrote in message
This -> Define a struct at Runtime (According to message.subject)

BobR wrote:
Please go through your post and replace all instances of 'this' with an
actual thought.

"I don't think Define a struct at Runtime is possible but, if anyone
has comments on a work around that would be great.
Let me explain the reason I would like to do Define a struct at Runtime.
Suppose you are given a file that contains a header defining a structure
for a set of data that is contained in the file. If you do not know what the
structure is before you read the header of the file how would you work
with Define a struct at Runtime in code?"

Yeah, It's all clear now! Duh. <G>

If you do not know what the structure is, how do you even know you need it?
What happens if you comment-out the #include line?

Change line 43 in your code to read, "struct SomeStruct;", and you'll be OK.

Look up:
template
factory pattern
forward declarations
std::vector (and containers)
[ snip rest of list ]

If you want help, you need to tell the people in this group exactly what you
are trying to do and what problem you are having. Show a short code example.

Please do not top-post (see: below Mr. Steinbach's sig).

--
Bob R
POVrookie
Dec 11 '05 #5

P: n/a
I guess my explanation of the problem is not as clear as it could be.
Let me further explain.
Suppose you are collecting data using a data acquisition system. You
collect data with 7 channels, 2 of them have integer values, 4 have
float values, and 1 has a double value. This information would be
described in the header of a binary data file. The header would also
indicate that you have 1000 such structures at a particular offset from
the begining of the file. This corresponds to 1000 samples of each of
the channels.

You could describe this using a structure as follows

struct channels{
int channel1
int channel2
float channel3
float channel4
float channel5
float channel6
double channel7
};

However the next time you collect data you have only six channels
described by the following structure.

struct channels(
int channel1
float channel2
int channel3
double channel4
float channel5
short channel6
};

Because you do not know the structure of the data before you read the
header of the file you cannot create a structure to read it at compile
time. This leads to the question: Can you
create a structure at runtime? I have never seen this done before so I
resorted to reading the data as an unsigned char array. I then created
a vector using the STL for each channel which are all stored in another
vector. I then cast each individual channel out of the char array
using an offset and push it onto the correct vector.

This seemed like a lot of work to me and being lazy I thought someone
else might have a better solution. I hope this better explains the
problem I am trying to solve and maybe someone has some advice on how
to proceed.

Cheers,
Tony

Dec 12 '05 #6

P: n/a
Ian
to************@gmail.com wrote:
This leads to the question: Can you create a structure at runtime?


Kind of, you can represent each structure as a vector of polymorphic
object pointers, where the object would be something like

struct ObjectBase
{
virtual void fromFile() = 0;

// any other operation you may wish to perform
};

template <typename T>
struct Object : ObjectBase
{
T value;

void fromFile() {...}

...
};

std::vector<ObjectBase*> psudoStruct;

Ian
Dec 12 '05 #7

P: n/a
Lets imagine that you can form "dynamic structure". Next question is:
how are you going to access its fields, if at compile time you don't
know what the fields are? Please provide example of usage of structure
with unknown number of fields of unknown type.

The only solution is a class like following:
struct A
{
int getNumFields();
int getFieldType(int fieldNo);
float getFloatField(int fieldNo);
float getIntField(int fieldNo);
//...
};

Dec 12 '05 #8

P: n/a
I didn't quite go through the post ... but sounds like a factory
pattern can help.
Search up "factory pattern" to learn more

Samee

Dec 12 '05 #9

P: n/a
to************@gmail.com wrote:
I don't think this is possible but, if anyone has comments on a work
around that would be great. Let me explain the reason I would like to
do this. Suppose you are given a file that contains a header defining
a structure for a set of data that is contained in the file. If you do
not know what the structure is before you read the header of the file
how would you work with this in code?


A factory class has been suggested, but may not be suitable in this
case. A factory class basically requires that you pre-define all the
possible structure types that might be read in, and then based on the
data in the file it decides which of those to create.

It sounds like your inputs form an unconstrained set -- i.e. you can't
pre-define all the possible inputs. Under these circumstances, I'd
consider representing each data item as something like an
std::vector<boost::any>. In particular, structs require that you
hard-code struct member names in your code, and under the
circumstances, you don't even know how many names there will be, not to
mention what their names or types are. With a vector, you can have an
arbitrary number of items and you don't need a name for each -- you
just need a number. It sounds like you'll format these as something
like "channel %d" during output (except we'll hope you're not really
using printf) which makes the numbers pretty easy to deal with anyway.

Dec 13 '05 #10

P: n/a
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.

Thanks,
Tony

Dec 14 '05 #11

P: n/a
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

Dec 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.