Public Member Functions | Static Public Member Functions

BArchivable Class Reference

Interface for objects that can be archived into a BMessage. More...

Inheritance diagram for BArchivable:
BBitmap BCollator BHandler BLayoutItem BLooper BView BLayout BApplication BBox BControl BScrollView BAbstractLayout BButton BChannelControl BCheckBox BColorControl BTwoDimensionalLayout BGridLayout BGroupLayout

List of all members.

Public Member Functions

 BArchivable (BMessage *from)
 Constructor. Does important behind-the-scenes work in the unarchiving process.
 BArchivable ()
 Constructor. Does nothing.
virtual ~BArchivable ()
 Destructor. Does nothing.
virtual status_t AllArchived (BMessage *archive) const
 Method relating to the use of BArchiver.
virtual status_t AllUnarchived (const BMessage *archive)
 Method relating to the use of BUnarchiver.
virtual status_t Archive (BMessage *into, bool deep=true) const
 Archive the object into a BMessage.
virtual status_t Perform (perform_code d, void *arg)
 Perform some action (Internal method defined for binary compatibility purposes).

Static Public Member Functions

static BArchivableInstantiate (BMessage *archive)
 Static member to restore objects from messages.

Detailed Description

Interface for objects that can be archived into a BMessage.

BArchivable provides an interface for objects that can be put into message archives and extracted into objects in another location. Using this you are able to send objects between applications, or even between computers across networks.

BArchivable differs from BFlattenable in that BFlattenable is designed to store objects into flat streams of data, the main objective being storage to disk. The objective of this interface, however, is to store objects that will later be restored as new (but identical) objects. To illustrate this point, BArchivable objects can be restored automatically to the correct class, whereas BFlattenables have a data type which you need to map to classes manually.

Archiving is done with the Archive() method. If your class supports it, the caller can request it to store into a deep archive, meaning that all child objects in it will be stored. Extracting the archive works with the Instantiate() method, which is static. Since the interface is designed to extract objects without the caller knowing what kind of object it actually is, the global function instantiate_object() instantiates a message without you manually having to determine the class the message is from. This adds considerable flexibility and allows BArchivable to be used in combination with other add-ons.

To provide this interface in your classes you should publicly inherit this class. You should implement Archive() and Instantiate(), and provide one constructor that takes one BMessage argument.

If your class holds references to other BArchivable objects that you wish to archive, then you should consider using the BArchiver and BUnarchiver classes in your Archive() method and archive constructor, respectively. You should also consider implementing the AllArchived() and AllUnarchived() methods, which were designed to ease archiving and unarchiving in such a situation.


Constructor & Destructor Documentation

BArchivable::BArchivable ( BMessage from)

Constructor. Does important behind-the-scenes work in the unarchiving process.

If you inherit this interface you should provide at least one constructor that takes one BMessage argument. In that constructor, you should call your parent class' archive constructor (even if your parent class is BArchivable).


Member Function Documentation

status_t BArchivable::AllArchived ( BMessage into) const [virtual]

Method relating to the use of BArchiver.

This hook function is called once the first BArchiver that was created in an archiving session is either destroyed, or has its Finish() method called. Implementations of this method can be used, in conjunction with BArchiver::IsArchived(), to reference objects in your archive that you do not own, depending on whether or not those objects were archived by their owners. Implementations of this method should call the implementation of their parent class, the same as for the Archive() method.

Warning:
To guarantee that your AllArchived() method will be called during archival, you must create a BArchiver object in your Archive() implementation.
You should archive any objects you own in your Archive() method implementation, and NOT your AllArchived() method.
See also:
BArchiver BArchiver::Finish()

Reimplemented in BAbstractLayout, BGridLayout, BGroupLayout, BLayout, BLayoutItem, BTwoDimensionalLayout, and BView.

status_t BArchivable::AllUnarchived ( const BMessage archive) [virtual]

Method relating to the use of BUnarchiver.

This hook function is called triggered in the BUnarchiver::Finish() method. In this method, you can rebuild references to objects that may be direct children of your object, or may be children of other objects. Implementations of this method should call the implementation of their parent class, the same as for the Archive() method.

Warning:
To guarantee that your AllUnarchived() method will be called during unarchival, you must create a BUnarchiver object in your archive constructor.
See also:
BUnarchiver, BUnarchiver::Finish()

Reimplemented in BAbstractLayout, BGridLayout, BGroupLayout, BLayout, BLayoutItem, BTwoDimensionalLayout, and BView.

status_t BArchivable::Archive ( BMessage into,
bool  deep = true 
) const [virtual]

Archive the object into a BMessage.

You should call this method from your derived implementation as it adds the data needed to instantiate your object to the message.

Parameters:
intoThe message you store your object in.
deepIf true, all children of this object should be archived as well.
Return values:
B_OKThe archive operation was successful.
B_BAD_VALUENULL archive message.
B_ERRORThe archive operation failed.

Reimplemented in BApplication, BHandler, BLooper, BAbstractLayout, BAlert, BBitmap, BBox, BButton, BChannelControl, BCheckBox, BColorControl, BControl, BGridLayout, BGroupLayout, BLayout, BLayoutItem, BScrollView, BTwoDimensionalLayout, BView, and BCollator.

static BArchivable * BArchivable::Instantiate ( BMessage archive) [static]

Static member to restore objects from messages.

You should always check that the archive argument actually corresponds to your class. The automatic functions, such as instantiate_object() and BUnarchiver::InstantiateObject() will not choose the wrong class but manual calls to this member might be faulty. You can verify that archive stores an object of your calss with the validate_instantiation() function.

Parameters:
archiveThe message with the data of the object to restore.
Return values:
Youshould return a pointer to the object you create with archive, or NULL if unarchival fails.
Warning:
The default implementation will always return NULL. Even though it is possible to store plain BArchivable objects, it is impossible to restore them.
See also:
instantiate_object(BMessage *from)
BUnarchiver::InstantiateObject()

Reimplemented in BApplication, BHandler, BLooper, BAlert, BBitmap, BBox, BButton, BCheckBox, BColorControl, BControl, BGridLayout, BGroupLayout, BScrollView, BView, and BCollator.


The Haiku Book pre-R1 - BArchivable Class Reference
Generated on Sat May 12 2012 by Doxygen 1.7.3