how to implement a "typoef" thingy.

G

Gernot Frisch

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
 
F

Ferdi Smit

Gernot said:
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: (e-mail address removed)
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
 
V

Victor Bazarov

Gernot said:
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
 
G

Gernot Frisch

Ferdi Smit said:
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.
 
V

Victor Bazarov

Gernot said:
[..]
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
 
M

mlimber

Victor said:
Gernot said:
[..]
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
 
F

Ferdi Smit

Gernot said:
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: (e-mail address removed)
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
 
E

Earl Purple

mlimber said:
Victor Bazarov wrote:

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

Gernot Frisch

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top