181071f5eSNiels Sascha Reedijk/* 2820dca4dSJohn Scipione * Copyright 2007 Haiku, Inc. All rights reserved. 381071f5eSNiels Sascha Reedijk * Distributed under the terms of the MIT License. 481071f5eSNiels Sascha Reedijk * 5886c23bfSAlex Wilson * Authors: 655f7db1bSNiels Sascha Reedijk * Niels Sascha Reedijk, niels.reedijk@gmail.com 7886c23bfSAlex Wilson * Alex Wilson, yourpalal2@gmail.com 8c4b9309aSJohn Scipione * John Scipione, jscipione@gmail.com 9e3c5ca85SNiels Sascha Reedijk * 1037503f54SNiels Sascha Reedijk * Proofreader: 1155f7db1bSNiels Sascha Reedijk * David Weizades, ddewbofh@hotmail.com 1255f7db1bSNiels Sascha Reedijk * Thom Holwerda, slakje@quicknet.nl 13e3c5ca85SNiels Sascha Reedijk * 1481071f5eSNiels Sascha Reedijk * Corresponds to: 15820dca4dSJohn Scipione * headers/os/support/Archivable.h rev 37751 16b7235efcSNiels Sascha Reedijk * src/kits/support/Archivable.cpp rev 44303 1781071f5eSNiels Sascha Reedijk */ 1881071f5eSNiels Sascha Reedijk 1955f7db1bSNiels Sascha Reedijk 20820dca4dSJohn Scipione/*! 21820dca4dSJohn Scipione \file Archivable.h 22820dca4dSJohn Scipione \ingroup support 2341611c9cSJohn Scipione \ingroup libbe 24886c23bfSAlex Wilson \brief Provides the BArchivable interface and declares the BArchiver and 25886c23bfSAlex Wilson BUnarchiver classes. 2681071f5eSNiels Sascha Reedijk*/ 2781071f5eSNiels Sascha Reedijk 2855f7db1bSNiels Sascha Reedijk 29820dca4dSJohn Scipione/*! 30820dca4dSJohn Scipione \class BArchivable 3181071f5eSNiels Sascha Reedijk \ingroup support 3241611c9cSJohn Scipione \ingroup libbe 33e3c5ca85SNiels Sascha Reedijk \brief Interface for objects that can be archived into a BMessage. 3481071f5eSNiels Sascha Reedijk 3537503f54SNiels Sascha Reedijk BArchivable provides an interface for objects that can be put into message 3637503f54SNiels Sascha Reedijk archives and extracted into objects in another location. Using this you are 3737503f54SNiels Sascha Reedijk able to send objects between applications, or even between computers across 3881071f5eSNiels Sascha Reedijk networks. 3981071f5eSNiels Sascha Reedijk 4037503f54SNiels Sascha Reedijk BArchivable differs from BFlattenable in that BFlattenable is designed to 4137503f54SNiels Sascha Reedijk store objects into flat streams of data, the main objective being storage to 4255f7db1bSNiels Sascha Reedijk disk. The objective of this interface, however, is to store objects that 43886c23bfSAlex Wilson will later be restored as new (but identical) objects. To illustrate this 44886c23bfSAlex Wilson point, BArchivable objects can be restored automatically to the correct 45820dca4dSJohn Scipione class, whereas BFlattenable objects have a data type which you need to map 46820dca4dSJohn Scipione to classes manually. 4781071f5eSNiels Sascha Reedijk 4837503f54SNiels Sascha Reedijk Archiving is done with the Archive() method. If your class supports it, the 4937503f54SNiels Sascha Reedijk caller can request it to store into a deep archive, meaning that all child 5037503f54SNiels Sascha Reedijk objects in it will be stored. Extracting the archive works with the 5137503f54SNiels Sascha Reedijk Instantiate() method, which is static. Since the interface is designed to 5255f7db1bSNiels Sascha Reedijk extract objects without the caller knowing what kind of object it actually 5355f7db1bSNiels Sascha Reedijk is, the global function #instantiate_object() instantiates a message without 5455f7db1bSNiels Sascha Reedijk you manually having to determine the class the message is from. This adds 5555f7db1bSNiels Sascha Reedijk considerable flexibility and allows BArchivable to be used in combination 5655f7db1bSNiels Sascha Reedijk with other add-ons. 5781071f5eSNiels Sascha Reedijk 5837503f54SNiels Sascha Reedijk To provide this interface in your classes you should publicly inherit this 5937503f54SNiels Sascha Reedijk class. You should implement Archive() and Instantiate(), and provide one 6081071f5eSNiels Sascha Reedijk constructor that takes one BMessage argument. 61886c23bfSAlex Wilson 62886c23bfSAlex Wilson If your class holds references to other BArchivable objects that you wish 63886c23bfSAlex Wilson to archive, then you should consider using the BArchiver and BUnarchiver 64886c23bfSAlex Wilson classes in your Archive() method and archive constructor, respectively. 65886c23bfSAlex Wilson You should also consider implementing the AllArchived() and AllUnarchived() 66886c23bfSAlex Wilson methods, which were designed to ease archiving and unarchiving in such 67886c23bfSAlex Wilson a situation. 68c4b9309aSJohn Scipione 69c4b9309aSJohn Scipione \since BeOS R3 7081071f5eSNiels Sascha Reedijk*/ 7181071f5eSNiels Sascha Reedijk 7255f7db1bSNiels Sascha Reedijk 736ac7032dSJohn Scipione/*! 746ac7032dSJohn Scipione \fn BArchivable::BArchivable(BMessage* from) 75820dca4dSJohn Scipione \brief Constructor. Does important behind-the-scenes work in the 76820dca4dSJohn Scipione unarchiving process. 7781071f5eSNiels Sascha Reedijk 7855f7db1bSNiels Sascha Reedijk If you inherit this interface you should provide at least one constructor 79820dca4dSJohn Scipione that takes one BMessage argument. In that constructor, you should call 80820dca4dSJohn Scipione your parent class' archive constructor (even if your parent class is 81886c23bfSAlex Wilson BArchivable). 82c4b9309aSJohn Scipione 83c4b9309aSJohn Scipione \since BeOS R3 8481071f5eSNiels Sascha Reedijk*/ 8581071f5eSNiels Sascha Reedijk 8655f7db1bSNiels Sascha Reedijk 876ac7032dSJohn Scipione/*! 886ac7032dSJohn Scipione \fn BArchivable::BArchivable() 8981071f5eSNiels Sascha Reedijk \brief Constructor. Does nothing. 90c4b9309aSJohn Scipione 91c4b9309aSJohn Scipione \since BeOS R3 9281071f5eSNiels Sascha Reedijk*/ 9381071f5eSNiels Sascha Reedijk 9455f7db1bSNiels Sascha Reedijk 956ac7032dSJohn Scipione/*! 966ac7032dSJohn Scipione \fn BArchivable::~BArchivable() 9781071f5eSNiels Sascha Reedijk \brief Destructor. Does nothing. 98c4b9309aSJohn Scipione 99c4b9309aSJohn Scipione \since BeOS R3 10081071f5eSNiels Sascha Reedijk*/ 10181071f5eSNiels Sascha Reedijk 10255f7db1bSNiels Sascha Reedijk 1036ac7032dSJohn Scipione/*! 104c4b9309aSJohn Scipione \fn virtual status_t BArchivable::Archive(BMessage* into, bool deep) const 10581071f5eSNiels Sascha Reedijk \brief Archive the object into a BMessage. 10681071f5eSNiels Sascha Reedijk 10737503f54SNiels Sascha Reedijk You should call this method from your derived implementation as it adds the 10837503f54SNiels Sascha Reedijk data needed to instantiate your object to the message. 10981071f5eSNiels Sascha Reedijk 11037503f54SNiels Sascha Reedijk \param into The message you store your object in. 111886c23bfSAlex Wilson \param deep If \c true, all children of this object should be archived as 112886c23bfSAlex Wilson well. 1136ac7032dSJohn Scipione 1146ac7032dSJohn Scipione \retval B_OK The archive operation was successful. 1156ac7032dSJohn Scipione \retval B_BAD_VALUE \c NULL \a archive message. 1166ac7032dSJohn Scipione \retval B_ERROR The archive operation failed. 117c4b9309aSJohn Scipione 118c4b9309aSJohn Scipione \since BeOS R3 11981071f5eSNiels Sascha Reedijk*/ 12081071f5eSNiels Sascha Reedijk 12155f7db1bSNiels Sascha Reedijk 1226ac7032dSJohn Scipione/*! 1236ac7032dSJohn Scipione \fn static BArchivable* BArchivable::Instantiate(BMessage* archive) 12481071f5eSNiels Sascha Reedijk \brief Static member to restore objects from messages. 12581071f5eSNiels Sascha Reedijk 12681071f5eSNiels Sascha Reedijk You should always check that the \a archive argument actually corresponds to 127886c23bfSAlex Wilson your class. The automatic functions, such as #instantiate_object() and 128886c23bfSAlex Wilson BUnarchiver::InstantiateObject() will not choose the wrong class but manual 129886c23bfSAlex Wilson calls to this member might be faulty. You can verify that \c archive 130c4b9309aSJohn Scipione stores an object of your class with the validate_instantiation() function. 13181071f5eSNiels Sascha Reedijk 13237503f54SNiels Sascha Reedijk \param archive The message with the data of the object to restore. 133c4b9309aSJohn Scipione 134c4b9309aSJohn Scipione \return A pointer to a BArchivable object. 135886c23bfSAlex Wilson \retval You should return a pointer to the object you create with 136c4b9309aSJohn Scipione \c archive, or \c NULL if the unarchival fails. 137c4b9309aSJohn Scipione 13881071f5eSNiels Sascha Reedijk \warning The default implementation will always return \c NULL. Even though 139a46d62a2SAlex Wilson it is possible to store plain BArchivable objects, it is impossible to 14055f7db1bSNiels Sascha Reedijk restore them. 141886c23bfSAlex Wilson 142c4b9309aSJohn Scipione \see instantiate_object(BMessage*) 143886c23bfSAlex Wilson \see BUnarchiver::InstantiateObject() 144c4b9309aSJohn Scipione 145c4b9309aSJohn Scipione \since BeOS R3 14681071f5eSNiels Sascha Reedijk*/ 14781071f5eSNiels Sascha Reedijk 14855f7db1bSNiels Sascha Reedijk 1496ac7032dSJohn Scipione/*! 1506ac7032dSJohn Scipione \fn virtual status_t BArchivable::Perform(perform_code d, void* arg) 1516ac7032dSJohn Scipione \brief Perform some action (Internal method defined for binary 1526ac7032dSJohn Scipione compatibility purposes). 1536ac7032dSJohn Scipione 154886c23bfSAlex Wilson \internal This method is defined for binary compatibility purposes, it is 155886c23bfSAlex Wilson used to ensure that the correct AllUnarchived() and AllArchived() 156c4b9309aSJohn Scipione methods are called for objects, as those methods are new to 157c4b9309aSJohn Scipione Haiku. 1586ac7032dSJohn Scipione 1596ac7032dSJohn Scipione \param d The perform code. 1606ac7032dSJohn Scipione \param arg A pointer to store some data. 1616ac7032dSJohn Scipione 1626ac7032dSJohn Scipione \returns A status code. 163c4b9309aSJohn Scipione 164c4b9309aSJohn Scipione \since Haiku R1 165886c23bfSAlex Wilson*/ 166886c23bfSAlex Wilson 167886c23bfSAlex Wilson 1686ac7032dSJohn Scipione/*! 1696ac7032dSJohn Scipione \fn virtual status_t BArchivable::AllUnarchived(const BMessage* archive) 170886c23bfSAlex Wilson \brief Method relating to the use of \c BUnarchiver. 171886c23bfSAlex Wilson 172886c23bfSAlex Wilson This hook function is called triggered in the BUnarchiver::Finish() method. 173886c23bfSAlex Wilson In this method, you can rebuild references to objects that may be direct 174886c23bfSAlex Wilson children of your object, or may be children of other objects. 175886c23bfSAlex Wilson Implementations of this method should call the implementation of 176886c23bfSAlex Wilson their parent class, the same as for the Archive() method. 177886c23bfSAlex Wilson 178a33f8fbdSAdrien Destugues \warning To guarantee that your AllUnarchived() method will be called 179a33f8fbdSAdrien Destugues during unarchival, you must create a BUnarchiver object in your 180a33f8fbdSAdrien Destugues archive constructor. 181886c23bfSAlex Wilson 182886c23bfSAlex Wilson \see BUnarchiver, BUnarchiver::Finish() 183c4b9309aSJohn Scipione 184c4b9309aSJohn Scipione \since Haiku R1 185886c23bfSAlex Wilson*/ 186886c23bfSAlex Wilson 187886c23bfSAlex Wilson 188a33f8fbdSAdrien Destugues/*! \fn virtual status_t BArchivable::AllArchived(BMessage* into) const 189886c23bfSAlex Wilson \brief Method relating to the use of \c BArchiver. 190886c23bfSAlex Wilson 191886c23bfSAlex Wilson This hook function is called once the first BArchiver that was created in 192a33f8fbdSAdrien Destugues an archiving session is either destroyed, or has its Finish() method 193886c23bfSAlex Wilson called. Implementations of this method can be used, in conjunction with 194886c23bfSAlex Wilson BArchiver::IsArchived(), to reference objects in your archive that you 195886c23bfSAlex Wilson do not own, depending on whether or not those objects were archived by their 196886c23bfSAlex Wilson owners. Implementations of this method should call the implementation of 197886c23bfSAlex Wilson their parent class, the same as for the Archive() method. 198886c23bfSAlex Wilson 199a33f8fbdSAdrien Destugues \warning To guarantee that your AllArchived() method will be called 200a33f8fbdSAdrien Destugues during archival, you must create a BArchiver object in your 201a33f8fbdSAdrien Destugues Archive() implementation. 202886c23bfSAlex Wilson 203a33f8fbdSAdrien Destugues \warning You should archive any objects you own in your Archive() 204a33f8fbdSAdrien Destugues method implementation, and \b NOT your AllArchived() method. 205886c23bfSAlex Wilson 206886c23bfSAlex Wilson \see BArchiver BArchiver::Finish() 207c4b9309aSJohn Scipione 208c4b9309aSJohn Scipione \since Haiku R1 20981071f5eSNiels Sascha Reedijk*/ 21081071f5eSNiels Sascha Reedijk 21155f7db1bSNiels Sascha Reedijk 2120e1ec24aSNiels Sascha Reedijk///// BArchiver ///// 2130e1ec24aSNiels Sascha Reedijk 2140e1ec24aSNiels Sascha Reedijk 2150e1ec24aSNiels Sascha Reedijk/*! 2160e1ec24aSNiels Sascha Reedijk \class BArchiver 2170e1ec24aSNiels Sascha Reedijk \ingroup support 2180e1ec24aSNiels Sascha Reedijk \ingroup libbe 2190e1ec24aSNiels Sascha Reedijk \brief A class that simplifies the archiving of complicated BArchivable 2200e1ec24aSNiels Sascha Reedijk hierarchies. 2210e1ec24aSNiels Sascha Reedijk 2220e1ec24aSNiels Sascha Reedijk The BArchiver class is a small class that is used for archiving of 2230e1ec24aSNiels Sascha Reedijk complicated BArchivable hierarchies. Such a hierarchy may include 2240e1ec24aSNiels Sascha Reedijk multiple BArchivable objects, each of which might be referenced by 2250e1ec24aSNiels Sascha Reedijk many BArchivable objects. With the BArchiver class, you can be certain 2260e1ec24aSNiels Sascha Reedijk that each BArchivable object is archived only once with very little work. 2270e1ec24aSNiels Sascha Reedijk When used in conjuction with the BArchivable::AllArchived() and 2280e1ec24aSNiels Sascha Reedijk BArchivable::AllUnarchived() methods, it is simple to rebuild your system of 2290e1ec24aSNiels Sascha Reedijk references upon unarchival so that they are equivalent to those that were 2300e1ec24aSNiels Sascha Reedijk present in your original hierarchy. 2310e1ec24aSNiels Sascha Reedijk 2320e1ec24aSNiels Sascha Reedijk The objects you archive can be retrieved using a BUnarchiver object. 233c4b9309aSJohn Scipione 234c4b9309aSJohn Scipione \since Haiku R1 2350e1ec24aSNiels Sascha Reedijk*/ 2360e1ec24aSNiels Sascha Reedijk 2370e1ec24aSNiels Sascha Reedijk 2380e1ec24aSNiels Sascha Reedijk/*! 2390e1ec24aSNiels Sascha Reedijk \fn BArchiver::BArchiver(BMessage* archive) 2400e1ec24aSNiels Sascha Reedijk \brief Constructs a BArchiver object that manages \c archive. 241c4b9309aSJohn Scipione 242c4b9309aSJohn Scipione \since Haiku R1 2430e1ec24aSNiels Sascha Reedijk*/ 2440e1ec24aSNiels Sascha Reedijk 2450e1ec24aSNiels Sascha Reedijk 2460e1ec24aSNiels Sascha Reedijk/*! 2470e1ec24aSNiels Sascha Reedijk \fn BArchiver::~BArchiver() 2480e1ec24aSNiels Sascha Reedijk \brief Destroys a BArchiver object. If the BArchiver object has not had its 2490e1ec24aSNiels Sascha Reedijk Finish() method called, this will be done now. 250c4b9309aSJohn Scipione 251c4b9309aSJohn Scipione \since Haiku R1 2520e1ec24aSNiels Sascha Reedijk*/ 2530e1ec24aSNiels Sascha Reedijk 2540e1ec24aSNiels Sascha Reedijk 2550e1ec24aSNiels Sascha Reedijk/*! 2560e1ec24aSNiels Sascha Reedijk \fn status_t BArchiver::AddArchivable(const char* name, 2570e1ec24aSNiels Sascha Reedijk BArchivable* archivable, bool deep = true) 2580e1ec24aSNiels Sascha Reedijk \brief Adds a reference to \c archivable to the archive used to 2590e1ec24aSNiels Sascha Reedijk construct this BArchiver. May call \c archivable's Archive() method. 2600e1ec24aSNiels Sascha Reedijk 2610e1ec24aSNiels Sascha Reedijk \param name Where this reference will be stored in the archive. 2620e1ec24aSNiels Sascha Reedijk \param archivable The BArchivable* object that to reference. 2630e1ec24aSNiels Sascha Reedijk \param deep Passed to \c archivable->Archive() if \c archivable must 2640e1ec24aSNiels Sascha Reedijk be archived. 2650e1ec24aSNiels Sascha Reedijk 2660e1ec24aSNiels Sascha Reedijk Adds a reference to \c archivable to your archive. If \c archivable has 2670e1ec24aSNiels Sascha Reedijk not yet been archived, then its Archive() method is called. BArchiver 2680e1ec24aSNiels Sascha Reedijk can only track BArchivable objects that have been archived through this 2690e1ec24aSNiels Sascha Reedijk method or the GetTokenForArchivable() methods. 2700e1ec24aSNiels Sascha Reedijk 2710e1ec24aSNiels Sascha Reedijk \warning If you manually archive an object, and then pass it to 272c4b9309aSJohn Scipione AddArchivable() or GetTokenForArchivable(), it will be archived 273c4b9309aSJohn Scipione again, and when unarchived you will end up with two different 274c4b9309aSJohn Scipione BArchivable objects. 275c4b9309aSJohn Scipione 276c4b9309aSJohn Scipione \since Haiku R1 2770e1ec24aSNiels Sascha Reedijk*/ 2780e1ec24aSNiels Sascha Reedijk 2790e1ec24aSNiels Sascha Reedijk 2800e1ec24aSNiels Sascha Reedijk/*! 2810e1ec24aSNiels Sascha Reedijk \fn status_t BArchiver::GetTokenForArchivable(BArchivable* archivable, 2820e1ec24aSNiels Sascha Reedijk bool deep, int32& _token); 2830e1ec24aSNiels Sascha Reedijk \brief Get a token representing a BArchivable object for this archiving 2840e1ec24aSNiels Sascha Reedijk session. 2850e1ec24aSNiels Sascha Reedijk 2860e1ec24aSNiels Sascha Reedijk \param archivable The BArchivable object for which you wish to get a 2870e1ec24aSNiels Sascha Reedijk token. 2880e1ec24aSNiels Sascha Reedijk \param deep Controls how \c archivable will be archived, if it has not yet 2890e1ec24aSNiels Sascha Reedijk been archived in this session. 2900e1ec24aSNiels Sascha Reedijk \param[out] _token The token representing \c archivable is stored here. 2910e1ec24aSNiels Sascha Reedijk 2920e1ec24aSNiels Sascha Reedijk Retrieves or creates a token to represent \c archivable in this archiving 2930e1ec24aSNiels Sascha Reedijk session. If \c archivable has not yet been archived, it will be now. If 2940e1ec24aSNiels Sascha Reedijk \c archivable gets archived, the \c deep parameter will be passed to its 2950e1ec24aSNiels Sascha Reedijk Archive() method. 2960e1ec24aSNiels Sascha Reedijk 2970e1ec24aSNiels Sascha Reedijk \warning If you manually archive an object, and then pass it to 298c4b9309aSJohn Scipione GetTokenForArchivable(), it will be archived again, and when 299c4b9309aSJohn Scipione unarchived you will end up with two different BArchivable objects. 300c4b9309aSJohn Scipione 301c4b9309aSJohn Scipione \since Haiku R1 3020e1ec24aSNiels Sascha Reedijk*/ 3030e1ec24aSNiels Sascha Reedijk 3040e1ec24aSNiels Sascha Reedijk 3050e1ec24aSNiels Sascha Reedijk/*! 3060e1ec24aSNiels Sascha Reedijk \fn status_t BArchiver::GetTokenForArchivable(BArchivable* archivable, 3070e1ec24aSNiels Sascha Reedijk int32 &_token) 3080e1ec24aSNiels Sascha Reedijk \brief Equivalent to calling the expanded GetTokenForArchivable( 309c4b9309aSJohn Scipione BArchivable*, bool, int32&), with the deep parameter equal to 310c4b9309aSJohn Scipione \c true. 3110e1ec24aSNiels Sascha Reedijk 3120e1ec24aSNiels Sascha Reedijk \see GetTokenForArchivable(BArchivable*, bool, int32&) 313c4b9309aSJohn Scipione 314c4b9309aSJohn Scipione \since Haiku R1 3150e1ec24aSNiels Sascha Reedijk*/ 3160e1ec24aSNiels Sascha Reedijk 3170e1ec24aSNiels Sascha Reedijk 3180e1ec24aSNiels Sascha Reedijk/*! 3190e1ec24aSNiels Sascha Reedijk \fn bool BArchiver::IsArchived(BArchivable* archivable); 3200e1ec24aSNiels Sascha Reedijk \brief Returns whether \c archivable has already been archived in this 3210e1ec24aSNiels Sascha Reedijk session. 3220e1ec24aSNiels Sascha Reedijk 323c4b9309aSJohn Scipione \return Whether or not the object has already been archived. 3240e1ec24aSNiels Sascha Reedijk \retval true \c archivable has been archived in this archiving session. 3250e1ec24aSNiels Sascha Reedijk \retval false \c archivable has not been archived in this archiving session. 326c4b9309aSJohn Scipione 327c4b9309aSJohn Scipione \since Haiku R1 3280e1ec24aSNiels Sascha Reedijk*/ 3290e1ec24aSNiels Sascha Reedijk 3300e1ec24aSNiels Sascha Reedijk 3310e1ec24aSNiels Sascha Reedijk/*! 3320e1ec24aSNiels Sascha Reedijk \fn status_t BArchiver::Finish(status_t err = B_OK); 3330e1ec24aSNiels Sascha Reedijk \brief Report any archiving errors and possibly complete the archiving 3340e1ec24aSNiels Sascha Reedijk session. 3350e1ec24aSNiels Sascha Reedijk 3360e1ec24aSNiels Sascha Reedijk This method may finish an archiving session (triggering the call of all 3370e1ec24aSNiels Sascha Reedijk archived objects' AllArchived() methods) if the following conditions 3380e1ec24aSNiels Sascha Reedijk are true: 339c4b9309aSJohn Scipione - No errors have been reported to this or any other BArchiver object 3400e1ec24aSNiels Sascha Reedijk within this session. 341c4b9309aSJohn Scipione - This is the last remaining BArchiver that has not had its Finish() 3420e1ec24aSNiels Sascha Reedijk method invoked. 343c4b9309aSJohn Scipione 3440e1ec24aSNiels Sascha Reedijk If you call this method with an error code not equal to B_OK, then this 3450e1ec24aSNiels Sascha Reedijk archiving session has failed, archived objects will not have their 3460e1ec24aSNiels Sascha Reedijk AllArchived() methods called, and any subsequent calls to this method 3470e1ec24aSNiels Sascha Reedijk on any BArchiver objects in this session will return your error code. 348c4b9309aSJohn Scipione 349c4b9309aSJohn Scipione \return The first error reported in this archiving session, or \c B_OK. 350c4b9309aSJohn Scipione 351c4b9309aSJohn Scipione \since Haiku R1 3520e1ec24aSNiels Sascha Reedijk*/ 3530e1ec24aSNiels Sascha Reedijk 3540e1ec24aSNiels Sascha Reedijk 3550e1ec24aSNiels Sascha Reedijk/*! 3560e1ec24aSNiels Sascha Reedijk \fn const BMessage* BArchiver::ArchiveMessage() const 3570e1ec24aSNiels Sascha Reedijk \brief Returns the BMessage* used to construct this BArchiver. This is 3580e1ec24aSNiels Sascha Reedijk the archive that AddArchivable() modifies. 359c4b9309aSJohn Scipione 360c4b9309aSJohn Scipione \since Haiku R1 3610e1ec24aSNiels Sascha Reedijk*/ 3620e1ec24aSNiels Sascha Reedijk 3630e1ec24aSNiels Sascha Reedijk 3640e1ec24aSNiels Sascha Reedijk///// BUnarchiver ///// 3650e1ec24aSNiels Sascha Reedijk 3660e1ec24aSNiels Sascha Reedijk 3670e1ec24aSNiels Sascha Reedijk/*! 3680e1ec24aSNiels Sascha Reedijk \class BUnarchiver 3690e1ec24aSNiels Sascha Reedijk \ingroup support 3700e1ec24aSNiels Sascha Reedijk \ingroup libbe 3710e1ec24aSNiels Sascha Reedijk \brief A class that simplifies the unarchiving of complicated BArchivable 3720e1ec24aSNiels Sascha Reedijk hierarchies. 3730e1ec24aSNiels Sascha Reedijk 3740e1ec24aSNiels Sascha Reedijk The BUnarchiver class is a small class used to recover BArchivable objects 3750e1ec24aSNiels Sascha Reedijk that have been archived with the BArchiver class. It also provides ownership 3760e1ec24aSNiels Sascha Reedijk semantics, so that memory leaks can be avoided during the unarchival 3770e1ec24aSNiels Sascha Reedijk process. When retrieving an object (either via GetObject() or FindObject()), 3780e1ec24aSNiels Sascha Reedijk you can specify a BUnarchiver::ownership_policy. If you specify 3790e1ec24aSNiels Sascha Reedijk BUnarchiver::B_ASSUME_OWNERSHIP, you will become responsible for deleting 3800e1ec24aSNiels Sascha Reedijk the retrieved item. If you specify BUnarchiver::B_DONT_ASSUME_OWNERSHIP, 3810e1ec24aSNiels Sascha Reedijk you will not become responsible. You cannot take ownership of the same 3820e1ec24aSNiels Sascha Reedijk object twice. After the unarchival process finishes, any unclaimed objects, 3830e1ec24aSNiels Sascha Reedijk excluding the root object (the object being instantiated via 3840e1ec24aSNiels Sascha Reedijk instantiate_object() or BUnarchiver::InstantiateObject()), will be deleted. 3850e1ec24aSNiels Sascha Reedijk 3860e1ec24aSNiels Sascha Reedijk If you are updating a class that previously did not use the BArchiver and 3870e1ec24aSNiels Sascha Reedijk BUnarchiver helper classes, and want to maintain backwards compatibility 3880e1ec24aSNiels Sascha Reedijk with old archive, this can be done using the IsArchiveManaged() method. 3890e1ec24aSNiels Sascha Reedijk 390c4b9309aSJohn Scipione \warning Calling methods on your BUnarchiver with a legacy archive (one 391c4b9309aSJohn Scipione that was not managed by a BArchiver during archival) will result 392c4b9309aSJohn Scipione in a call to debugger(). 393c4b9309aSJohn Scipione 394c4b9309aSJohn Scipione \since Haiku R1 3950e1ec24aSNiels Sascha Reedijk*/ 3960e1ec24aSNiels Sascha Reedijk 3970e1ec24aSNiels Sascha Reedijk 3980e1ec24aSNiels Sascha Reedijk/*! 399*4679af27SNiels Sascha Reedijk \enum BUnarchiver::ownership_policy 400*4679af27SNiels Sascha Reedijk \brief Options for the ownership policy of objects retrieved from 401*4679af27SNiels Sascha Reedijk BUnarchiver 402*4679af27SNiels Sascha Reedijk 403*4679af27SNiels Sascha Reedijk \since Haiku R1 404*4679af27SNiels Sascha Reedijk*/ 405*4679af27SNiels Sascha Reedijk 406*4679af27SNiels Sascha Reedijk 407*4679af27SNiels Sascha Reedijk/*! 408*4679af27SNiels Sascha Reedijk \var ownership_policy BUnarchiver::B_ASSUME_OWNERSHIP 409*4679af27SNiels Sascha Reedijk \brief Ownership of unarchived objects will be transferred to the caller. 410*4679af27SNiels Sascha Reedijk 411*4679af27SNiels Sascha Reedijk \since Haiku R1 412*4679af27SNiels Sascha Reedijk*/ 413*4679af27SNiels Sascha Reedijk 414*4679af27SNiels Sascha Reedijk 415*4679af27SNiels Sascha Reedijk/*! 416*4679af27SNiels Sascha Reedijk \var ownership_policy BUnarchiver::B_DONT_ASSUME_OWNERSHIP 417*4679af27SNiels Sascha Reedijk \brief The unarchived objects will be borrowed to the caller. 418*4679af27SNiels Sascha Reedijk 419*4679af27SNiels Sascha Reedijk \since Haiku R1 420*4679af27SNiels Sascha Reedijk*/ 421*4679af27SNiels Sascha Reedijk 422*4679af27SNiels Sascha Reedijk 423*4679af27SNiels Sascha Reedijk/*! 4240e1ec24aSNiels Sascha Reedijk \fn BUnarchiver::BUnarchiver(const BMessage* archive) 4250e1ec24aSNiels Sascha Reedijk \brief Constructs a BUnarchiver object to manage \c archive. 4260e1ec24aSNiels Sascha Reedijk 4270e1ec24aSNiels Sascha Reedijk \note To guarantee that your AllUnarchived() method will be called during 4280e1ec24aSNiels Sascha Reedijk archival, you must create a BUnarchiver object in your archive 4290e1ec24aSNiels Sascha Reedijk constructor. It is necessary to do this even if you won't use the 4300e1ec24aSNiels Sascha Reedijk BUnarchiver object in your archive constructor. 4310e1ec24aSNiels Sascha Reedijk 4320e1ec24aSNiels Sascha Reedijk \warning Do not construct a BUnarchiver object without first calling 433c4b9309aSJohn Scipione BUnarchiver::PrepareArchive() on \c archive. It is only safe to 434c4b9309aSJohn Scipione build a BUnarchiver without this call in your AllUnarchived() 435c4b9309aSJohn Scipione implementation. 4360e1ec24aSNiels Sascha Reedijk 4370e1ec24aSNiels Sascha Reedijk \see BUnarchiver::PrepareArchive() 438c4b9309aSJohn Scipione 439c4b9309aSJohn Scipione \since Haiku R1 4400e1ec24aSNiels Sascha Reedijk*/ 4410e1ec24aSNiels Sascha Reedijk 4420e1ec24aSNiels Sascha Reedijk 4430e1ec24aSNiels Sascha Reedijk/*! 4440e1ec24aSNiels Sascha Reedijk \fn BUnarchiver::~BUnarchiver() 4450e1ec24aSNiels Sascha Reedijk \brief Destroys a BUnarchiver object. 4460e1ec24aSNiels Sascha Reedijk 4470e1ec24aSNiels Sascha Reedijk Calls this objects Finish() method, if it has not yet been called. 448c4b9309aSJohn Scipione 449c4b9309aSJohn Scipione \since Haiku R1 4500e1ec24aSNiels Sascha Reedijk*/ 4510e1ec24aSNiels Sascha Reedijk 4520e1ec24aSNiels Sascha Reedijk 4530e1ec24aSNiels Sascha Reedijk/*! 4540e1ec24aSNiels Sascha Reedijk \fn status_t BUnarchiver::EnsureUnarchived(int32 token) 4550e1ec24aSNiels Sascha Reedijk \brief Ensure the object represented by \a token is unarchived and 4560e1ec24aSNiels Sascha Reedijk instantiated. 4570e1ec24aSNiels Sascha Reedijk 4580e1ec24aSNiels Sascha Reedijk \param token the object \a token 4590e1ec24aSNiels Sascha Reedijk 4600e1ec24aSNiels Sascha Reedijk \returns A status code. 461c4b9309aSJohn Scipione 462c4b9309aSJohn Scipione \since Haiku R1 4630e1ec24aSNiels Sascha Reedijk*/ 4640e1ec24aSNiels Sascha Reedijk 4650e1ec24aSNiels Sascha Reedijk 4660e1ec24aSNiels Sascha Reedijk/*! 4670e1ec24aSNiels Sascha Reedijk \fn status_t BUnarchiver::EnsureUnarchived(const char* name, 4680e1ec24aSNiels Sascha Reedijk int32 index = 0) 4690e1ec24aSNiels Sascha Reedijk \brief Ensure the object archived under \a name at \a index is unarchived 4700e1ec24aSNiels Sascha Reedijk and instantiated. 4710e1ec24aSNiels Sascha Reedijk 4720e1ec24aSNiels Sascha Reedijk \param name The archive \a name. 4730e1ec24aSNiels Sascha Reedijk \param index The archive \a index. 4740e1ec24aSNiels Sascha Reedijk 4750e1ec24aSNiels Sascha Reedijk \returns A status code. 476c4b9309aSJohn Scipione 477c4b9309aSJohn Scipione \since Haiku R1 4780e1ec24aSNiels Sascha Reedijk*/ 4790e1ec24aSNiels Sascha Reedijk 4800e1ec24aSNiels Sascha Reedijk 4810e1ec24aSNiels Sascha Reedijk/*! 4820e1ec24aSNiels Sascha Reedijk \fn bool BUnarchiver::IsInstantiated(int32 token) 4830e1ec24aSNiels Sascha Reedijk \brief Checks whether the object represented by \c token has been 4840e1ec24aSNiels Sascha Reedijk instantiated in this session. 4850e1ec24aSNiels Sascha Reedijk 4860e1ec24aSNiels Sascha Reedijk \param token The object \a token. 4870e1ec24aSNiels Sascha Reedijk 4880e1ec24aSNiels Sascha Reedijk \returns \c true if instantiated, \c false otherwise 489c4b9309aSJohn Scipione 490c4b9309aSJohn Scipione \since Haiku R1 4910e1ec24aSNiels Sascha Reedijk*/ 4920e1ec24aSNiels Sascha Reedijk 4930e1ec24aSNiels Sascha Reedijk 4940e1ec24aSNiels Sascha Reedijk/*! 4950e1ec24aSNiels Sascha Reedijk \fn bool BUnarchiver::IsInstantiated(const char* name, int32 index = 0) 4960e1ec24aSNiels Sascha Reedijk \brief Checks whether the object archived under \a name at \a index has been 4970e1ec24aSNiels Sascha Reedijk instantiated in this session. 4980e1ec24aSNiels Sascha Reedijk 4990e1ec24aSNiels Sascha Reedijk \param name The archive \a name. 5000e1ec24aSNiels Sascha Reedijk \param index The arcive \a token. 5010e1ec24aSNiels Sascha Reedijk 5020e1ec24aSNiels Sascha Reedijk \returns \c true if instantiated, \c false otherwise. 503c4b9309aSJohn Scipione 504c4b9309aSJohn Scipione \since Haiku R1 5050e1ec24aSNiels Sascha Reedijk*/ 5060e1ec24aSNiels Sascha Reedijk 5070e1ec24aSNiels Sascha Reedijk 5080e1ec24aSNiels Sascha Reedijk/*! 5090e1ec24aSNiels Sascha Reedijk \fn template<class T> status_t BUnarchiver::GetObject(int32 token, 5100e1ec24aSNiels Sascha Reedijk ownership_policy owning, T*& object) 5110e1ec24aSNiels Sascha Reedijk \brief Recover an object by token that was archived by a BArchiver object. 5120e1ec24aSNiels Sascha Reedijk If the object has not yet been instantiated, and this request is not coming 5130e1ec24aSNiels Sascha Reedijk from an AllUnarchived() implementation, the object will be instantiated now. 5140e1ec24aSNiels Sascha Reedijk 5150e1ec24aSNiels Sascha Reedijk If the retrieved object is not of the type T, then this method will fail. 5160e1ec24aSNiels Sascha Reedijk If this method fails, you will not receive ownership of the object, no 5170e1ec24aSNiels Sascha Reedijk matter what you specified in \c owning. 5180e1ec24aSNiels Sascha Reedijk 5190e1ec24aSNiels Sascha Reedijk \tparam T The type of \a object you wish to find. 5200e1ec24aSNiels Sascha Reedijk 5210e1ec24aSNiels Sascha Reedijk \param token The \a token you got for this object from 5220e1ec24aSNiels Sascha Reedijk BArchiver::GetTokenForArchivable() during archival. 5230e1ec24aSNiels Sascha Reedijk \param owning Whether or not you wish to take ownership of the 5240e1ec24aSNiels Sascha Reedijk retrieved object. 5250e1ec24aSNiels Sascha Reedijk \param object Return parameter for the retrieved object of type T. 5260e1ec24aSNiels Sascha Reedijk 5270e1ec24aSNiels Sascha Reedijk \returns A status code. 5280e1ec24aSNiels Sascha Reedijk \retval B_OK The object retrieved was of type T. 5290e1ec24aSNiels Sascha Reedijk \retval B_BAD_TYPE The object retrieved was not of type T. 530c4b9309aSJohn Scipione 531c4b9309aSJohn Scipione \since Haiku R1 5320e1ec24aSNiels Sascha Reedijk*/ 5330e1ec24aSNiels Sascha Reedijk 5340e1ec24aSNiels Sascha Reedijk 5350e1ec24aSNiels Sascha Reedijk/*! 5360e1ec24aSNiels Sascha Reedijk \fn template<class T> status_t BUnarchiver::GetObject(int32 token, 5370e1ec24aSNiels Sascha Reedijk T*& object) 5380e1ec24aSNiels Sascha Reedijk \brief Recover and take ownership of an object represented by \a token. 5390e1ec24aSNiels Sascha Reedijk 5400e1ec24aSNiels Sascha Reedijk Equivalent to calling GetObject(token, \c B_ASSUME_OWNERSHIP, object) 5410e1ec24aSNiels Sascha Reedijk 5420e1ec24aSNiels Sascha Reedijk \tparam T The type of \a object you wish to find. 5430e1ec24aSNiels Sascha Reedijk 5440e1ec24aSNiels Sascha Reedijk \param token The \a token you got for this object from 5450e1ec24aSNiels Sascha Reedijk BArchiver::GetTokenForArchivable() during archival. 5460e1ec24aSNiels Sascha Reedijk \param object The return parameter for the retrieved object of type T. 5470e1ec24aSNiels Sascha Reedijk 5480e1ec24aSNiels Sascha Reedijk \returns A status code. 5490e1ec24aSNiels Sascha Reedijk \retval B_OK The object retrieved was of type T. 5500e1ec24aSNiels Sascha Reedijk \retval B_BAD_TYPE The object retrieved was not of type T. 551c4b9309aSJohn Scipione 552c4b9309aSJohn Scipione \since Haiku R1 5530e1ec24aSNiels Sascha Reedijk*/ 5540e1ec24aSNiels Sascha Reedijk 5550e1ec24aSNiels Sascha Reedijk 5560e1ec24aSNiels Sascha Reedijk/*! 5570e1ec24aSNiels Sascha Reedijk \fn template<class T> status_t BUnarchiver::FindObject(const char* name, 5580e1ec24aSNiels Sascha Reedijk int32 index, ownership_policy owning, T*& object) 5590e1ec24aSNiels Sascha Reedijk \brief Recover an object that had previously been archived using 5600e1ec24aSNiels Sascha Reedijk the BArchiver::AddArchivable() method. 5610e1ec24aSNiels Sascha Reedijk 5620e1ec24aSNiels Sascha Reedijk If the object has not yet been instantiated, and this request is not 5630e1ec24aSNiels Sascha Reedijk coming from an AllUnarchived() implementation, the object will be 5640e1ec24aSNiels Sascha Reedijk instantiated now. 5650e1ec24aSNiels Sascha Reedijk 5660e1ec24aSNiels Sascha Reedijk If the retrieved object is not of the type T, then this method will fail. 5670e1ec24aSNiels Sascha Reedijk If this method fails, you will not receive ownership of the object, no 5680e1ec24aSNiels Sascha Reedijk matter what you specified in \c owning. 5690e1ec24aSNiels Sascha Reedijk 5700e1ec24aSNiels Sascha Reedijk \tparam T The type of object you wish to find. 5710e1ec24aSNiels Sascha Reedijk 5720e1ec24aSNiels Sascha Reedijk \param name The name that was passed to BArchiver::AddArchivable() when 5730e1ec24aSNiels Sascha Reedijk adding this object. 5740e1ec24aSNiels Sascha Reedijk \param index The index of the object you wish to recover (\c 0-based, 5750e1ec24aSNiels Sascha Reedijk like BMessage::FindData(). 5760e1ec24aSNiels Sascha Reedijk \param owning Dictates whether or not you wish to take ownership of the 5770e1ec24aSNiels Sascha Reedijk retrieved object. 5780e1ec24aSNiels Sascha Reedijk \param object Return parameter for the retrieved object of type T. 5790e1ec24aSNiels Sascha Reedijk 5800e1ec24aSNiels Sascha Reedijk \returns A status code. 5810e1ec24aSNiels Sascha Reedijk \retval B_OK The object retrieved was of type T. 5820e1ec24aSNiels Sascha Reedijk \retval B_BAD_TYPE The object retrieved was not of type T. 583c4b9309aSJohn Scipione 584c4b9309aSJohn Scipione \since Haiku R1 5850e1ec24aSNiels Sascha Reedijk*/ 5860e1ec24aSNiels Sascha Reedijk 5870e1ec24aSNiels Sascha Reedijk 5880e1ec24aSNiels Sascha Reedijk/*! 5890e1ec24aSNiels Sascha Reedijk \fn template<class T> status_t BUnarchiver::FindObject(const char* name, 5900e1ec24aSNiels Sascha Reedijk int32 index, T*& object) 5910e1ec24aSNiels Sascha Reedijk \brief Recover and take ownership of an object that had previously been 5920e1ec24aSNiels Sascha Reedijk archived using the BArchiver::AddArchivable() method. 5930e1ec24aSNiels Sascha Reedijk 5940e1ec24aSNiels Sascha Reedijk \tparam T The type of object you wish to find. 5950e1ec24aSNiels Sascha Reedijk 5960e1ec24aSNiels Sascha Reedijk \param name The name that was passed to BArchiver::AddArchivable() when 5970e1ec24aSNiels Sascha Reedijk adding this object. 5980e1ec24aSNiels Sascha Reedijk \param index The index of the object you wish to recover (\c 0-based, 5990e1ec24aSNiels Sascha Reedijk like #BMessage::FindData(). 6000e1ec24aSNiels Sascha Reedijk \param object Return parameter for the retrieved object of type T. 6010e1ec24aSNiels Sascha Reedijk 6020e1ec24aSNiels Sascha Reedijk \returns A status code. 6030e1ec24aSNiels Sascha Reedijk \retval B_OK The object retrieved was of type T. 6040e1ec24aSNiels Sascha Reedijk \retval B_BAD_TYPE The object retrieved was not of type T. 605c4b9309aSJohn Scipione 606c4b9309aSJohn Scipione \since Haiku R1 6070e1ec24aSNiels Sascha Reedijk*/ 6080e1ec24aSNiels Sascha Reedijk 6090e1ec24aSNiels Sascha Reedijk 6100e1ec24aSNiels Sascha Reedijk/*! 6110e1ec24aSNiels Sascha Reedijk \fn template<class T> status_t BUnarchiver::FindObject(const char* name, 6120e1ec24aSNiels Sascha Reedijk ownership_policy owning, T*& object) 6130e1ec24aSNiels Sascha Reedijk \brief Recover an object at index \c 0 that had previously been 6140e1ec24aSNiels Sascha Reedijk archived using the BArchiver::AddArchivable() method. 6150e1ec24aSNiels Sascha Reedijk 6160e1ec24aSNiels Sascha Reedijk Equivalent to calling FindObject(name, \c 0, owning, object). 6170e1ec24aSNiels Sascha Reedijk 6180e1ec24aSNiels Sascha Reedijk \tparam T The type of \a object you wish to find. 6190e1ec24aSNiels Sascha Reedijk 6200e1ec24aSNiels Sascha Reedijk \param name The name that was passed to BArchiver::AddArchivable() when 6210e1ec24aSNiels Sascha Reedijk adding this object. 6220e1ec24aSNiels Sascha Reedijk \param owning Dictates whether or not you wish to take ownership of the 6230e1ec24aSNiels Sascha Reedijk retrieved object. 6240e1ec24aSNiels Sascha Reedijk \param object Return parameter for the retrieved object of type T. 6250e1ec24aSNiels Sascha Reedijk 6260e1ec24aSNiels Sascha Reedijk \returns A status code. 6270e1ec24aSNiels Sascha Reedijk \retval B_OK The object retrieved was of type T. 6280e1ec24aSNiels Sascha Reedijk \retval B_BAD_TYPE The object retrieved was not of type T. 629c4b9309aSJohn Scipione 630c4b9309aSJohn Scipione \since Haiku R1 6310e1ec24aSNiels Sascha Reedijk*/ 6320e1ec24aSNiels Sascha Reedijk 6330e1ec24aSNiels Sascha Reedijk 6340e1ec24aSNiels Sascha Reedijk/*! 6350e1ec24aSNiels Sascha Reedijk \fn template<class T> status_t BUnarchiver::FindObject(const char* name, 6360e1ec24aSNiels Sascha Reedijk T*& object) 6370e1ec24aSNiels Sascha Reedijk \brief Recover and take ownership of an object at index \c 0 that had 6380e1ec24aSNiels Sascha Reedijk previously been archived using the BArchiver::AddArchivable() method. 6390e1ec24aSNiels Sascha Reedijk 6400e1ec24aSNiels Sascha Reedijk Equivalent to calling FindObject(name, \c 0, 6410e1ec24aSNiels Sascha Reedijk BUnarchiver::B_ASSUME_OWNERSHIP, object). 6420e1ec24aSNiels Sascha Reedijk 6430e1ec24aSNiels Sascha Reedijk \tparam T The type of \a object you wish to find. 6440e1ec24aSNiels Sascha Reedijk 6450e1ec24aSNiels Sascha Reedijk \param name The name that was passed to BArchiver::AddArchivable() when 6460e1ec24aSNiels Sascha Reedijk adding this object. 6470e1ec24aSNiels Sascha Reedijk \param object Return parameter for the retrieved \a object of type T. 6480e1ec24aSNiels Sascha Reedijk 6490e1ec24aSNiels Sascha Reedijk \returns A status code. 6500e1ec24aSNiels Sascha Reedijk \retval B_OK The \a object retrieved was of type T. 6510e1ec24aSNiels Sascha Reedijk \retval B_BAD_TYPE The \a object retrieved was not of type T. 652c4b9309aSJohn Scipione 653c4b9309aSJohn Scipione \since Haiku R1 6540e1ec24aSNiels Sascha Reedijk*/ 6550e1ec24aSNiels Sascha Reedijk 6560e1ec24aSNiels Sascha Reedijk 6570e1ec24aSNiels Sascha Reedijk/*! 6580e1ec24aSNiels Sascha Reedijk \fn status_t BUnarchiver::Finish(status_t err = B_OK); 6590e1ec24aSNiels Sascha Reedijk \brief Report any unarchiving errors and possibly complete the archiving 6600e1ec24aSNiels Sascha Reedijk session. 6610e1ec24aSNiels Sascha Reedijk 6620e1ec24aSNiels Sascha Reedijk This method may finish an unarchiving session (triggering the call of all 6630e1ec24aSNiels Sascha Reedijk instantiated objects' AllUnarchived() methods) if the following conditions 6640e1ec24aSNiels Sascha Reedijk are true: 6650e1ec24aSNiels Sascha Reedijk 666c4b9309aSJohn Scipione - No errors have been reported to this or any other BUnarchiver 6670e1ec24aSNiels Sascha Reedijk object within this session. 668c4b9309aSJohn Scipione - This is the last remaining BUnarchiver that has not had its 6690e1ec24aSNiels Sascha Reedijk Finish() method invoked. 6700e1ec24aSNiels Sascha Reedijk 6710e1ec24aSNiels Sascha Reedijk If you call this method with an error code not equal to B_OK, then this 6720e1ec24aSNiels Sascha Reedijk unarchiving session has failed, instantiated objects will not have their 6730e1ec24aSNiels Sascha Reedijk AllUnarchived() methods called, and any subsequent calls to this method 6740e1ec24aSNiels Sascha Reedijk on any BUnarchiver objects in this session will return your error code. 6750e1ec24aSNiels Sascha Reedijk Furthermore, any objects that have been instantiated, but have not had 6760e1ec24aSNiels Sascha Reedijk their ownership assumed by another object will now be deleted (excluding 6770e1ec24aSNiels Sascha Reedijk the root object). 6780e1ec24aSNiels Sascha Reedijk 6790e1ec24aSNiels Sascha Reedijk \return The first error reported in this unarchiving session, or \c B_OK. 680c4b9309aSJohn Scipione 681c4b9309aSJohn Scipione \since Haiku R1 6820e1ec24aSNiels Sascha Reedijk*/ 6830e1ec24aSNiels Sascha Reedijk 6840e1ec24aSNiels Sascha Reedijk 6850e1ec24aSNiels Sascha Reedijk/*! 6860e1ec24aSNiels Sascha Reedijk \fn const BMessage* BUnarchiver::ArchiveMessage() const 6870e1ec24aSNiels Sascha Reedijk \brief Returns the BMessage* used to construct this BUnarchiver. 6880e1ec24aSNiels Sascha Reedijk 6890e1ec24aSNiels Sascha Reedijk This is the archive that FindObject() uses. 6900e1ec24aSNiels Sascha Reedijk*/ 6910e1ec24aSNiels Sascha Reedijk 6920e1ec24aSNiels Sascha Reedijk 6930e1ec24aSNiels Sascha Reedijk/*! 6940e1ec24aSNiels Sascha Reedijk \fn static bool BUnarchiver::IsArchiveManaged(const BMessage* archive) 6950e1ec24aSNiels Sascha Reedijk \brief Checks whether \a archive was managed by a BArchiver object. 6960e1ec24aSNiels Sascha Reedijk 6970e1ec24aSNiels Sascha Reedijk This method can be used to maintain archive backwards-compatibility for a 6980e1ec24aSNiels Sascha Reedijk class that has been updated to use the BArchiver class. If there is a 6990e1ec24aSNiels Sascha Reedijk possibility that you are may dealing with a legacy archive, you can use 7000e1ec24aSNiels Sascha Reedijk this method to find out before calling any methods on your BUnarchiver 7010e1ec24aSNiels Sascha Reedijk object. 7020e1ec24aSNiels Sascha Reedijk 7030e1ec24aSNiels Sascha Reedijk Here is an example of how you might use this method. Note that you 7040e1ec24aSNiels Sascha Reedijk must still call PrepareArchive(archive) either way. 7050e1ec24aSNiels Sascha Reedijk 7060e1ec24aSNiels Sascha Reedijk\code 7070e1ec24aSNiels Sascha ReedijkMyArchivableClas::MyArchivableClass(BMessage* archive) 7080e1ec24aSNiels Sascha Reedijk : 7090e1ec24aSNiels Sascha Reedijk BArchivable(BUnarchiver::PrepareArchive(archive)) 7100e1ec24aSNiels Sascha Reedijk{ 7110e1ec24aSNiels Sascha Reedijk BUnarchiver unarchiver(archive); 7120e1ec24aSNiels Sascha Reedijk 7130e1ec24aSNiels Sascha Reedijk if (BUnarchiver::IsArchiveManaged(archive)) { 7140e1ec24aSNiels Sascha Reedijk // ... calls to FindObject() or GetObject() here ... 7150e1ec24aSNiels Sascha Reedijk } else { 7160e1ec24aSNiels Sascha Reedijk // ... calls to BMessage::FindMessage() here ... 7170e1ec24aSNiels Sascha Reedijk } 7180e1ec24aSNiels Sascha Reedijk} 7190e1ec24aSNiels Sascha Reedijk\endcode 7200e1ec24aSNiels Sascha Reedijk 7210e1ec24aSNiels Sascha Reedijk \returns Whether \a archive was managed by a BArchiver object. 7220e1ec24aSNiels Sascha Reedijk \retval true if \a archive was managed by a BArchiver object. 7230e1ec24aSNiels Sascha Reedijk \retval false otherwise. 724c4b9309aSJohn Scipione 725c4b9309aSJohn Scipione \since Haiku R1 7260e1ec24aSNiels Sascha Reedijk*/ 7270e1ec24aSNiels Sascha Reedijk 7280e1ec24aSNiels Sascha Reedijk 7290e1ec24aSNiels Sascha Reedijk/*! 7300e1ec24aSNiels Sascha Reedijk \fn static BMessage* BUnarchiver::PrepareArchive(BMessage* &archive) 7310e1ec24aSNiels Sascha Reedijk \brief Prepares \c archive for use by a BUnarchiver. 7320e1ec24aSNiels Sascha Reedijk 7330e1ec24aSNiels Sascha Reedijk This method must be called if you plan to use a BUnarchiver on an archive. 7340e1ec24aSNiels Sascha Reedijk It must be called once for each class an object inherits from that 7350e1ec24aSNiels Sascha Reedijk will use a BUnarchiver. 7360e1ec24aSNiels Sascha Reedijk 7370e1ec24aSNiels Sascha Reedijk \warning This method \b must be called \b before a call to the 7380e1ec24aSNiels Sascha Reedijk archive constructor of your parent class. 7390e1ec24aSNiels Sascha Reedijk 7400e1ec24aSNiels Sascha Reedijk Notice the use of this method in the example provided below. 7410e1ec24aSNiels Sascha Reedijk\code 7420e1ec24aSNiels Sascha ReedijkMyArchivableClas::MyArchivableClas(BMessage* archive) 7430e1ec24aSNiels Sascha Reedijk : 7440e1ec24aSNiels Sascha Reedijk BArchivable(BUnarchiver::PrepareArchive(archive)) 7450e1ec24aSNiels Sascha Reedijk{ 7460e1ec24aSNiels Sascha Reedijk // ... 7470e1ec24aSNiels Sascha Reedijk} 7480e1ec24aSNiels Sascha Reedijk\endcode 7490e1ec24aSNiels Sascha Reedijk 7500e1ec24aSNiels Sascha Reedijk \param archive The archive you wish to have prepared. 7510e1ec24aSNiels Sascha Reedijk 7520e1ec24aSNiels Sascha Reedijk \return The same #BMessage as is passed in. 753c4b9309aSJohn Scipione 754c4b9309aSJohn Scipione \since Haiku R1 7550e1ec24aSNiels Sascha Reedijk*/ 7560e1ec24aSNiels Sascha Reedijk 7570e1ec24aSNiels Sascha Reedijk 7580e1ec24aSNiels Sascha Reedijk/*! 7590e1ec24aSNiels Sascha Reedijk \fn void BUnarchiver::AssumeOwnership(BArchivable* archivable) 7600e1ec24aSNiels Sascha Reedijk \brief Become the owner of \a archivable. 7610e1ec24aSNiels Sascha Reedijk 7620e1ec24aSNiels Sascha Reedijk After calling this method you are responsible for deleting the 7630e1ec24aSNiels Sascha Reedijk \a archivable. 7640e1ec24aSNiels Sascha Reedijk 7650e1ec24aSNiels Sascha Reedijk \param archivable The \a archivable object. 766c4b9309aSJohn Scipione 767c4b9309aSJohn Scipione \since Haiku R1 7680e1ec24aSNiels Sascha Reedijk*/ 7690e1ec24aSNiels Sascha Reedijk 7700e1ec24aSNiels Sascha Reedijk 7710e1ec24aSNiels Sascha Reedijk/*! 7720e1ec24aSNiels Sascha Reedijk \fn void BUnarchiver::RelinquishOwnership(BArchivable* archivable) 7730e1ec24aSNiels Sascha Reedijk \brief Relinquish ownership of \a archivable. If \a archivable remains 7740e1ec24aSNiels Sascha Reedijk unclaimed at the end of the unarchiving session, it will be deleted 7750e1ec24aSNiels Sascha Reedijk (unless it is the root object). 7760e1ec24aSNiels Sascha Reedijk 7770e1ec24aSNiels Sascha Reedijk \param archivable The \a archivable object. 778c4b9309aSJohn Scipione 779c4b9309aSJohn Scipione \since Haiku R1 7800e1ec24aSNiels Sascha Reedijk*/ 7810e1ec24aSNiels Sascha Reedijk 7820e1ec24aSNiels Sascha Reedijk 7830e1ec24aSNiels Sascha Reedijk/*! 7840e1ec24aSNiels Sascha Reedijk \fn template<class T> status_t BUnarchiver::InstantiateObject( 7850e1ec24aSNiels Sascha Reedijk BMessage* from, T*& object) 7860e1ec24aSNiels Sascha Reedijk \brief Attempt to instantiate an object of type T from BMessage* 7870e1ec24aSNiels Sascha Reedijk \a from. 7880e1ec24aSNiels Sascha Reedijk 7890e1ec24aSNiels Sascha Reedijk If the instantiated object is not of type T, then it will be deleted, 7900e1ec24aSNiels Sascha Reedijk and this method will return \c B_BAD_TYPE. This method is similar to 7910e1ec24aSNiels Sascha Reedijk the instantiate_object() function, but provides error reporting and 7920e1ec24aSNiels Sascha Reedijk protection from memory leaks. 7930e1ec24aSNiels Sascha Reedijk 7940e1ec24aSNiels Sascha Reedijk \param from The #BMessage to instantiate from. 7950e1ec24aSNiels Sascha Reedijk \param object Return parameter for the retrieved object of type T. 7960e1ec24aSNiels Sascha Reedijk 7970e1ec24aSNiels Sascha Reedijk \returns A status code. 7980e1ec24aSNiels Sascha Reedijk \retval B_OK The object retrieved was of type T. 7990e1ec24aSNiels Sascha Reedijk \retval B_BAD_TYPE The object retrieved was not of type T. 800c4b9309aSJohn Scipione 801c4b9309aSJohn Scipione \since Haiku R1 8020e1ec24aSNiels Sascha Reedijk*/ 8030e1ec24aSNiels Sascha Reedijk 8040e1ec24aSNiels Sascha Reedijk 80537503f54SNiels Sascha Reedijk///// Global methods ///// 806820dca4dSJohn Scipione 807820dca4dSJohn Scipione 80881071f5eSNiels Sascha Reedijk/*! 80981071f5eSNiels Sascha Reedijk \addtogroup support_globals 81081071f5eSNiels Sascha Reedijk*/ 81181071f5eSNiels Sascha Reedijk 81255f7db1bSNiels Sascha Reedijk 813820dca4dSJohn Scipione//! @{ 814820dca4dSJohn Scipione 815820dca4dSJohn Scipione 816820dca4dSJohn Scipione/*! 817820dca4dSJohn Scipione \typedef typedef BArchivable* (*instantiation_func)(BMessage*) 81881071f5eSNiels Sascha Reedijk \brief Internal definition of a function that can instantiate objects that 81981071f5eSNiels Sascha Reedijk have been created with the BArchivable API. 820c4b9309aSJohn Scipione 821c4b9309aSJohn Scipione \since BeOS R3 82281071f5eSNiels Sascha Reedijk*/ 82381071f5eSNiels Sascha Reedijk 82455f7db1bSNiels Sascha Reedijk 8256ac7032dSJohn Scipione/*! 8266ac7032dSJohn Scipione \fn BArchivable* instantiate_object(BMessage *from, image_id *id) 82781071f5eSNiels Sascha Reedijk \brief Instantiate an archived object with the object being defined in a 82881071f5eSNiels Sascha Reedijk different application or library. 82981071f5eSNiels Sascha Reedijk 83081071f5eSNiels Sascha Reedijk This function is similar to instantiate_object(BMessage *from), except that 831e3c5ca85SNiels Sascha Reedijk it takes the \a id argument referring to an image where the object might be 83237503f54SNiels Sascha Reedijk stored. 83381071f5eSNiels Sascha Reedijk 83437503f54SNiels Sascha Reedijk \note Images are names for executable files. Image id's refer to these 835c4b9309aSJohn Scipione executable files that have been loaded by your application. Have a 836c4b9309aSJohn Scipione look at the kernel API for further information. 837c4b9309aSJohn Scipione 838c4b9309aSJohn Scipione \since BeOS R3 83981071f5eSNiels Sascha Reedijk*/ 84081071f5eSNiels Sascha Reedijk 84155f7db1bSNiels Sascha Reedijk 8426ac7032dSJohn Scipione/*! 8436ac7032dSJohn Scipione \fn BArchivable* instantiate_object(BMessage *from) 84481071f5eSNiels Sascha Reedijk \brief Instantiate an archived object. 84581071f5eSNiels Sascha Reedijk 84637503f54SNiels Sascha Reedijk This global function will determine the base class, based on the \a from 84781071f5eSNiels Sascha Reedijk argument, and it will call the Instantiate() function of that object to 84881071f5eSNiels Sascha Reedijk restore it. 84981071f5eSNiels Sascha Reedijk 85081071f5eSNiels Sascha Reedijk \param from The archived object. 851c4b9309aSJohn Scipione 85281071f5eSNiels Sascha Reedijk \return The object returns a pointer to the instantiated object, or \c NULL 853c4b9309aSJohn Scipione if the instantiation failed. The global \c errno variable will 854c4b9309aSJohn Scipione contain the reason why it failed. 855c4b9309aSJohn Scipione 85681071f5eSNiels Sascha Reedijk \see instantiate_object(BMessage *from, image_id *id) 857c4b9309aSJohn Scipione 858c4b9309aSJohn Scipione \since BeOS R3 85981071f5eSNiels Sascha Reedijk*/ 86081071f5eSNiels Sascha Reedijk 86155f7db1bSNiels Sascha Reedijk 8626ac7032dSJohn Scipione/*! 8636ac7032dSJohn Scipione \fn bool validate_instantiation(BMessage* from, const char* className) 86481071f5eSNiels Sascha Reedijk \brief Internal function that checks if the \a className is the same as the 86581071f5eSNiels Sascha Reedijk one stored in the \a from message. 866c4b9309aSJohn Scipione 867c4b9309aSJohn Scipione \since BeOS R3 86881071f5eSNiels Sascha Reedijk*/ 86981071f5eSNiels Sascha Reedijk 87055f7db1bSNiels Sascha Reedijk 8716ac7032dSJohn Scipione/*! 8726ac7032dSJohn Scipione \fn instantiation_func find_instantiation_func(const char* className, 87355f7db1bSNiels Sascha Reedijk const char* signature) 87481071f5eSNiels Sascha Reedijk \brief Internal function that searches for the instantiation func with a 87581071f5eSNiels Sascha Reedijk specific signature. Use instantiate_object() instead. 876c4b9309aSJohn Scipione 877c4b9309aSJohn Scipione \since Haiku R1 87881071f5eSNiels Sascha Reedijk*/ 87981071f5eSNiels Sascha Reedijk 88055f7db1bSNiels Sascha Reedijk 8816ac7032dSJohn Scipione/*! 8826ac7032dSJohn Scipione \fn instantiation_func find_instantiation_func(const char* className) 88381071f5eSNiels Sascha Reedijk \brief Internal function that searches for the instantiation func of a 88481071f5eSNiels Sascha Reedijk specific class. Use instantiate_object() instead. 885c4b9309aSJohn Scipione 886c4b9309aSJohn Scipione \since BeOS R3 88781071f5eSNiels Sascha Reedijk*/ 88881071f5eSNiels Sascha Reedijk 88955f7db1bSNiels Sascha Reedijk 8906ac7032dSJohn Scipione/*! 8916ac7032dSJohn Scipione \fn instantiation_func find_instantiation_func(BMessage* archive) 89281071f5eSNiels Sascha Reedijk \brief Internal function that searches for the instantiation func that 89381071f5eSNiels Sascha Reedijk works on the specified \a archive. Use instantiate_object() instead. 894c4b9309aSJohn Scipione 895c4b9309aSJohn Scipione \since BeOS R3 89681071f5eSNiels Sascha Reedijk*/ 89781071f5eSNiels Sascha Reedijk 89855f7db1bSNiels Sascha Reedijk 89981071f5eSNiels Sascha Reedijk//! @} 900*4679af27SNiels Sascha Reedijk