A class that simplifies the unarchiving of complicated BArchivable hierarchies. More...
Public Types | |
enum | ownership_policy { B_ASSUME_OWNERSHIP , B_DONT_ASSUME_OWNERSHIP } |
Options for the ownership policy of objects retrieved from BUnarchiver. More... | |
Public Member Functions | |
BUnarchiver (const BMessage *archive) | |
Constructs a BUnarchiver object to manage archive . | |
~BUnarchiver () | |
Destroys a BUnarchiver object. | |
const BMessage * | ArchiveMessage () const |
Returns the BMessage* used to construct this BUnarchiver. | |
void | AssumeOwnership (BArchivable *archivable) |
Become the owner of archivable. | |
status_t | EnsureUnarchived (const char *name, int32 index=0) |
Ensure the object archived under name at index is unarchived and instantiated. | |
status_t | EnsureUnarchived (int32 token) |
Ensure the object represented by token is unarchived and instantiated. | |
template<class T > | |
status_t | FindObject (const char *name, int32 index, ownership_policy owning, T *&object) |
Recover an object that had previously been archived using the BArchiver::AddArchivable() method. | |
template<class T > | |
status_t | FindObject (const char *name, int32 index, T *&object) |
Recover and take ownership of an object that had previously been archived using the BArchiver::AddArchivable() method. | |
template<class T > | |
status_t | FindObject (const char *name, ownership_policy owning, T *&object) |
Recover an object at index 0 that had previously been archived using the BArchiver::AddArchivable() method. | |
template<class T > | |
status_t | FindObject (const char *name, T *&object) |
Recover and take ownership of an object at index 0 that had previously been archived using the BArchiver::AddArchivable() method. | |
status_t | Finish (status_t err=B_OK) |
Report any unarchiving errors and possibly complete the archiving session. | |
template<class T > | |
status_t | GetObject (int32 token, ownership_policy owning, T *&object) |
Recover an object by token that was archived by a BArchiver object. If the object has not yet been instantiated, and this request is not coming from an AllUnarchived() implementation, the object will be instantiated now. | |
template<class T > | |
status_t | GetObject (int32 token, T *&object) |
Recover and take ownership of an object represented by token. | |
bool | IsInstantiated (const char *name, int32 index=0) |
Checks whether the object archived under name at index has been instantiated in this session. | |
bool | IsInstantiated (int32 token) |
Checks whether the object represented by token has been instantiated in this session. | |
void | RelinquishOwnership (BArchivable *archivable) |
Relinquish ownership of archivable. If archivable remains unclaimed at the end of the unarchiving session, it will be deleted (unless it is the root object). | |
Static Public Member Functions | |
template<class T > | |
static status_t | InstantiateObject (BMessage *archive, T *&object) |
Attempt to instantiate an object of type T from BMessage* from. | |
static bool | IsArchiveManaged (const BMessage *archive) |
Checks whether archive was managed by a BArchiver object. | |
static BMessage * | PrepareArchive (BMessage *&archive) |
Prepares archive for use by a BUnarchiver. | |
A class that simplifies the unarchiving of complicated BArchivable hierarchies.
The BUnarchiver class is a small class used to recover BArchivable objects that have been archived with the BArchiver class. It also provides ownership semantics, so that memory leaks can be avoided during the unarchival process. When retrieving an object (either via GetObject() or FindObject()), you can specify a BUnarchiver::ownership_policy. If you specify BUnarchiver::B_ASSUME_OWNERSHIP, you will become responsible for deleting the retrieved item. If you specify BUnarchiver::B_DONT_ASSUME_OWNERSHIP, you will not become responsible. You cannot take ownership of the same object twice. After the unarchival process finishes, any unclaimed objects, excluding the root object (the object being instantiated via instantiate_object() or BUnarchiver::InstantiateObject()), will be deleted.
If you are updating a class that previously did not use the BArchiver and BUnarchiver helper classes, and want to maintain backwards compatibility with old archive, this can be done using the IsArchiveManaged() method.
Options for the ownership policy of objects retrieved from BUnarchiver.
BUnarchiver::BUnarchiver | ( | const BMessage * | archive | ) |
Constructs a BUnarchiver object to manage archive
.
archive
. It is only safe to build a BUnarchiver without this call in your AllUnarchived() implementation.BUnarchiver::~BUnarchiver | ( | ) |
Destroys a BUnarchiver object.
Calls this objects Finish() method, if it has not yet been called.
const BMessage * BUnarchiver::ArchiveMessage | ( | ) | const |
Returns the BMessage* used to construct this BUnarchiver.
This is the archive that FindObject() uses.
void BUnarchiver::AssumeOwnership | ( | BArchivable * | archivable | ) |
Become the owner of archivable.
After calling this method you are responsible for deleting the archivable.
archivable | The archivable object. |
Ensure the object archived under name at index is unarchived and instantiated.
name | The archive name. |
index | The archive index. |
References B_DONT_ASSUME_OWNERSHIP, and FindObject().
Ensure the object represented by token is unarchived and instantiated.
token | the object token |
References B_DONT_ASSUME_OWNERSHIP, and GetObject().
status_t BUnarchiver::FindObject | ( | const char * | name, |
int32 | index, | ||
ownership_policy | owning, | ||
T *& | object | ||
) |
Recover an object that had previously been archived using the BArchiver::AddArchivable() method.
If the object has not yet been instantiated, and this request is not coming from an AllUnarchived() implementation, the object will be instantiated now.
If the retrieved object is not of the type T, then this method will fail. If this method fails, you will not receive ownership of the object, no matter what you specified in owning
.
T | The type of object you wish to find. |
name | The name that was passed to BArchiver::AddArchivable() when adding this object. |
index | The index of the object you wish to recover (0-based , like BMessage::FindData(). |
owning | Dictates whether or not you wish to take ownership of the retrieved object. |
object | Return parameter for the retrieved object of type T. |
B_OK | The object retrieved was of type T. |
B_BAD_TYPE | The object retrieved was not of type T. |
References B_ASSUME_OWNERSHIP, FindObject(), NULL, and RelinquishOwnership().
|
inline |
Recover and take ownership of an object that had previously been archived using the BArchiver::AddArchivable() method.
T | The type of object you wish to find. |
name | The name that was passed to BArchiver::AddArchivable() when adding this object. |
index | The index of the object you wish to recover (0-based , like BMessage::FindData(). |
object | Return parameter for the retrieved object of type T. |
B_OK | The object retrieved was of type T. |
B_BAD_TYPE | The object retrieved was not of type T. |
References B_ASSUME_OWNERSHIP, and FindObject().
|
inline |
Recover an object at index 0
that had previously been archived using the BArchiver::AddArchivable() method.
Equivalent to calling FindObject(name, 0
, owning, object).
T | The type of object you wish to find. |
name | The name that was passed to BArchiver::AddArchivable() when adding this object. |
owning | Dictates whether or not you wish to take ownership of the retrieved object. |
object | Return parameter for the retrieved object of type T. |
B_OK | The object retrieved was of type T. |
B_BAD_TYPE | The object retrieved was not of type T. |
References FindObject().
|
inline |
Recover and take ownership of an object at index 0
that had previously been archived using the BArchiver::AddArchivable() method.
Equivalent to calling FindObject(name, 0
, BUnarchiver::B_ASSUME_OWNERSHIP, object).
T | The type of object you wish to find. |
name | The name that was passed to BArchiver::AddArchivable() when adding this object. |
object | Return parameter for the retrieved object of type T. |
B_OK | The object retrieved was of type T. |
B_BAD_TYPE | The object retrieved was not of type T. |
References B_ASSUME_OWNERSHIP.
Referenced by EnsureUnarchived(), and FindObject().
Report any unarchiving errors and possibly complete the archiving session.
This method may finish an unarchiving session (triggering the call of all instantiated objects' AllUnarchived() methods) if the following conditions are true:
If you call this method with an error code not equal to B_OK, then this unarchiving session has failed, instantiated objects will not have their AllUnarchived() methods called, and any subsequent calls to this method on any BUnarchiver objects in this session will return your error code. Furthermore, any objects that have been instantiated, but have not had their ownership assumed by another object will now be deleted (excluding the root object).
B_OK
.status_t BUnarchiver::GetObject | ( | int32 | token, |
ownership_policy | owning, | ||
T *& | object | ||
) |
Recover an object by token that was archived by a BArchiver object. If the object has not yet been instantiated, and this request is not coming from an AllUnarchived() implementation, the object will be instantiated now.
If the retrieved object is not of the type T, then this method will fail. If this method fails, you will not receive ownership of the object, no matter what you specified in owning
.
T | The type of object you wish to find. |
token | The token you got for this object from BArchiver::GetTokenForArchivable() during archival. |
owning | Whether or not you wish to take ownership of the retrieved object. |
object | Return parameter for the retrieved object of type T. |
B_OK | The object retrieved was of type T. |
B_BAD_TYPE | The object retrieved was not of type T. |
References B_ASSUME_OWNERSHIP, GetObject(), NULL, and RelinquishOwnership().
Recover and take ownership of an object represented by token.
Equivalent to calling GetObject(token, B_ASSUME_OWNERSHIP
, object)
T | The type of object you wish to find. |
token | The token you got for this object from BArchiver::GetTokenForArchivable() during archival. |
object | The return parameter for the retrieved object of type T. |
B_OK | The object retrieved was of type T. |
B_BAD_TYPE | The object retrieved was not of type T. |
References B_ASSUME_OWNERSHIP.
Referenced by EnsureUnarchived(), and GetObject().
Attempt to instantiate an object of type T from BMessage* from.
If the instantiated object is not of type T, then it will be deleted, and this method will return B_BAD_TYPE
. This method is similar to the instantiate_object() function, but provides error reporting and protection from memory leaks.
from | The BMessage to instantiate from. |
object | Return parameter for the retrieved object of type T. |
B_OK | The object retrieved was of type T. |
B_BAD_TYPE | The object retrieved was not of type T. |
References InstantiateObject(), and NULL.
Referenced by InstantiateObject().
|
static |
Checks whether archive was managed by a BArchiver object.
This method can be used to maintain archive backwards-compatibility for a class that has been updated to use the BArchiver class. If there is a possibility that you are may dealing with a legacy archive, you can use this method to find out before calling any methods on your BUnarchiver object.
Here is an example of how you might use this method. Note that you must still call PrepareArchive(archive) either way.
true | if archive was managed by a BArchiver object. |
false | otherwise. |
bool BUnarchiver::IsInstantiated | ( | const char * | name, |
int32 | index = 0 |
||
) |
Checks whether the object archived under name at index has been instantiated in this session.
name | The archive name. |
index | The arcive token. |
true
if instantiated, false
otherwise.bool BUnarchiver::IsInstantiated | ( | int32 | token | ) |
Checks whether the object represented by token
has been instantiated in this session.
token | The object token. |
true
if instantiated, false
otherwisePrepares archive
for use by a BUnarchiver.
This method must be called if you plan to use a BUnarchiver on an archive. It must be called once for each class an object inherits from that will use a BUnarchiver.
Notice the use of this method in the example provided below.
archive | The archive you wish to have prepared. |
void BUnarchiver::RelinquishOwnership | ( | BArchivable * | archivable | ) |
Relinquish ownership of archivable. If archivable remains unclaimed at the end of the unarchiving session, it will be deleted (unless it is the root object).
archivable | The archivable object. |
Referenced by FindObject(), and GetObject().