Changeset 55

Show
Ignore:
Timestamp:
06/26/08 21:43:21 (7 months ago)
Author:
baxissimo
Message:

Actually, if all uints and ulongs are saved with variable length encoding it should fix all the 32/64 bit problems (except problems of actually trying to store an array of >4GB).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/doost/util/serializer/archive/BinaryArchive.d

    r54 r55  
    179179            // Load file format indicator 
    180180            uint fmt; 
    181             loadNumber(fmt); 
     181            loadNumberDirect(fmt); 
    182182 
    183183            uint altEndianCode = FormatCode; 
     
    198198            // Load version number 
    199199            uint ver; 
    200             loadNumber(ver); 
     200            loadNumberDirect(ver); 
    201201            if (ver>FormatVersion) { 
    202202                throw new ParsingException("Binary File too new"); 
     
    206206            // Load size_t.sizeof indicator (tells if it's a 32 or 64 bit platform) 
    207207            ubyte sizeof_length; 
    208             loadNumber(sizeof_length); 
     208            loadNumberDirect(sizeof_length); 
    209209            if (sizeof_length != LengthSizeof) { 
    210210                // TODO: loading 32bit data on 64 bit platform (or vice versa) 
     
    225225        else { 
    226226            // write header 
    227             storage.put(encode(FormatCode)); 
    228             storage.put(encode(FormatVersion)); 
    229             storage.put(encode(LengthSizeof)); 
     227            dumpNumberDirect(FormatCode); 
     228            dumpNumberDirect(FormatVersion); 
     229            dumpNumberDirect(LengthSizeof); 
    230230            ubyte[ReservedHeaderBytes] pad; 
    231231            pad[] = 0x00; 
     
    305305            return true; 
    306306        } 
     307        static if(is(VALUE==uint) || 
     308                  is(VALUE==ulong))  
     309        { 
     310            // See note in dumpNumber() about these special cases. 
     311            if (!loadPackedUInteger(value)) 
     312                return false; 
     313        } 
    307314        else { 
    308315            value = decode!(VALUE)(storage.get(VALUE.sizeof)); 
     
    314321     **************************************************************************/ 
    315322    bool dumpNumber(VALUE)(ref VALUE value) { 
    316         static if( is(VALUE==real) || is(VALUE==creal) || is(VALUE==ireal)) { 
     323        static if( is(VALUE==real) || 
     324                   is(VALUE==creal) || 
     325                   is(VALUE==ireal))  
     326        { 
    317327            // TODO: implement platform independent multi-precision float  
    318328            // to handle any platform-specific 'real' type. 
     
    326336                return false; 
    327337        } 
     338        static if(is(VALUE==uint) || 
     339                  is(VALUE==ulong))  
     340        { 
     341            // All uints and ulongs are stored packed for 32/64 bit compatibility. 
     342            // This makes it so things like size_t can be reliably packed/unpacked 
     343            // and they will work across the 32/64 divide as long as the actual 
     344            // size in a size_t doesn't exceed the size of int.max (which should be 
     345            // most of the time.) 
     346            if (!dumpPackedUInteger(value)) 
     347                return false; 
     348        } 
    328349        else { 
    329350            storage.put(encode(value)); 
    330351        } 
     352        return true; 
     353    } 
     354 
     355    /*************************************************************************** 
     356     **************************************************************************/ 
     357    bool loadNumberDirect(VALUE)(ref VALUE value) { 
     358        value = decode!(VALUE)(storage.get(VALUE.sizeof)); 
     359        return true; 
     360    } 
     361 
     362    /*************************************************************************** 
     363     **************************************************************************/ 
     364    bool dumpNumberDirect(VALUE)(VALUE value) { 
     365        storage.put(encode(value)); 
    331366        return true; 
    332367    } 
     
    516551            if (skip(Marker.defref)) { 
    517552                uint symbol; 
    518                 if (!traverse(symbol)) throw new ParsingException("Lack of symbol!"); 
     553                if (!traverse(symbol))  
     554                    throw new ParsingException("Lack of symbol!"); 
    519555                value = registry.get!(TypeDescription, VALUE).address(symbol); 
    520556                return true; 
     
    546582            if (symbol > 0) { 
    547583                storage.put(Marker.defref); 
    548                 storage.put(encode(symbol)); 
     584                dumpNumber(symbol); 
    549585                return true; 
    550586            } 
     
    578614        if (defined_ver > 0) { 
    579615            storage.put( Marker.verBegin ); 
    580             storage.put( encode(defined_ver) ); 
     616            dumpNumber(defined_ver); 
    581617            storage.put( Marker.verEnd ); 
    582618        }