xHarbour Reference Documentation > Class Reference (textmode) xHarbour Developers Network  

HBPersistent()

Abstract base class for user-defined persistent object.

Description

The HBPersistent() class is an abstract class for the creation of persistent objects. When user-defined classes should become persistent, they must inherit from HBPersistent() which provides methods for converting an object to/from a binary string and/or file on disk.

All instance variables declared with the PERSISTENT attribute are preserved and re-assigned when a persistent object is converted to a binary string and back to the Object data type.

Note:  if code blocks are stored in instance variables of a persistent object, the same restrictions apply as outlined with function HB_Serialize().

Methods for saving an object

:saveToFile( <cFileName> ) --> lSuccess
Writes the persistent object data to a file.
:saveToText() --> cBinaryString
Creates a binary string from the data held in an object.

Methods for restoring an object

:loadFromFile( <cFileName> ) --> lSuccess
Loads persistent object from a file.
:loadFromText( <cBinaryString> ) --> lSuccess
Copies data held in a binary string to an object.

Info

See also:CLASS, DATA, HB_Serialize()
Category: Object functions , xHarbour extensions
Source:rtl\persist.prg
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example demonstrates the steps required to make objects
// persistent. Only the instance variables with the PERSISTENT
// attribute are written to and restored from the file.

   #include "HbClass.ch"

   PROCEDURE Main
      LOCAL obj, cPersistent

      obj := test():new()
      obj:volatile := "Volatile"

      obj:show()

      obj:value := "Data from file"


      obj:saveToFile( "Persist.txt" )

      obj := NIL

      WAIT "obj is gone, press a key..."

      obj := Test():new()
      obj:loadFromFile( "Persist.txt" )

      obj:show()

      ? "Elapsed:",  Seconds()-obj:secs
   RETURN


   CLASS Test FROM HBPersistent
      DATA value    INIT "Test persistent Object"  PERSISTENT
      DATA date     INIT Date()                    PERSISTENT
      DATA time     INIT Time()                    PERSISTENT
      DATA secs     INIT Seconds()                 PERSISTENT
      DATA volatile
      METHOD show
   ENDCLASS

   METHOD Show
      ? ::value
      ? "Creation Date:", ::date
      ? "Creation Time:", ::time
      ? "    Timestamp:", ::secs
      ? "Volatile data:", ::volatile
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe