Fog Creek Software
Discussion Board




<iostream> - polymoronic

Way back in the day, I was a iostream fanatic.  OK, forget that.

Anyway, I'm up late writing this parser, and its going to take a bit longer than this day - so I'm going to bed.

But I was musing that the only types really suppored by iostreams are files and strings.

Well, the POSIX file descriptor 'int' is more polymorphic than that.  With open, close, read, write, I can read sockets, files, ttys, devices, and all kinds of mayhem (notably missing is a string type, but I could write a device driver if I had to - heh, heh).

To support sockets and devices, one would have to write a streambuf type to support these types.  I'm surprised that there isn't already something out there.  Is there?

Good night.

hoser
Tuesday, August 24, 2004

http://www.gnu.org/software/commoncpp/docs/refman/html/class_t_c_p_stream.html

Oren Miller
Tuesday, August 24, 2004

http://www.dre.vanderbilt.edu/Doxygen/Current/html/ace/classACE__IOStream.html

Oren Miller
Tuesday, August 24, 2004

http://members.aon.at/hstraub/linux/socket++/index.html

Oren Miller
Tuesday, August 24, 2004

hoser, so why not write a POSIX file stream buffer class?  Then you can get an iostream interface to any POSIX file type by just supplying the handle (and you get the benefit of formatted I/O that comes with iostreams).

Personally, I use iostream quite a bit.  I've made a "ordered data base" class that stores tables of data, where each cell is an iostream (this is useful for package files).  I've made a handy little system for writing network servers, which makes use of iostreams for new connections.  I use iostreams to wrap IPC mechanisms between processes.  I've also got stream mutator objects, which produce an istream or ostream by changing some property of another istream or ostream (for example EncryptStream(x) produces an encrypted version of the ostream x, or DecompressStream(y) produces a decompressed version of the istream y) -- this is also useful for syntactic transformations (for example, I've got a stream for converting something like "<head><title><%= my-title %></title</head>" to "(progn (send "<head><title>") (send my-title) (send "</title></head>"))").

On top of that I standardize on iostream for parsing data (for example, I've made a parser generator library that produces parsers which take istream references as input).  This would be difficult to do with POSIX files in some instances, because you have less control of how to account for "data not ready" states and so on.

For example, in my network server system I have a function RunTCPServer<X>.  The X is a type parameter that identifies the kind of object to create for each connection,  so long as this object supports function-call behavior (i.e.: is a function or overloads ()).  RunTCPServer will pass that function an iostream object that represents the connection, and then the function can use that iostream object as if it was the only connection handler running.  This allows me to write code that's as simple as "get a byte, run it through a DFA, continue until we reach an end state" rather than managing disjoint buffers and writing code to switch between connections and so on.

Kalani
Tuesday, August 24, 2004

Very cool replies!  Thanks!

hoser
Tuesday, August 24, 2004

I had a bit of a epiphany about iostreams a while back.

Iostreams have very little to do with I/O. The iostream library is all about converting objects to strings and back. The I/O only comes in because you need somewhere to put said strings.

Once I realized that the library made a lot more sense. :-)

Chris Tavares
Tuesday, August 24, 2004

Well, converting objects to streams and back.

Oren Miller
Tuesday, August 24, 2004

*  Recent Topics

*  Fog Creek Home