2007-01-26 19:36:29 +03:00
|
|
|
/*
|
2010-07-08 18:54:25 +04:00
|
|
|
* Copyright 2001-2010, Haiku, Inc. All Rights Reserved.
|
2007-01-26 19:36:29 +03:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2002-07-09 16:24:59 +04:00
|
|
|
#ifndef _ARCHIVABLE_H
|
|
|
|
#define _ARCHIVABLE_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <image.h>
|
2010-07-16 20:49:42 +04:00
|
|
|
#include <Message.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
|
|
|
|
|
|
|
class BMessage;
|
|
|
|
|
2010-07-16 20:49:42 +04:00
|
|
|
|
2010-07-08 18:54:25 +04:00
|
|
|
namespace BPrivate {
|
|
|
|
namespace Archiving {
|
|
|
|
class BArchiveManager;
|
|
|
|
class BUnarchiveManager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-21 14:29:41 +04:00
|
|
|
using ::BPrivate::Archiving::BArchiveManager;
|
|
|
|
using ::BPrivate::Archiving::BUnarchiveManager;
|
2010-07-08 18:54:25 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2007-01-26 19:36:29 +03:00
|
|
|
class BArchivable {
|
2010-07-08 18:54:25 +04:00
|
|
|
public:
|
2010-07-08 19:04:38 +04:00
|
|
|
BArchivable(BMessage* from);
|
|
|
|
BArchivable();
|
|
|
|
virtual ~BArchivable();
|
2010-07-08 18:54:25 +04:00
|
|
|
|
2010-07-08 19:04:38 +04:00
|
|
|
virtual status_t Archive(BMessage* into, bool deep = true) const;
|
|
|
|
static BArchivable* Instantiate(BMessage* archive);
|
2010-07-08 18:54:25 +04:00
|
|
|
|
2010-07-08 19:04:38 +04:00
|
|
|
virtual status_t Perform(perform_code d, void* arg);
|
2010-07-08 18:54:25 +04:00
|
|
|
|
2010-07-08 19:04:38 +04:00
|
|
|
virtual status_t AllUnarchived(const BMessage* archive);
|
|
|
|
virtual status_t AllArchived(BMessage* archive) const;
|
2010-07-08 18:54:25 +04:00
|
|
|
|
|
|
|
private:
|
2010-07-16 20:49:42 +04:00
|
|
|
friend class BUnarchiveManager;
|
|
|
|
|
2010-07-08 19:04:38 +04:00
|
|
|
virtual void _ReservedArchivable3();
|
2010-07-08 18:54:25 +04:00
|
|
|
|
2010-07-16 20:49:42 +04:00
|
|
|
int32 fArchivingToken;
|
|
|
|
uint32 _reserved;
|
2010-07-08 18:54:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class BArchiver {
|
|
|
|
public:
|
2010-07-08 19:04:38 +04:00
|
|
|
BArchiver(BMessage* archive);
|
|
|
|
~BArchiver();
|
2010-07-08 18:54:25 +04:00
|
|
|
|
2010-07-08 19:04:38 +04:00
|
|
|
status_t AddArchivable(const char* name,
|
|
|
|
BArchivable* archivable, bool deep = true);
|
2010-07-08 18:54:25 +04:00
|
|
|
|
2010-07-16 20:49:42 +04:00
|
|
|
inline status_t GetTokenForArchivable(BArchivable* archivable,
|
|
|
|
int32& _token);
|
2010-07-08 19:04:38 +04:00
|
|
|
status_t GetTokenForArchivable(BArchivable* archivable,
|
2010-07-16 20:49:42 +04:00
|
|
|
bool deep, int32& _token);
|
2010-07-08 18:54:25 +04:00
|
|
|
|
2010-07-08 19:04:38 +04:00
|
|
|
bool IsArchived(BArchivable* archivable);
|
2010-07-16 20:49:42 +04:00
|
|
|
status_t Finish(status_t err = B_OK);
|
2010-07-08 19:04:38 +04:00
|
|
|
BMessage* ArchiveMessage() const;
|
2010-07-08 18:54:25 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class BArchivable;
|
|
|
|
|
2010-07-08 19:04:38 +04:00
|
|
|
BArchiver(); // not defined
|
|
|
|
BArchiver(const BArchiver&); // not defined
|
2010-07-08 18:54:25 +04:00
|
|
|
|
2010-07-08 19:04:38 +04:00
|
|
|
void RegisterArchivable(
|
|
|
|
const BArchivable* archivable);
|
2010-07-08 18:54:25 +04:00
|
|
|
|
2010-07-08 19:04:38 +04:00
|
|
|
BArchiveManager* fManager;
|
|
|
|
BMessage* fArchive;
|
|
|
|
bool fFinished;
|
2010-07-16 20:49:42 +04:00
|
|
|
uint32 _reserved[2];
|
2010-07-08 18:54:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class BUnarchiver {
|
|
|
|
public:
|
2010-07-16 20:49:42 +04:00
|
|
|
enum ownership_policy {
|
|
|
|
B_ASSUME_OWNERSHIP,
|
|
|
|
B_DONT_ASSUME_OWNERSHIP
|
|
|
|
};
|
|
|
|
|
2010-07-08 18:54:25 +04:00
|
|
|
BUnarchiver(const BMessage* archive);
|
|
|
|
~BUnarchiver();
|
|
|
|
|
2010-07-16 20:49:42 +04:00
|
|
|
template<class T>
|
|
|
|
inline status_t GetObject(int32 token, T*& object);
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
status_t GetObject(int32 token, ownership_policy owning,
|
|
|
|
T*& object);
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline status_t FindObject(const char* name, T*& object);
|
2010-07-08 18:54:25 +04:00
|
|
|
|
2010-07-16 20:49:42 +04:00
|
|
|
template<class T>
|
|
|
|
inline status_t FindObject(const char* name,
|
|
|
|
ownership_policy owning,
|
|
|
|
T*& object);
|
2010-07-08 18:54:25 +04:00
|
|
|
|
2010-07-16 20:49:42 +04:00
|
|
|
template<class T>
|
|
|
|
inline status_t FindObject(const char* name,
|
|
|
|
int32 index, T*& object);
|
2010-07-08 18:54:25 +04:00
|
|
|
|
2010-07-16 20:49:42 +04:00
|
|
|
template<class T>
|
|
|
|
status_t FindObject(const char* name, int32 index,
|
|
|
|
ownership_policy owning, T*& object);
|
|
|
|
|
|
|
|
inline status_t EnsureUnarchived(const char* name,
|
2010-07-08 18:54:25 +04:00
|
|
|
int32 index = 0);
|
2010-07-16 20:49:42 +04:00
|
|
|
inline status_t EnsureUnarchived(int32 token);
|
2010-07-08 18:54:25 +04:00
|
|
|
|
|
|
|
bool IsInstantiated(int32 token);
|
|
|
|
bool IsInstantiated(const char* name,
|
|
|
|
int32 index = 0);
|
|
|
|
|
2010-07-16 20:49:42 +04:00
|
|
|
status_t Finish(status_t err = B_OK);
|
2010-07-08 18:54:25 +04:00
|
|
|
const BMessage* ArchiveMessage() const;
|
|
|
|
|
2010-07-16 20:49:42 +04:00
|
|
|
void AssumeOwnership(BArchivable* archivable);
|
|
|
|
void RelinquishOwnership(BArchivable* archivable);
|
|
|
|
|
2010-07-26 12:02:33 +04:00
|
|
|
static bool IsArchiveManaged(const BMessage* archive);
|
2010-07-08 18:54:25 +04:00
|
|
|
static BMessage* PrepareArchive(BMessage*& archive);
|
2010-07-08 19:04:38 +04:00
|
|
|
|
2010-07-16 20:49:42 +04:00
|
|
|
template<class T>
|
|
|
|
static status_t InstantiateObject(BMessage* archive,
|
|
|
|
T*& object);
|
|
|
|
|
2010-07-08 18:54:25 +04:00
|
|
|
private:
|
|
|
|
friend class BArchivable;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2010-07-08 18:54:25 +04:00
|
|
|
BUnarchiver(); // not defined
|
|
|
|
BUnarchiver(const BUnarchiver&); // not defined
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2010-07-08 18:54:25 +04:00
|
|
|
void RegisterArchivable(BArchivable* archivable);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2010-07-16 20:49:42 +04:00
|
|
|
inline void _CallDebuggerIfManagerNull();
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2010-07-08 18:54:25 +04:00
|
|
|
BUnarchiveManager* fManager;
|
|
|
|
const BMessage* fArchive;
|
|
|
|
bool fFinished;
|
2010-07-16 20:49:42 +04:00
|
|
|
uint32 _reserved[2];
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-01-26 19:36:29 +03:00
|
|
|
// global functions
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2007-01-26 19:36:29 +03:00
|
|
|
typedef BArchivable* (*instantiation_func)(BMessage*);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2010-07-08 18:54:25 +04:00
|
|
|
BArchivable* instantiate_object(BMessage* from, image_id* id);
|
|
|
|
BArchivable* instantiate_object(BMessage* from);
|
2007-01-26 19:36:29 +03:00
|
|
|
bool validate_instantiation(BMessage* from, const char* className);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2007-01-26 19:36:29 +03:00
|
|
|
instantiation_func find_instantiation_func(const char* className,
|
|
|
|
const char* signature);
|
|
|
|
instantiation_func find_instantiation_func(const char* className);
|
|
|
|
instantiation_func find_instantiation_func(BMessage* archive);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2010-07-16 20:49:42 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
BArchiver::GetTokenForArchivable(BArchivable* archivable, int32& _token)
|
|
|
|
{
|
|
|
|
return GetTokenForArchivable(archivable, true, _token);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
status_t BUnarchiver::FindObject<BArchivable>(const char* name, int32 index,
|
|
|
|
ownership_policy owning, BArchivable*& archivable);
|
|
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
status_t
|
|
|
|
BUnarchiver::FindObject(const char* name, int32 index,
|
|
|
|
ownership_policy owning, T*& object)
|
|
|
|
{
|
|
|
|
object = NULL;
|
|
|
|
|
|
|
|
BArchivable* interim;
|
|
|
|
status_t err = FindObject(name, index, owning, interim);
|
|
|
|
|
|
|
|
if (err == B_OK && interim) {
|
|
|
|
object = dynamic_cast<T*>(interim);
|
|
|
|
if (!object) {
|
|
|
|
err = B_BAD_TYPE;
|
|
|
|
// we will not be deleting this item, but it must be deleted
|
|
|
|
if (owning == B_ASSUME_OWNERSHIP)
|
|
|
|
RelinquishOwnership(interim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
status_t
|
|
|
|
BUnarchiver::GetObject<BArchivable>(int32 token,
|
|
|
|
ownership_policy owning, BArchivable*& object);
|
|
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
status_t
|
|
|
|
BUnarchiver::GetObject(int32 token, ownership_policy owning, T*& object)
|
|
|
|
{
|
|
|
|
object = NULL;
|
|
|
|
|
|
|
|
BArchivable* interim;
|
|
|
|
status_t err = GetObject(token, owning, interim);
|
|
|
|
|
|
|
|
if (err == B_OK && interim) {
|
|
|
|
object = dynamic_cast<T*>(interim);
|
|
|
|
if (!object) {
|
|
|
|
err = B_BAD_TYPE;
|
|
|
|
// we will not be deleting this item, but it must be deleted
|
|
|
|
if (owning == B_ASSUME_OWNERSHIP)
|
|
|
|
RelinquishOwnership(interim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
status_t
|
|
|
|
BUnarchiver::GetObject(int32 token, T*& object)
|
|
|
|
{
|
|
|
|
return GetObject<T>(token, B_ASSUME_OWNERSHIP, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
status_t
|
|
|
|
BUnarchiver::FindObject(const char* name, ownership_policy owning, T*& object)
|
|
|
|
{
|
|
|
|
return FindObject(name, 0, owning, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
status_t
|
|
|
|
BUnarchiver::FindObject(const char* name, T*& object)
|
|
|
|
{
|
|
|
|
return FindObject<T>(name, 0, B_ASSUME_OWNERSHIP, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
status_t
|
|
|
|
BUnarchiver::FindObject(const char* name,
|
|
|
|
int32 index, T*& object)
|
|
|
|
{
|
|
|
|
return FindObject(name, index, B_ASSUME_OWNERSHIP, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
BUnarchiver::EnsureUnarchived(int32 token)
|
|
|
|
{
|
|
|
|
BArchivable* dummy;
|
|
|
|
return GetObject(token, B_DONT_ASSUME_OWNERSHIP, dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
BUnarchiver::EnsureUnarchived(const char* name, int32 index)
|
|
|
|
{
|
|
|
|
BArchivable* dummy;
|
|
|
|
return FindObject(name, index, B_DONT_ASSUME_OWNERSHIP, dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
status_t
|
|
|
|
BUnarchiver::InstantiateObject<BArchivable>(BMessage* from,
|
|
|
|
BArchivable*& object);
|
|
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
status_t
|
|
|
|
BUnarchiver::InstantiateObject(BMessage* archive, T*& object)
|
|
|
|
{
|
|
|
|
object = NULL;
|
|
|
|
|
|
|
|
BArchivable* interim;
|
|
|
|
status_t err = InstantiateObject(archive, interim);
|
|
|
|
if (err != B_OK || interim == NULL)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
object = dynamic_cast<T*>(interim);
|
|
|
|
if (object == NULL) {
|
|
|
|
delete interim;
|
|
|
|
return B_BAD_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#endif // _ARCHIVABLE_H
|