1/* 2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5#ifndef ARCHIVABLE_UTILS_H 6#define ARCHIVABLE_UTILS_H 7 8 9#include <Archivable.h> 10 11 12class ArchivingUtils { 13public: 14 template<typename ObjectType> 15 static ObjectType* CastOrDelete(BArchivable* archivable); 16 17 template<typename ObjectType> 18 static ObjectType* Unarchive(const BMessage& archive); 19 20 static status_t ArchiveChild(BArchivable* object, 21 BMessage& parentArchive, 22 const char* fieldName); 23 static BArchivable* UnarchiveChild(const BMessage& parentArchive, 24 const char* fieldName, int32 index = 0); 25 26 template<typename ObjectType> 27 static ObjectType* UnarchiveChild(const BMessage& archive, 28 const char* fieldName, int32 index = 0); 29}; 30 31 32template<typename ObjectType> 33/*static*/ ObjectType* 34ArchivingUtils::CastOrDelete(BArchivable* archivable) 35{ 36 if (archivable == NULL) 37 return NULL; 38 39 ObjectType* object = dynamic_cast<ObjectType*>(archivable); 40 if (object == NULL) 41 delete archivable; 42 43 return object; 44} 45 46 47template<typename ObjectType> 48/*static*/ ObjectType* 49ArchivingUtils::Unarchive(const BMessage& archive) 50{ 51 return CastOrDelete<ObjectType>(instantiate_object( 52 const_cast<BMessage*>(&archive))); 53} 54 55 56template<typename ObjectType> 57/*static*/ ObjectType* 58ArchivingUtils::UnarchiveChild(const BMessage& archive, const char* fieldName, 59 int32 index) 60{ 61 return CastOrDelete<ObjectType>(UnarchiveChild(archive, fieldName, index)); 62} 63 64 65 66#endif // ARCHIVABLE_UTILS_H 67