Changeset 99

Show
Ignore:
Timestamp:
11/08/06 13:56:05 (6 years ago)
Author:
Sequoh
Message:

Updates to serialization library (no need to register classes any more, under some circumstances)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/freeuniverse/arc/io/serialization/basicarchive.d

    r59 r99  
    7777                static if (is(POINTERTYPE == class)) 
    7878                { 
    79                     pragma(msg, "Error: class pointers are not supported and should also be unneccessary."); 
    80                     pragma(error, "See the above msg."); 
     79                    static assert(0, "class pointers are not supported and should also be unneccessary"); 
    8180                } 
    8281                else 
     
    9998        else 
    10099        { 
    101             pragma(msg, "Error: describe called with unsupported type."); 
    102             pragma(error, "See the above msg."); 
     100            static assert(0, "describe called with unsupported type"); 
    103101        }        
    104102    } 
     
    123121        if (write_or_read == WriteRead.Write) 
    124122        { 
    125             // if we haven't encountered the class yet, it's an error 
    126             if(!(T.classinfo in class_register.by_classinfo)) 
    127                 throw new Exception("Class " ~ T.classinfo.name ~ " must be registered to be written."); 
     123            // check if x's type is registered 
     124            if(!(x.classinfo in class_register.by_classinfo)) 
     125            { 
     126                // if T is default constructible, not registering isn't an error 
     127                static if( is(typeof(new T)) ) 
     128                { 
     129                    // if we're not serializing through a base pointer, register! 
     130                    if(T.classinfo is x.classinfo) 
     131                        registerClass!(T)(); 
     132                    else 
     133                        throw new Exception("Trying to write unregistered class " ~ x.classinfo.name ~  
     134                                " through class reference of type " ~ T.classinfo.name ~ ".\nYou need to register the former ahead of time."); 
     135                } 
     136                else // otherwise, it is! 
     137                { 
     138                    throw new Exception("Trying to write unregistered class " ~ x.classinfo.name ~  
     139                            "\nIt is not default-constructible, so you need to register it ahead of time."); 
     140                } 
     141            } 
    128142             
    129143            char[] name = class_register.by_classinfo[x.classinfo].name; 
     
    145159                //FIXME: If throwing Exception, archive should be in ok state! 
    146160                //file.position(position); 
    147                 throw new Exception("Class must be registered to be read:\nThe name " ~ name ~ " is not known."); 
     161                 
     162                // if the name is the mangle of the class reference type we're serializing through 
     163                // it is ok to just default-register the class 
     164                if(name == T.mangleof) 
     165                { 
     166                    static if( is(typeof(new T)) ) 
     167                        registerClass!(T)(); 
     168                    else 
     169                        throw new Exception("Trying to read unregistered class - probably " ~ T.classinfo.name ~  
     170                                                             " - through a class reference of that type.\nBut " ~ T.classinfo.name ~ 
     171                                                             " is not default-constructible, so you need to register it ahead of time."); 
     172                } 
     173                else 
     174                { 
     175                    throw new Exception("Trying to read class that has been written with the " ~ name ~ " tag " ~ 
     176                            "through a class reference of type " ~ T.classinfo.name ~ "." ~ 
     177                            "\nThe tag is unknown, you need to register this tag ahead of time."); 
     178                } 
    148179            } 
    149180             
  • trunk/freeuniverse/init/init.d

    r89 r99  
    686686    gameTime = new Time(); 
    687687 
    688     InitSerialization(); 
    689      
    690688    GameObjects.init();  
    691689    //SpaceObjects = new dlinkedlist!(FU_OBJECT); 
     
    716714    arc.io.window.close(); 
    717715} 
    718  
    719 void InitSerialization() 
    720 { 
    721     Serializer.registerClass!(FU_OBJECT)(); 
    722     Serializer.registerClass!(FU_SHIP)(); 
    723     Serializer.registerClass!(FU_ITEM)(); 
    724     Serializer.registerClass!(FU_ITEMVARS)(); 
    725     Serializer.registerClass!(FU_JOB)(); 
    726     Serializer.registerClass!(FU_OBJECTIVE)(); 
    727     Serializer.registerClass!(dlinkedlist!(FU_ITEM))(); 
    728     Serializer.registerClass!(dlinkedlist!(FU_SHIP))(); 
    729     Serializer.registerClass!(dlinkedlist!(FU_OBJECT))(); 
    730     Serializer.registerClass!(dlinkedlist!(FU_JOB))(); 
    731     Serializer.registerClass!(FU_DOCKINFO)(); 
    732 }