xref: /haiku/src/tests/kits/app/bmessage/MessageRefItemTest.h (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
1 //------------------------------------------------------------------------------
2 //	MessageRefItemTest.h
3 //
4 //------------------------------------------------------------------------------
5 
6 #ifndef MESSAGEREFITEMTEST_H
7 #define MESSAGEREFITEMTEST_H
8 
9 // Standard Includes -----------------------------------------------------------
10 
11 // System Includes -------------------------------------------------------------
12 #include <Entry.h>
13 #include <Path.h>
14 
15 // Project Includes ------------------------------------------------------------
16 
17 // Local Includes --------------------------------------------------------------
18 
19 // Local Defines ---------------------------------------------------------------
20 
21 // Globals ---------------------------------------------------------------------
22 
23 struct TRefFuncPolicy
24 {
25 	static status_t Add(BMessage& msg, const char* name, entry_ref& val)
26 	{
27 		return msg.AddRef(name, &val);
28 	}
29 	static status_t AddData(BMessage& msg, const char* name, type_code type,
30 							entry_ref* data, ssize_t size, bool);
31 	static status_t Find(BMessage& msg, const char* name, int32 index,
32 						 entry_ref* val)
33 	{
34 		return msg.FindRef(name, index, val);
35 	}
36 	static status_t ShortFind(BMessage& msg, const char* name, entry_ref* val)
37 	{
38 		return msg.FindRef(name, val);
39 	}
40 	static entry_ref QuickFind(BMessage& msg, const char* name, int32 index);
41 	static bool Has(BMessage& msg, const char* name, int32 index)
42 	{
43 		return msg.HasRef(name, index);
44 	}
45 	static status_t Replace(BMessage& msg, const char* name, int32 index,
46 							entry_ref& val)
47 	{
48 		return msg.ReplaceRef(name, index, &val);
49 	}
50 	static status_t FindData(BMessage& msg, const char* name, type_code type,
51 							 int32 index, const void** data, ssize_t* size);
52 
53 	private:
54 		static entry_ref sRef;
55 };
56 status_t TRefFuncPolicy::AddData(BMessage& msg, const char* name, type_code type,
57 								 entry_ref* data, ssize_t size, bool)
58 {
59 	BPath Path(data);
60 	status_t err = Path.InitCheck();
61 	char* buf = NULL;
62 //	if (!err)
63 	{
64 		buf = new char[size];
65 		err = Path.Flatten(buf, size);
66 	}
67 
68 	if (!err)
69 	{
70 		err = msg.AddData(name, type, buf, size, false);
71 	}
72 	delete[] buf;
73 	return err;
74 }
75 
76 entry_ref TRefFuncPolicy::QuickFind(BMessage &msg, const char *name, int32 index)
77 {
78 	//	TODO: make cooler
79 	//	There must be some 1337 template voodoo way of making the inclusion
80 	//	of this function conditional.  In the meantime, we offer this
81 	//	cheesy hack
82 	entry_ref ref;
83 	msg.FindRef(name, index, &ref);
84 	return ref;
85 }
86 
87 status_t TRefFuncPolicy::FindData(BMessage& msg, const char* name,
88 										 type_code type, int32 index,
89 										 const void** data, ssize_t* size)
90 {
91 	*data = NULL;
92 	char* ptr;
93 	status_t err = msg.FindData(name, type, index, (const void**)&ptr, size);
94 	if (!err)
95 	{
96 		BPath Path;
97 		err = Path.Unflatten(type, ptr, *size);
98 		if (!err)
99 		{
100 			if (Path.Path())
101 			{
102 				err = get_ref_for_path(Path.Path(), &sRef);
103 				if (!err)
104 				{
105 					*(entry_ref**)data = &sRef;
106 				}
107 			}
108 			else
109 			{
110 				sRef = entry_ref();
111 				*(entry_ref**)data = &sRef;
112 			}
113 		}
114 	}
115 	return err;
116 }
117 entry_ref TRefFuncPolicy::sRef;
118 //------------------------------------------------------------------------------
119 struct TRefInitPolicy : public ArrayTypeBase<entry_ref>
120 {
121 	inline static entry_ref Zero()	{ return entry_ref(); }
122 	static entry_ref Test1()
123 	{
124 		entry_ref ref;
125 		get_ref_for_path("/boot/beos/apps/camera", &ref);
126 		return ref;
127 	}
128 	static entry_ref Test2()
129 	{
130 		entry_ref ref;
131 		get_ref_for_path("/boot/develop/headers/be/Be.h", &ref);
132 		return ref;
133 	}
134 	static size_t SizeOf(const entry_ref& data)
135 	{
136 		BPath Path(&data);
137 		return Path.FlattenedSize();
138 	}
139 	inline static ArrayType Array()
140 	{
141 		ArrayType array;
142 		array.push_back(Zero());
143 		array.push_back(Test1());
144 		array.push_back(Test2());
145 		return array;
146 	}
147 };
148 //------------------------------------------------------------------------------
149 struct TRefAssertPolicy
150 {
151 	inline static entry_ref Zero()		{ return TRefInitPolicy::Zero(); }
152 	inline static entry_ref Invalid()	{ return TRefInitPolicy::Zero();}
153 // TODO: fix
154 	static bool		Size(size_t size, entry_ref& ref)
155 	{
156 		return size == TRefInitPolicy::SizeOf(ref);
157 	}
158 };
159 //------------------------------------------------------------------------------
160 typedef TMessageItemTest
161 <
162 	entry_ref,
163 	B_REF_TYPE,
164 	TRefFuncPolicy,
165 	TRefInitPolicy,
166 	TRefAssertPolicy
167 >
168 TMessageRefItemTest;
169 
170 #endif	// MESSAGEREFITEMTEST_H
171 
172 /*
173  * $Log $
174  *
175  * $Id  $
176  *
177  */
178 
179