473,466 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Runtime Definition of a struct

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
11 5553

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
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
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

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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave | last post by:
Hello all, I am creating a linked list implementation which will be used in a number of contexts. As a result, I am defining its value node as type (void *). I hope to pass something in to its...
10
by: Kobu | last post by:
My question is about the use and meaning of the terms "declaration" and "definition" as it pertains to the C language. I've read sources that mix the two up when talking about such things as...
5
by: A.M | last post by:
Hi, Using c# language, I am developing a web service that needs to return mutiple values. I am, therefore, returning a class contains mutiple values to be returned. So the web method's...
4
by: chris | last post by:
Hi, I write some code guarded with exception handling... simplified code look like this... int main(int argc, char* argv){ try{
3
by: joe.user0 | last post by:
Your C program is using library libA which has a structure struS. You need to modify struS but may not touch the library. How can your program redefine the structure struS while keeping the...
9
by: gopal | last post by:
Why is ; ~ semi-colon used at the end of class definition ? Thanks JK
2
by: Laurent Deniau | last post by:
I would like to know why the following small program does not compile (checked with gcc 4.1.2) and if the compiler behavior is correct: struct A; typedef void (T)(struct A*); void f(void) {...
15
by: vaib | last post by:
hi to all.i'd like to know the actual difference between variable declaration and definition.it would be very helpful if anyone out there wud help me out with this thing.i'm writing here after here...
7
by: sh.vipin | last post by:
/* Sample code*/ #include <stdio.h> int a; //line # 3 int a; //line # 4 int main (int argc, char *argv){ printf("a is %d",a); }
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.