1 //------------------------------------------------------------------------------
2 // MessageMessageItemTest.h
4 //------------------------------------------------------------------------------
6 #ifndef MESSAGEMESSAGEITEMTEST_H
7 #define MESSAGEMESSAGEITEMTEST_H
9 // Standard Includes -----------------------------------------------------------
11 // System Includes -------------------------------------------------------------
14 // Project Includes ------------------------------------------------------------
16 // Local Includes --------------------------------------------------------------
17 #include "MessageItemTest.h"
19 // Local Defines ---------------------------------------------------------------
21 // Globals ---------------------------------------------------------------------
23 //------------------------------------------------------------------------------
24 struct TMessageFuncPolicy
26 static status_t
Add(BMessage
& msg
, const char* name
, BMessage
& val
);
27 static status_t
AddData(BMessage
& msg
, const char* name
, type_code type
,
28 BMessage
* data
, ssize_t size
, bool);
29 static status_t
Find(BMessage
& msg
, const char* name
, int32 index
,
31 static status_t
ShortFind(BMessage
& msg
, const char* name
, BMessage
* val
);
32 static BMessage
QuickFind(BMessage
& msg
, const char* name
, int32 index
);
33 static bool Has(BMessage
& msg
, const char* name
, int32 index
);
34 static status_t
Replace(BMessage
& msg
, const char* name
, int32 index
,
36 static status_t
FindData(BMessage
& msg
, const char* name
, type_code type
,
37 int32 index
, const void** data
, ssize_t
* size
);
42 BMessage
TMessageFuncPolicy::sMsg
;
43 //------------------------------------------------------------------------------
44 status_t
TMessageFuncPolicy::Add(BMessage
& msg
, const char* name
,
47 return msg
.AddMessage(name
, &val
);
49 //------------------------------------------------------------------------------
50 status_t
TMessageFuncPolicy::AddData(BMessage
& msg
, const char* name
,
51 type_code type
, BMessage
* data
,
54 char* buf
= new char[size
];
55 status_t err
= data
->Flatten(buf
, size
);
58 err
= msg
.AddData(name
, type
, buf
, size
, false);
63 //------------------------------------------------------------------------------
64 inline status_t
TMessageFuncPolicy::Find(BMessage
& msg
, const char* name
,
65 int32 index
, BMessage
* val
)
67 return msg
.FindMessage(name
, index
, val
);
69 //------------------------------------------------------------------------------
70 inline status_t
TMessageFuncPolicy::ShortFind(BMessage
& msg
, const char* name
,
73 return msg
.FindMessage(name
, val
);
75 //------------------------------------------------------------------------------
76 BMessage
TMessageFuncPolicy::QuickFind(BMessage
& msg
, const char* name
,
80 msg
.FindMessage(name
, index
, &val
);
83 //------------------------------------------------------------------------------
84 inline bool TMessageFuncPolicy::Has(BMessage
& msg
, const char* name
,
87 return msg
.HasMessage(name
, index
);
89 //------------------------------------------------------------------------------
90 inline status_t
TMessageFuncPolicy::Replace(BMessage
& msg
, const char* name
,
91 int32 index
, BMessage
& val
)
93 return msg
.ReplaceMessage(name
, index
, &val
);
95 //------------------------------------------------------------------------------
96 inline status_t
TMessageFuncPolicy::FindData(BMessage
& msg
, const char* name
,
97 type_code type
, int32 index
,
98 const void** data
, ssize_t
* size
)
102 status_t err
= msg
.FindData(name
, type
, index
, (const void**)&ptr
, size
);
105 err
= sMsg
.Unflatten(ptr
);
108 *(BMessage
**)data
= &sMsg
;
112 // return (msg.*FindDataFunc)(name, type, index, data, size);
114 //------------------------------------------------------------------------------
117 //------------------------------------------------------------------------------
119 struct ArrayTypeBase
<BMessage
>
124 ArrayType() : array(NULL
), size(0) {;}
125 ArrayType(const ArrayType
& rhs
) : array(NULL
), size(0)
127 ArrayType(BMessage
* data
, uint32 len
) : array(NULL
), size(0)
128 { Assign(data
, len
); }
129 ~ArrayType() { if (array
) delete[] array
; }
131 ArrayType
& operator=(const ArrayType
& rhs
)
134 Assign(rhs
.array
, rhs
.size
);
138 uint32
Size() { return size
; }
139 BMessage
& operator[](int index
)
141 // We're just gonna let a segfault happen
146 void Assign(BMessage
* data
, uint32 len
)
152 array
= new BMessage
[Size()];
153 for (uint32 i
= 0; i
< Size(); ++i
)
160 typedef uint32 SizeType
;
161 static SizeType
Size(ArrayType
& array
) { return array
.Size(); }
163 //------------------------------------------------------------------------------
164 struct TMessageInitPolicy
: public ArrayTypeBase
<BMessage
>
166 inline static BMessage
Zero() { return BMessage(); }
167 static BMessage
Test1()
169 BMessage
msg('1234');
170 msg
.AddInt32("int32", 1234);
173 static BMessage
Test2()
175 BMessage
msg('5678');
176 msg
.AddString("string", "5678");
179 static size_t SizeOf(const BMessage
& data
)
181 return data
.FlattenedSize();
183 inline static ArrayType
Array()
189 return ArrayType(array
, 3);
192 //------------------------------------------------------------------------------
193 struct TMessageAssertPolicy
195 inline static BMessage
Zero() { return BMessage(); }
196 inline static BMessage
Invalid() { return BMessage(); }
197 static bool Size(size_t size
, BMessage
& msg
)
198 ;//{ return size == msg.FlattenedSize(); }
200 bool TMessageAssertPolicy::Size(size_t size
, BMessage
& msg
)
202 ssize_t msgSize
= msg
.FlattenedSize();
203 return size
== (size_t)msgSize
;
205 //------------------------------------------------------------------------------
207 struct TMessageItemComparePolicy
<BMessage
>
209 inline static bool Compare(const BMessage
& lhs
, const BMessage
& rhs
)
210 { return lhs
.what
== rhs
.what
; }
212 //------------------------------------------------------------------------------
213 typedef TMessageItemTest
221 TMessageMessageItemTest
;
222 //------------------------------------------------------------------------------
224 #endif // MESSAGEMESSAGEITEMTEST_H