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