Utility functionality for archiving/unarchiving.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@31726 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2009, Ingo Weinhold, [email protected]. All Rights Reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "ArchivingUtils.h"
|
||||||
|
|
||||||
|
#include <Message.h>
|
||||||
|
|
||||||
|
|
||||||
|
/*static*/ status_t
|
||||||
|
ArchivingUtils::ArchiveChild(BArchivable* object, BMessage& parentArchive,
|
||||||
|
const char* fieldName)
|
||||||
|
{
|
||||||
|
if (object == NULL)
|
||||||
|
return B_BAD_VALUE;
|
||||||
|
|
||||||
|
BMessage archive;
|
||||||
|
status_t error = object->Archive(&archive, true);
|
||||||
|
if (error != B_OK)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
return parentArchive.AddMessage(fieldName, &archive);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*static*/ BArchivable*
|
||||||
|
ArchivingUtils::UnarchiveChild(const BMessage& parentArchive,
|
||||||
|
const char* fieldName, int32 index)
|
||||||
|
{
|
||||||
|
BMessage archive;
|
||||||
|
if (parentArchive.FindMessage(fieldName, index, &archive) != B_OK)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return instantiate_object(&archive);
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2009, Ingo Weinhold, [email protected]. All Rights Reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
#ifndef ARCHIVABLE_UTILS_H
|
||||||
|
#define ARCHIVABLE_UTILS_H
|
||||||
|
|
||||||
|
|
||||||
|
#include <Archivable.h>
|
||||||
|
|
||||||
|
|
||||||
|
class ArchivingUtils {
|
||||||
|
public:
|
||||||
|
template<typename ObjectType>
|
||||||
|
static ObjectType* CastOrDelete(BArchivable* archivable);
|
||||||
|
|
||||||
|
template<typename ObjectType>
|
||||||
|
static ObjectType* Unarchive(const BMessage& archive);
|
||||||
|
|
||||||
|
static status_t ArchiveChild(BArchivable* object,
|
||||||
|
BMessage& parentArchive,
|
||||||
|
const char* fieldName);
|
||||||
|
static BArchivable* UnarchiveChild(const BMessage& parentArchive,
|
||||||
|
const char* fieldName, int32 index = 0);
|
||||||
|
|
||||||
|
template<typename ObjectType>
|
||||||
|
static ObjectType* UnarchiveChild(const BMessage& archive,
|
||||||
|
const char* fieldName, int32 index = 0);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<typename ObjectType>
|
||||||
|
/*static*/ ObjectType*
|
||||||
|
ArchivingUtils::CastOrDelete(BArchivable* archivable)
|
||||||
|
{
|
||||||
|
if (archivable == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
ObjectType* object = dynamic_cast<ObjectType*>(archivable);
|
||||||
|
if (object == NULL)
|
||||||
|
delete archivable;
|
||||||
|
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename ObjectType>
|
||||||
|
/*static*/ ObjectType*
|
||||||
|
ArchivingUtils::Unarchive(const BMessage& archive)
|
||||||
|
{
|
||||||
|
return CastOrDelete<ObjectType>(instantiate_object(
|
||||||
|
const_cast<BMessage*>(&archive)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename ObjectType>
|
||||||
|
/*static*/ ObjectType*
|
||||||
|
ArchivingUtils::UnarchiveChild(const BMessage& archive, const char* fieldName,
|
||||||
|
int32 index)
|
||||||
|
{
|
||||||
|
return CastOrDelete<ObjectType>(UnarchiveChild(archive, fieldName, index));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif // ARCHIVABLE_UTILS_H
|
||||||
Reference in New Issue
Block a user