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

Home Posts Topics Members FAQ

how to implement a "typoef" thingy.

I need this: (basically because I want to read a structure from disk
and then fix big/little endian problems for certain members.

class ST
{
int i;
float f;
double d;
};

int main()
{
ST st;
for each member in st
{
select (member.type)
{
case int: ...
}
}
}
What's the best way to implement this?

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jan 16 '06 #1
8 1514
Gernot Frisch wrote:
I need this: (basically because I want to read a structure from disk
and then fix big/little endian problems for certain members.

class ST
{
int i;
float f;
double d;
};

int main()
{
ST st;
for each member in st
{
select (member.type)
{
case int: ...
}
}
}
What's the best way to implement this?


Overload a function, that's the easiest way. Ie.

void foo(int x);
void foo(float x);
void foo(double x);

foo(st.f);

and possibly encapsulate it all nicely into classes etc.

--
Regards,

Ferdi Smit (M.Sc.)
Email: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
Jan 16 '06 #2
Gernot Frisch wrote:
I need this: (basically because I want to read a structure from disk
and then fix big/little endian problems for certain members.

class ST
{
int i;
float f;
double d;
};

int main()
{
ST st;
for each member in st
{
select (member.type)
{
case int: ...
}
}
}
What's the best way to implement this?


This is covered in the FAQ, see Templates section.

V
Jan 16 '06 #3

"Ferdi Smit" <Fe********@cwi.nl> schrieb im Newsbeitrag
news:It********@cwi.nl...
Gernot Frisch wrote:
I need this: (basically because I want to read a structure from
disk and then fix big/little endian problems for certain members.

class ST
{
int i;
float f;
double d;
};

int main()
{
ST st;
for each member in st
{
select (member.type)
{
case int: ...
}
}
}
What's the best way to implement this?


Overload a function, that's the easiest way. Ie.

void foo(int x);
void foo(float x);
void foo(double x);

foo(st.f);

and possibly encapsulate it all nicely into classes etc.


But... how would I perform a "for each member" ? I guess that is my
question. Yes, now that I think of it. I asked the part I know, not
the part I don't know.



Jan 16 '06 #4
Gernot Frisch wrote:
[..]
But... how would I perform a "for each member" ? I guess that is my
question. Yes, now that I think of it. I asked the part I know, not
the part I don't know.


You can't perform "for each member". There is no construct in C++
available to do that.

V
Jan 16 '06 #5
Victor Bazarov wrote:
Gernot Frisch wrote:
[..]
But... how would I perform a "for each member" ? I guess that is my
question. Yes, now that I think of it. I asked the part I know, not
the part I don't know.


You can't perform "for each member". There is no construct in C++
available to do that.

V


Well, I could conceive of one using iterator-like functionality and
implemented via boost::any, but it's probably not worth the hassle. The
easiest way to accomplish what the OP wants to do is probably just to
have a function that switches endianess on some particular structure or
class. That function would have knowledge of the internal details of
the struct/class, but that seems unavoidable when we're already
worrying about low-level details such as endianess. Just consider it
part of the read process.

Cheers! --M

Jan 16 '06 #6
Gernot Frisch wrote:

But... how would I perform a "for each member" ? I guess that is my
question. Yes, now that I think of it. I asked the part I know, not
the part I don't know.


Hmmm, that is actually not so simple, if possible at all. My suggestion
is to take a closer look at Boost.Serialization (
http://www.boost.org/libs/serialization/doc/index.html ), or maybe even
use it? In C++ you cannot simply iterate over program structures, as for
example in scripted languages like PHP (yikes). The easiest method is to
simply allow these structures to write their own members; but many of
the different methods and pros and cons are described on that boost
serialization page. Theoretically you can, in fact, write a sort of
template meta structure with a list of member types and their data, and
iterate over that... I wouldn't advice it. How I wish C++ would just
support meta programming. This template mess always feels so much like a
hack.
--
Regards,

Ferdi Smit (M.Sc.)
Email: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
Jan 16 '06 #7

mlimber wrote:
Victor Bazarov wrote:

You can't perform "for each member". There is no construct in C++
available to do that.

V


Well, I could conceive of one using iterator-like functionality and
implemented via boost::any, but it's probably not worth the hassle. The
easiest way to accomplish what the OP wants to do is probably just to
have a function that switches endianess on some particular structure or
class. That function would have knowledge of the internal details of
the struct/class, but that seems unavoidable when we're already
worrying about low-level details such as endianess. Just consider it
part of the read process.

Cheers! --M


The other option is to take advantage of the fact that assignment and
copying by default does member-by-member assignment/copy.

So write your wrapper classes for each type and then have your struct
contain such wrappers. When you copy/assign your struct it will
automatically copy/assign all the wrappers thus calling all your
overloads which can be specialised to do what you want.

Is a bit "abusive" though.

Jan 16 '06 #8
The other option is to take advantage of the fact that assignment
and
copying by default does member-by-member assignment/copy.

So write your wrapper classes for each type and then have your
struct
contain such wrappers. When you copy/assign your struct it will
automatically copy/assign all the wrappers thus calling all your
overloads which can be specialised to do what you want.

Is a bit "abusive" though.


Very nice idea!
Jan 16 '06 #9

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

Similar topics

3
by: Robbie | last post by:
Even though this is not very important it would be nice to get working. Basically all I want to do is continuously write to the same line. So I could have a little rotating text thing showing...
6
by: Charles Law | last post by:
This is going to seem like a basic OO question, but it comes up and bites me every now and again. Suppose we have a multi-tiered protocol to implement, what is the logical, OO way to design the...
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
4
by: Peter | last post by:
I want to copy a parent class instance's all datas to a child's. It's actually a C++'s copy constructor. But why the following code does not work - there is a compile error! How it should look...
3
by: Brett Hall | last post by:
I have a VB.NET interface that my managed C++ code is to implement. I seem to be stuck implementing an event defined in that interface. Does anyone have a simple code snippet that will show me...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
7
by: moondaddy | last post by:
If I'm in a class that inherits an interface, is there a shortcut key that will write the implementation of the interface into the class? I remember seeing something like this in vb.net. ...
8
by: GS | last post by:
Guys: I have a question, Is it possible to implement pop-up window without Java script, we don't want to use java script since it might get blocked by pop-up blocker. Thanks in advance. GS.
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
0
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,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
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: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
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.