From 2d350c10106a6241284a356f5706322c36b0d2a6 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Fri, 24 Jul 2009 02:21:06 +0000 Subject: [PATCH] Utility functionality for archiving/unarchiving. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@31726 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/debugger/util/ArchivingUtils.cpp | 37 +++++++++++++ src/apps/debugger/util/ArchivingUtils.h | 66 +++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/apps/debugger/util/ArchivingUtils.cpp create mode 100644 src/apps/debugger/util/ArchivingUtils.h diff --git a/src/apps/debugger/util/ArchivingUtils.cpp b/src/apps/debugger/util/ArchivingUtils.cpp new file mode 100644 index 0000000000..1f8e21ab1b --- /dev/null +++ b/src/apps/debugger/util/ArchivingUtils.cpp @@ -0,0 +1,37 @@ +/* + * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ + + +#include "ArchivingUtils.h" + +#include + + +/*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); +} diff --git a/src/apps/debugger/util/ArchivingUtils.h b/src/apps/debugger/util/ArchivingUtils.h new file mode 100644 index 0000000000..1f5e703b72 --- /dev/null +++ b/src/apps/debugger/util/ArchivingUtils.h @@ -0,0 +1,66 @@ +/* + * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef ARCHIVABLE_UTILS_H +#define ARCHIVABLE_UTILS_H + + +#include + + +class ArchivingUtils { +public: + template + static ObjectType* CastOrDelete(BArchivable* archivable); + + template + 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 + static ObjectType* UnarchiveChild(const BMessage& archive, + const char* fieldName, int32 index = 0); +}; + + +template +/*static*/ ObjectType* +ArchivingUtils::CastOrDelete(BArchivable* archivable) +{ + if (archivable == NULL) + return NULL; + + ObjectType* object = dynamic_cast(archivable); + if (object == NULL) + delete archivable; + + return object; +} + + +template +/*static*/ ObjectType* +ArchivingUtils::Unarchive(const BMessage& archive) +{ + return CastOrDelete(instantiate_object( + const_cast(&archive))); +} + + +template +/*static*/ ObjectType* +ArchivingUtils::UnarchiveChild(const BMessage& archive, const char* fieldName, + int32 index) +{ + return CastOrDelete(UnarchiveChild(archive, fieldName, index)); +} + + + +#endif // ARCHIVABLE_UTILS_H