Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 4001

Show
Ignore:
Timestamp:
10/10/08 22:06:13 (1 month ago)
Author:
kris
Message:

added a [tentative] DataInput?.get() wrapper for allocating array memory

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/io/stream/DataStream.d

    r3856 r4001  
    9696        /*********************************************************************** 
    9797 
     98                Read an array back from the source, with the assumption 
     99                it has been written using DataOutput.put() or otherwise 
     100                prefixed with an integer representing the total number 
     101                of bytes within the array content. That's *bytes*, not 
     102                elements. 
     103 
     104                An array of the appropriate size is allocated either via 
     105                the provided delegate, or from the heap, populated and 
     106                returned to the caller. Casting the return value to an 
     107                appropriate type will adjust the number of elements as 
     108                required: 
     109                --- 
     110                auto text = cast(char[]) input.get; 
     111                --- 
     112                 
     113 
     114        ***********************************************************************/ 
     115 
     116        final void[] get (void[] delegate(uint bytes) dg = null) 
     117        { 
     118                auto len = getInt; 
     119                auto dst = (dg ? dg(len) : new void[len]); 
     120                input.readExact (dst.ptr, len); 
     121                return dst; 
     122        } 
     123 
     124        /*********************************************************************** 
     125 
    98126        ***********************************************************************/ 
    99127 
     
    356384 
    357385                auto input = new DataInput (buf); 
    358                 assert (input.get(new char[9]) is 9); 
     386                assert (input.get.length is 9); 
    359387                assert (input.getInt is 1024); 
    360                 input.getInt; 
    361388        } 
    362389}