how to implement a "typoef" thingy.

Discussion in 'C++' started by Gernot Frisch, Jan 16, 2006.

  1. 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
    Gernot Frisch, Jan 16, 2006
    #1
    1. Advertising

  2. Gernot Frisch

    Ferdi Smit Guest

    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:
    Room: C0.07 Phone: 4229
    INS3 Visualization and 3D Interfaces
    CWI Amsterdam, The Netherlands
    Ferdi Smit, Jan 16, 2006
    #2
    1. Advertising

  3. 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
    Victor Bazarov, Jan 16, 2006
    #3
  4. "Ferdi Smit" <> schrieb im Newsbeitrag
    news:...
    > 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.
    Gernot Frisch, Jan 16, 2006
    #4
  5. 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
    Victor Bazarov, Jan 16, 2006
    #5
  6. Gernot Frisch

    mlimber Guest

    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
    mlimber, Jan 16, 2006
    #6
  7. Gernot Frisch

    Ferdi Smit Guest

    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:
    Room: C0.07 Phone: 4229
    INS3 Visualization and 3D Interfaces
    CWI Amsterdam, The Netherlands
    Ferdi Smit, Jan 16, 2006
    #7
  8. Gernot Frisch

    Earl Purple Guest

    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.
    Earl Purple, Jan 16, 2006
    #8

  9. > 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!
    Gernot Frisch, Jan 16, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M.E.
    Replies:
    0
    Views:
    343
  2. David Graham

    bucies image selector thingy

    David Graham, Jul 3, 2003, in forum: HTML
    Replies:
    4
    Views:
    539
    brucie
    Jul 3, 2003
  3. Robbie

    loading message thingy

    Robbie, Aug 11, 2004, in forum: Python
    Replies:
    3
    Views:
    259
    John J. Lee
    Aug 12, 2004
  4. keith

    g++ thingy

    keith, Feb 14, 2010, in forum: C++
    Replies:
    14
    Views:
    624
    Jorgen Grahn
    Feb 28, 2010
  5. Mark Hobley

    Another doubleunderscore thingy

    Mark Hobley, Feb 28, 2010, in forum: C Programming
    Replies:
    11
    Views:
    862
    Ersek, Laszlo
    Mar 1, 2010
Loading...

Share This Page