Extending iostreams to structures
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Extending iostreams to structures

  1. #1
    Join Date
    Dec 2004
    Location
    Paso de Robles
    Posts
    296

    Extending iostreams to structures

    I'm trying to write an object oriented application framework. The programs can be console or GUI based, but I'm having some difficulty providing a consistent interface for outputting structures. The structures will be displayed by a variable description and its value. So, for a RECT structure the output on console would look like the following.
    Code:
    left           0
    top           0
    right      100
    bottom   200
    The numbers should be aligned next to their description. In console mode, white space must be appended to each description. In GUI, this extra process will not be necessary (some other processing might be, but it will be different).
    The right way to display data in C++ is of course using a stream and defining the appropriate operator<<.
    Code:
    std::ostream& operator<<(std::ostream& strm, const RECT& r)
    {
       strm << "left" << r.left << "top" << r.top << "right" << r.right << "bottom" << r.bottom;
       return strm;
    }
    Note that endl and the spacing has been omitted. This is because GUI output will also be implemented as a stream and they do not require a carriage return or padding. Should I define a StructStream object?
    After I use the structstream object to store the output as a stream how should the output be written to the screen. Should I call cout from within the stream object. Should I have two buffers, one which borrows cout.rdbuf()? Should I call printf? Should I open another standard out file and use write? Should I convert the buffer to a string which will be display by cout or a winout? Can someone give me an idea of what I should do? Thanks.

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,712

    Re: Extending iostreams to structures

    You could maybe define another overload for ostringstream

    cout would use the ostream version and writes to an ostringstream would use the other.

    Code:
    struct RECT
    {
        int left;
        int top;
        int right;
        int bottom;
    };
    
    std::ostringstream& operator<<(std::ostringstream& strm, const RECT& r)
    {
       strm << "left" << r.left << "top" << r.top << "right" << r.right << "bottom" << r.bottom;
       return strm;
    }
    
    std::ostream& operator<<(std::ostream& strm, const RECT& r)
    {
       strm << "left" << r.left << "top" << r.top << "right" << r.right << "bottom" << r.bottom;
       return strm;
    }
    
    int main()
    {
        RECT r;
    
        std::cout << r;
    
        std::ostringstream ss;
        ss << r; 
    
        return 0;
    }
    Don't know if that helps?
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center