xref: /haiku/src/tests/kits/app/bmessage/MessageBStringItemTest.h (revision 169ae2662c992c0258b95ee16b0e5e71035a1075)
1 //------------------------------------------------------------------------------
2 //	MessageStringItemTest.h
3 //
4 //------------------------------------------------------------------------------
5 
6 #ifndef MESSAGESTRINGITEMTEST_H
7 #define MESSAGESTRINGITEMTEST_H
8 
9 // Standard Includes -----------------------------------------------------------
10 
11 // System Includes -------------------------------------------------------------
12 #include <Debug.h>
13 #include <String.h>
14 
15 // Project Includes ------------------------------------------------------------
16 
17 // Local Includes --------------------------------------------------------------
18 #include "MessageItemTest.h"
19 
20 // Local Defines ---------------------------------------------------------------
21 
22 // Globals ---------------------------------------------------------------------
23 
24 struct TBStringFuncPolicy
25 {
26 	static status_t Add(BMessage& msg, const char* name, BString& data);
27 	static status_t Find(BMessage& msg, const char* name, int32 index,
28 						 BString* data);
29 	static status_t ShortFind(BMessage& msg, const char* name, BString* data);
30 	static BString QuickFind(BMessage& msg, const char* name, int32 index);
31 	static bool Has(BMessage& msg, const char* name, int32 index);
32 	static status_t Replace(BMessage& msg, const char* name, int32 index,
33 						 BString& data);
34 	static status_t AddData(BMessage& msg, const char* name, type_code type,
35 						 const BString* data, ssize_t size, bool);
36 	static status_t FindData(BMessage& msg, const char* name, type_code type,
37 						  int32 index, const void** data, ssize_t* size);
38 
39 	private:
40 		static BString sStr;
41 };
42 BString TBStringFuncPolicy::sStr;
43 //------------------------------------------------------------------------------
Add(BMessage & msg,const char * name,BString & data)44 status_t TBStringFuncPolicy::Add(BMessage& msg, const char* name,
45 									 BString& data)
46 {
47 	return msg.AddString(name, data);
48 }
49 //------------------------------------------------------------------------------
Find(BMessage & msg,const char * name,int32 index,BString * data)50 status_t TBStringFuncPolicy::Find(BMessage& msg, const char* name,
51 									  int32 index, BString* data)
52 {
53 	return msg.FindString(name, index, data);
54 }
55 //------------------------------------------------------------------------------
ShortFind(BMessage & msg,const char * name,BString * data)56 inline status_t TBStringFuncPolicy::ShortFind(BMessage& msg, const char* name,
57 											  BString* data)
58 {
59 	return msg.FindString(name, data);
60 }
61 //------------------------------------------------------------------------------
QuickFind(BMessage & msg,const char * name,int32 index)62 BString TBStringFuncPolicy::QuickFind(BMessage& msg, const char* name,
63 										  int32 index)
64 {
65 	BString data;
66 	msg.FindString(name, index, &data);
67 	return data;
68 }
69 //------------------------------------------------------------------------------
Has(BMessage & msg,const char * name,int32 index)70 bool TBStringFuncPolicy::Has(BMessage& msg, const char* name,
71 								 int32 index)
72 {
73 	return msg.HasString(name, index);
74 }
75 //------------------------------------------------------------------------------
Replace(BMessage & msg,const char * name,int32 index,BString & data)76 status_t TBStringFuncPolicy::Replace(BMessage& msg, const char* name,
77 										 int32 index, BString& data)
78 {
79 	return msg.ReplaceString(name, index, data);
80 }
81 //------------------------------------------------------------------------------
AddData(BMessage & msg,const char * name,type_code type,const BString * data,ssize_t size,bool)82 status_t TBStringFuncPolicy::AddData(BMessage& msg, const char* name,
83 										 type_code type, const BString* data,
84 										 ssize_t size, bool)
85 {
86 	return msg.AddData(name, type, (const void*)data->String(), size,
87 					   false);
88 }
89 //------------------------------------------------------------------------------
FindData(BMessage & msg,const char * name,type_code type,int32 index,const void ** data,ssize_t * size)90 status_t TBStringFuncPolicy::FindData(BMessage& msg, const char* name,
91 										  type_code type, int32 index,
92 										  const void** data, ssize_t* size)
93 {
94 	*data = NULL;
95 	char* ptr;
96 	status_t err = msg.FindData(name, type, index, (const void**)&ptr, size);
97 	if (!err)
98 	{
99 		sStr = ptr;
100 		*(BString**)data = &sStr;
101 	}
102 
103 	return err;
104 }
105 //------------------------------------------------------------------------------
106 
107 
108 struct TBStringInitPolicy : public ArrayTypeBase<BString>
109 {
ZeroTBStringInitPolicy110 	inline static BString Zero()	{ return BString(); }
Test1TBStringInitPolicy111 	inline static BString Test1()	{ return "Test1"; }
Test2TBStringInitPolicy112 	inline static BString Test2()	{ return "Supercalafragilistricexpialadocious"; }
SizeOfTBStringInitPolicy113 	inline static size_t SizeOf(const BString& s)	{ return s.Length() + 1; }
ArrayTBStringInitPolicy114 	inline static ArrayType Array()
115 	{
116 		ArrayType array;
117 		array.push_back(Zero());
118 		array.push_back(Test1());
119 		array.push_back(Test2());
120 		return array;
121 	}
122 };
123 //------------------------------------------------------------------------------
124 struct TBStringAssertPolicy
125 {
ZeroTBStringAssertPolicy126 	inline static BString	Zero()				{ return BString(); }
InvalidTBStringAssertPolicy127 	inline static BString	Invalid()			{ return BString(); }
128 	 static bool			Size(size_t size, BString& msg)
129 		;//{ return size == msg.FlattenedSize(); }
130 };
Size(size_t size,BString & data)131 bool TBStringAssertPolicy::Size(size_t size, BString& data)
132 {
133 	return size == (size_t)data.Length() + 1;
134 }
135 //------------------------------------------------------------------------------
136 typedef TMessageItemTest
137 <
138 	BString,
139 	B_STRING_TYPE,
140 	TBStringFuncPolicy,
141 	TBStringInitPolicy,
142 	TBStringAssertPolicy
143 >
144 TMessageBStringItemTest;
145 
146 #endif	// MESSAGESTRINGITEMTEST_H
147 
148 /*
149  * $Log $
150  *
151  * $Id  $
152  *
153  */
154 
155