xref: /haiku/src/kits/debugger/util/ArchivingUtils.h (revision fce4895d1884da5ae6fb299d23c735c598e690b1)
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 
12 class ArchivingUtils {
13 public:
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 
32 template<typename ObjectType>
33 /*static*/ ObjectType*
CastOrDelete(BArchivable * archivable)34 ArchivingUtils::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 
47 template<typename ObjectType>
48 /*static*/ ObjectType*
Unarchive(const BMessage & archive)49 ArchivingUtils::Unarchive(const BMessage& archive)
50 {
51 	return CastOrDelete<ObjectType>(instantiate_object(
52 		const_cast<BMessage*>(&archive)));
53 }
54 
55 
56 template<typename ObjectType>
57 /*static*/ ObjectType*
UnarchiveChild(const BMessage & archive,const char * fieldName,int32 index)58 ArchivingUtils::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