Getting the const-correctness of Object sorted once and for all

Era Scarecrow rtcvb32 at yahoo.com
Sun May 13 12:57:31 PDT 2012


On Sunday, 13 May 2012 at 19:43:19 UTC, Walter Bright wrote:
> On 5/13/2012 9:39 AM, Stewart Gordon wrote:
>> It seems that the main obstacle is rewriting the relevant 
>> methods in std.stream. The current implementation doesn't make 
>> sense anyway - reading the entire contents of a file is 
>> certainly not the way to generate a hash or string 
>> representation of the stream. I'm thinking the hash should 
>> probably be the stream handle, and the string representation 
>> could perhaps be the full pathname of the file. Of course, 
>> what it should be for non-file streams is another matter. 
>> (This would be a change at the API level, but when the API's 
>> as fundamentally flawed as this....)
>
> I'd like to see std.stream dumped. I don't see any reason for 
> it to exist that std.stdio doesn't do (or should do).
>
> The only reason it's there at the moment is for backwards 
> compatibility.

  Recently in my own code I've started making use of std.stream, 
mostly creating temporary string streams to test IO in functions 
that would otherwise need actual files for unittests.

  Glancing over std.stdio, I only see specific file access 
functions and routines. If I'm doing the wrong approach please 
correct me now.

---
import std.stream;

struct Header {
   char[4] name;
   int size;

   int read(InputStream is_in) {
     is_in.readExact(name.ptr, name.sizeof);
     is_in.read(size);
     return this.sizeof;
   }

   int write(OutputStream os_out) {
     os_out.writeExact(name.ptr, name.sizeof);
     os_out.write(size);
     return this.sizeof;
   }

   unittest {
     ubyte[Header.sizeof] buffer;
     auto buff_stream = new TArrayStream!(ubyte[])(buffer);

     Header x = Header("abcd", 1024);
     x.write(buff_stream);
     assert(buffer == [97, 98, 99, 100, 0, 4, 0, 0]);

     buff_stream.seekSet(0);
     x = Header();

     assert(x.size == 0);
     x.read(buff_stream);
     assert(x.name == "abcd");
     assert(x.size == 1024);
   }
}


More information about the Digitalmars-d mailing list