473,800 Members | 2,893 Online
Bytes | Software Development & Data Engineering Community
+ 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 5591

to************@ gmail.com wrote in message
<11************ **********@f14g 2000cwb.googleg roups.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************ **********@f14g 2000cwb.googleg roups.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,s td::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<Obj ectBase*> 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(in t fieldNo);
float getFloatField(i nt 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<boo st::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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2446
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 "constructor" so that I will be able to manipulate my list without the need for constant casting; some sort of runtime type-safety mechanism. For example, I want a linked lists of ints. I want to be able to say:
10
20896
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 setting aside storage for an object, defining/declaring a struct, parts of a function, referencing an external variable in another module. sourcefile1.c ============== extern long globalfoo; /* declaration? */
5
1202
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 definition is like this: public LocationResult GetClosestPoint(string Licence,string Location, int r,bool ReturnImage)
4
13691
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
2030
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 compiler happy?
9
5097
by: gopal | last post by:
Why is ; ~ semi-colon used at the end of class definition ? Thanks JK
2
4706
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) { struct A { int _; } a; ((T*)0)(&a);
15
14918
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 after a long time since people here also helped me out with my lexer. thanking in anticipation.
7
1836
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); }
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10273
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10250
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5469
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
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 we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.