  
  [1X5 Object serialisation (Pickling)[0X
  
  The  idea  of  "object  serialisation"  is  that  one  wants to store nearly
  arbitrary  [5XGAP[0m  objects  to  disk or transfer them over the network. To this
  end, one wants to convert them to a byte stream that is platform independent
  and  can  later be converted back to a copy of the same object in memory, be
  it in the same [5XGAP[0m process or another one maybe even on another machine. The
  main  problem  here  are the vast amount of different types occurring in [5XGAP[0m
  and the possibly highly self-referential structure of [5XGAP[0m objects.
  
  The  [5XIO[0m  package  contains a framework to implement object serialisation and
  implementations  for  most  of the basic data types in [5XGAP[0m. The framework is
  easily  extendible to other types and takes complete care of self-references
  and  corresponding  problems.  It  builds  upon  the  buffered I/O functions
  described in Section [14X4[0m. We start by describing the user interface.
  
  
  [1X5.1 Result objects[0X
  
  The  following static objects are used to report about success or failure of
  the (un-)pickling operations:
  
  [1X5.1-1 IO_Error[0m
  
  [2X> IO_Error___________________________________________________[0Xglobal variable
  
  This object is returned if an error occurs.
  
  [1X5.1-2 IO_Nothing[0m
  
  [2X> IO_Nothing_________________________________________________[0Xglobal variable
  
  This  object  is returned when there is nothing to return, for example if an
  unpickler (see [2XIO_Unpickle[0m ([14X5.2-2[0m)) encounters the end of a file.
  
  [1X5.1-3 IO_OK[0m
  
  [2X> IO_OK______________________________________________________[0Xglobal variable
  
  This  object  is  returned  if  everything  went  well and there is no other
  canonical value to return to indicate this.
  
  The  only  thing  you can do with these special values is to compare them to
  each other and to other objects.
  
  
  [1X5.2 Pickling and unpickling[0X
  
  [1X5.2-1 IO_Pickle[0m
  
  [2X> IO_Pickle( [0X[3Xf, ob[0X[2X ) ______________________________________________[0Xoperation
  [6XReturns:[0X  [10XIO_OK[0m or [10XIO_Error[0m
  
  The  argument  [3Xf[0m must be an open, writable [10XFile[0m object. The object [3Xob[0m can be
  an  arbitrary [5XGAP[0m object. The operation "pickles" or "serialises" the object
  [3Xob[0m  and  writes  the result into the [10XFile[0m object [3Xf[0m. If everything is OK, the
  unique  value [10XIO_OK[0m is returned and otherwise the unique value [10XIO_Error[0m. The
  resulting  byte  stream  can  be  read again using the operation [2XIO_Unpickle[0m
  ([14X5.2-2[0m)  and  is  platform-  and  architecture  independent.  Especially the
  question  whether  a system has 32 bit or 64 bit wide words and the question
  of endianess does not matter.
  
  Note  that  not all of [5XGAP[0m's object types are supported but it is relatively
  easy  to  extend  the  system.  This  package supports in particular boolean
  values,  integers,  permutations,  rational  numbers, finite field elements,
  cyclotomics,  strings,  polynomials,  rational  functions,  lists,  records,
  compressed vectors and matrices over finite fields (objects are uncompressed
  in  the  byte  stream but recompressed during unpickling), and straight line
  programs.
  
  Self-referential  objects built from records and lists are handled correctly
  and are restored completely with the same self-references during unpickling.
  
  [1X5.2-2 IO_Unpickle[0m
  
  [2X> IO_Unpickle( [0X[3Xf[0X[2X ) ________________________________________________[0Xoperation
  [6XReturns:[0X  [10XIO_Error[0m or a [5XGAP[0m object
  
  The  argument  [3Xf[0m  must be an open, readable [10XFile[0m object. The operation reads
  from [3Xf[0m and "unpickles" the next object. If an error occurs, the unique value
  [10XIO_Error[0m  is  returned.  If  the  [10XFile[0m  object  is at end of file, the value
  [10XIO_Nothing[0m  is  returned.  Note  that  these  two  values are not picklable,
  because of their special meaning as return values of this operation here.
  
  [1X5.2-3 IO_ClearPickleCache[0m
  
  [2X> IO_ClearPickleCache( [0X[3X[0X[2X ) __________________________________________[0Xfunction
  [6XReturns:[0X  Nothing
  
  This  function  clears  the  "pickle  cache".  This  cache stores all object
  pickled  in the current recursive call to [2XIO_Pickle[0m ([14X5.2-1[0m) and is necessary
  to handle self-references. Usually it is not necessary to call this function
  explicitly.  Only  in the rare case (that should not happen) that a pickling
  or  unpickling  operation enters a break loop which is left by the user, the
  pickle  cache  has  to  be  cleared explicitly using this function for later
  calls to [2XIO_Pickle[0m ([14X5.2-1[0m) and [2XIO_Unpickle[0m ([14X5.2-2[0m) to work!
  
  
  [1X5.3 Extending the pickling framework[0X
  
  The framework can be extended for other [5XGAP[0m object types as follows:
  
  For  pickling,  a  method  for  the  operation  [2XIO_Pickle[0m  ([14X5.2-1[0m) has to be
  installed  which  does the work. If the object to be pickled has subobjects,
  then  the first action of the method is to call the function [10XIO_AddToPickled[0m
  with the object as argument. This will put it into the pickle cache and take
  care  of  self-references.  Arbitrary  subobjects  can then be pickled using
  recursive  calls  to  the  operation [2XIO_Pickle[0m ([14X5.2-1[0m) handing down the same
  [10XFile[0m  object  into  the recursion. The method must either return [10XIO_Error[0m in
  case  of  an  error  or  [10XIO_OK[0m  if everything goes well. Before returning, a
  method   that   has   called   [10XIO_AddToPickled[0m   must   call   the  function
  [10XIO_FinalizePickled[0m  without  arguments [13Xunder all circumstances[0m. If this call
  is missing, global data for the pickling procedure becomes corrupt!
  
  Every  pickling method must first write a 4 byte magic value such that later
  during  unpickling  of  the  byte  stream the right unpickling method can be
  called  (see  below).  Then  it can write arbitrary data, however, this data
  should be platform- and architecture independent, and it must be possible to
  unpickle it later without "lookahead".
  
  Pickling  methods  should  usually  not  go into a break loop, because after
  leaving the user has to call [2XIO_ClearPickleCache[0m ([14X5.2-3[0m) explicitly!
  
  Unpickling  is  implemented  as  follows: For every 4 byte magic value there
  must  be  a function bound to that value in the record [10XIO_Unpicklers[0m. If the
  unpickling  operation  [2XIO_Unpickle[0m  ([14X5.2-2[0m)  encounters that magic value, it
  calls  the  corresponding  unpickling  function. This function just gets one
  [10XFile[0m  object  as  argument.  Since  the  magic value is already read, it can
  immediately  start  with  reading  and  rebuilding  the serialised object in
  memory. The method has to take care to restore the object including its type
  completely.
  
  If  an  object  type  has  subobjects,  the unpickling function has to first
  create   a  skeleton  of  the  object  without  its  subobjects,  then  call
  [10XIO_AddToUnpickled[0m  on this skeleton, [13Xbefore[0m unpickling subobjects. If things
  are  not  done  in  this  order, the handling of self-references down in the
  recursion   will   not   work!   An  unpickling  function  that  has  called
  [10XIO_AddToUnpickled[0m  at the beginning has to call [10XIO_FinalizeUnpickled[0m without
  arguments before returning [13Xunder all circumstances[0m! If this call is missing,
  global data for the unpickling procedure becomes corrupt!
  
  Of  course, unpickling functions can recursively call [2XIO_Unpickle[0m ([14X5.2-2[0m) to
  unpickle subobjects. Apart from this, unpickling functions can use arbitrary
  reading  functions  on  the  [10XFile[0m  object.  However,  they  should only read
  sequentially  and never move the current file position pointer otherwise. An
  unpickling  function  should  return  the  newly created object or the value
  [10XIO_Error[0m  if  an  error  occurred.  They  should never go into a break loop,
  because  after  leaving  the  user  has  to call [2XIO_ClearPickleCache[0m ([14X5.2-3[0m)
  explicitly!
  
  Perhaps  the  best  way to learn how to extend the framework is to study the
  code for the basic [5XGAP[0m objects in the file [11Xpkg/io/gap/pickle.gi[0m.
  
