xref: /haiku/headers/build/os/app/Message.h (revision 56eb8e78cc702792e3b032e3f5f45da9e5dbea9e)
1 /*
2  * Copyright 2005-2007, Haiku Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *	Michael Lotz <mmlr@mlotz.ch>
7  */
8 #ifndef _MESSAGE_H
9 #define _MESSAGE_H
10 
11 
12 #include <BeBuild.h>
13 #include <DataIO.h>
14 #include <Flattenable.h>
15 #include <OS.h>
16 #include <Rect.h>
17 
18 #include <AppDefs.h>		/* For convenience */
19 #include <TypeConstants.h>	/* For convenience */
20 
21 class BBlockCache;
22 class BMessenger;
23 class BHandler;
24 class BString;
25 struct entry_ref;
26 
27 
28 // Name lengths and Scripting specifiers
29 #define B_FIELD_NAME_LENGTH			255
30 #define B_PROPERTY_NAME_LENGTH		255
31 
32 enum {
33 	B_NO_SPECIFIER = 0,
34 	B_DIRECT_SPECIFIER = 1,
35 	B_INDEX_SPECIFIER,
36 	B_REVERSE_INDEX_SPECIFIER,
37 	B_RANGE_SPECIFIER,
38 	B_REVERSE_RANGE_SPECIFIER,
39 	B_NAME_SPECIFIER,
40 	B_ID_SPECIFIER,
41 
42 	B_SPECIFIERS_END = 128
43 	// app-defined specifiers start at B_SPECIFIERS_END + 1
44 };
45 
46 class BMessage {
47 	public:
48 		uint32			what;
49 
50 						BMessage();
51 						BMessage(uint32 what);
52 						BMessage(const BMessage &other);
53 		virtual			~BMessage();
54 
55 		BMessage		&operator=(const BMessage &other);
56 
57 		// Statistics and misc info
58 		status_t		GetInfo(type_code typeRequested, int32 index,
59 							char **nameFound, type_code *typeFound,
60 							int32 *countFound = NULL) const;
61 		status_t		GetInfo(const char *name, type_code *typeFound,
62 							int32 *countFound = NULL) const;
63 		status_t		GetInfo(const char *name, type_code *typeFound,
64 							bool *fixedSize) const;
65 
66 		int32			CountNames(type_code type) const;
67 		bool			IsEmpty() const;
68 		bool			IsSystem() const;
69 		bool			IsReply() const;
70 		void			PrintToStream() const;
71 
72 		status_t		Rename(const char *oldEntry, const char *newEntry);
73 
74 		// Delivery info
75 		bool			WasDelivered() const;
76 		bool			IsSourceWaiting() const;
77 		BMessenger		ReturnAddress() const;
78 		const BMessage	*Previous() const;
79 		bool			WasDropped() const;
80 		BPoint			DropPoint(BPoint *offset = NULL) const;
81 
82 		// Flattening data
83 		ssize_t			FlattenedSize() const;
84 		status_t		Flatten(char *buffer, ssize_t size) const;
85 		status_t		Flatten(BDataIO *stream, ssize_t *size = NULL) const;
86 		status_t		Unflatten(const char *flatBuffer);
87 		status_t		Unflatten(BDataIO *stream);
88 
89 		// Specifiers (scripting)
90 		status_t		AddSpecifier(const char *property);
91 		status_t		AddSpecifier(const char *property, int32 index);
92 		status_t		AddSpecifier(const char *property, int32 index, int32 range);
93 		status_t		AddSpecifier(const char *property, const char *name);
94 		status_t		AddSpecifier(const BMessage *specifier);
95 
96 		status_t		SetCurrentSpecifier(int32 index);
97 		status_t		GetCurrentSpecifier(int32 *index,
98 							BMessage *specifier = NULL, int32 *what = NULL,
99 							const char **property = NULL) const;
100 		bool			HasSpecifiers() const;
101 		status_t		PopSpecifier();
102 
103 		// Adding data
104 		status_t		AddRect(const char *name, BRect aRect);
105 		status_t		AddPoint(const char *name, BPoint aPoint);
106 		status_t		AddString(const char *name, const char *aString);
107 		status_t		AddString(const char *name, const BString &aString);
108 		status_t		AddInt8(const char *name, int8 value);
109 		status_t		AddInt16(const char *name, int16 value);
110 		status_t		AddInt32(const char *name, int32 value);
111 		status_t		AddInt64(const char *name, int64 value);
112 		status_t		AddBool(const char *name, bool aBoolean);
113 		status_t		AddFloat(const char *name, float aFloat);
114 		status_t		AddDouble(const char *name, double aDouble);
115 		status_t		AddPointer(const char *name, const void *aPointer);
116 		status_t		AddMessenger(const char *name, BMessenger messenger);
117 		status_t		AddRef(const char *name, const entry_ref *ref);
118 		status_t		AddMessage(const char *name, const BMessage *message);
119 		status_t		AddFlat(const char *name, BFlattenable *object,
120 							int32 count = 1);
121 		status_t		AddData(const char *name, type_code type,
122 							const void *data, ssize_t numBytes,
123 							bool isFixedSize = true, int32 count = 1);
124 
125 		// Removing data
126 		status_t		RemoveData(const char *name, int32 index = 0);
127 		status_t		RemoveName(const char *name);
128 		status_t		MakeEmpty();
129 
130 		// Finding data
131 		status_t		FindRect(const char *name, BRect *rect) const;
132 		status_t		FindRect(const char *name, int32 index, BRect *rect) const;
133 		status_t		FindPoint(const char *name, BPoint *point) const;
134 		status_t		FindPoint(const char *name, int32 index, BPoint *point) const;
135 		status_t		FindString(const char *name, const char **string) const;
136 		status_t		FindString(const char *name, int32 index, const char **string) const;
137 		status_t		FindString(const char *name, BString *string) const;
138 		status_t		FindString(const char *name, int32 index, BString *string) const;
139 		status_t		FindInt8(const char *name, int8 *value) const;
140 		status_t		FindInt8(const char *name, int32 index, int8 *value) const;
141 		status_t		FindInt16(const char *name, int16 *value) const;
142 		status_t		FindInt16(const char *name, int32 index, int16 *value) const;
143 		status_t		FindInt32(const char *name, int32 *value) const;
144 		status_t		FindInt32(const char *name, int32 index, int32 *value) const;
145 		status_t		FindInt64(const char *name, int64 *value) const;
146 		status_t		FindInt64(const char *name, int32 index, int64 *value) const;
147 		status_t		FindBool(const char *name, bool *value) const;
148 		status_t		FindBool(const char *name, int32 index, bool *value) const;
149 		status_t		FindFloat(const char *name, float *value) const;
150 		status_t		FindFloat(const char *name, int32 index, float *value) const;
151 		status_t		FindDouble(const char *name, double *value) const;
152 		status_t		FindDouble(const char *name, int32 index, double *value) const;
153 		status_t		FindPointer(const char *name, void **pointer) const;
154 		status_t		FindPointer(const char *name, int32 index,  void **pointer) const;
155 		status_t		FindMessenger(const char *name, BMessenger *messenger) const;
156 		status_t		FindMessenger(const char *name, int32 index, BMessenger *messenger) const;
157 		status_t		FindRef(const char *name, entry_ref *ref) const;
158 		status_t		FindRef(const char *name, int32 index, entry_ref *ref) const;
159 		status_t		FindMessage(const char *name, BMessage *message) const;
160 		status_t		FindMessage(const char *name, int32 index, BMessage *message) const;
161 		status_t		FindFlat(const char *name, BFlattenable *object) const;
162 		status_t		FindFlat(const char *name, int32 index, BFlattenable *object) const;
163 		status_t		FindData(const char *name, type_code type,
164 							const void **data, ssize_t *numBytes) const;
165 		status_t		FindData(const char *name, type_code type, int32 index,
166 							const void **data, ssize_t *numBytes) const;
167 
168 		// Replacing data
169 		status_t		ReplaceRect(const char *name, BRect aRect);
170 		status_t		ReplaceRect(const char *name, int32 index, BRect aRect);
171 		status_t		ReplacePoint(const char *name, BPoint aPoint);
172 		status_t		ReplacePoint(const char *name, int32 index, BPoint aPoint);
173 		status_t		ReplaceString(const char *name, const char *aString);
174 		status_t		ReplaceString(const char *name, int32 index, const char *aString);
175 		status_t		ReplaceString(const char *name, const BString &aString);
176 		status_t		ReplaceString(const char *name, int32 index, const BString &aString);
177 		status_t		ReplaceInt8(const char *name, int8 value);
178 		status_t		ReplaceInt8(const char *name, int32 index, int8 value);
179 		status_t		ReplaceInt16(const char *name, int16 value);
180 		status_t		ReplaceInt16(const char *name, int32 index, int16 value);
181 		status_t		ReplaceInt32(const char *name, int32 value);
182 		status_t		ReplaceInt32(const char *name, int32 index, int32 value);
183 		status_t		ReplaceInt64(const char *name, int64 value);
184 		status_t		ReplaceInt64(const char *name, int32 index, int64 value);
185 		status_t		ReplaceBool(const char *name, bool aBoolean);
186 		status_t		ReplaceBool(const char *name, int32 index, bool aBoolean);
187 		status_t		ReplaceFloat(const char *name, float aFloat);
188 		status_t		ReplaceFloat(const char *name, int32 index, float aFloat);
189 		status_t		ReplaceDouble(const char *name, double aDouble);
190 		status_t		ReplaceDouble(const char *name, int32 index, double aDouble);
191 		status_t		ReplacePointer(const char *name, const void *pointer);
192 		status_t		ReplacePointer(const char *name,int32 index,const void *pointer);
193 		status_t		ReplaceMessenger(const char *name, BMessenger messenger);
194 		status_t		ReplaceMessenger(const char *name, int32 index, BMessenger messenger);
195 		status_t		ReplaceRef(	const char *name,const entry_ref *ref);
196 		status_t		ReplaceRef(	const char *name, int32 index, const entry_ref *ref);
197 		status_t		ReplaceMessage(const char *name, const BMessage *message);
198 		status_t		ReplaceMessage(const char *name, int32 index, const BMessage *message);
199 		status_t		ReplaceFlat(const char *name, BFlattenable *object);
200 		status_t		ReplaceFlat(const char *name, int32 index, BFlattenable *object);
201 		status_t		ReplaceData(const char *name, type_code type,
202 							const void *data, ssize_t numBytes);
203 		status_t		ReplaceData(const char *name, type_code type, int32 index,
204 							const void *data, ssize_t numBytes);
205 
206 		void			*operator new(size_t size);
207 		void			*operator new(size_t, void *pointer);
208 		void			operator delete(void *pointer, size_t size);
209 
210 		// Private, reserved, or obsolete
211 		bool			HasRect(const char *, int32 n = 0) const;
212 		bool			HasPoint(const char *, int32 n = 0) const;
213 		bool			HasString(const char *, int32 n = 0) const;
214 		bool			HasInt8(const char *, int32 n = 0) const;
215 		bool			HasInt16(const char *, int32 n = 0) const;
216 		bool			HasInt32(const char *, int32 n = 0) const;
217 		bool			HasInt64(const char *, int32 n = 0) const;
218 		bool			HasBool(const char *, int32 n = 0) const;
219 		bool			HasFloat(const char *, int32 n = 0) const;
220 		bool			HasDouble(const char *, int32 n = 0) const;
221 		bool			HasPointer(const char *, int32 n = 0) const;
222 		bool			HasMessenger(const char *, int32 n = 0) const;
223 		bool			HasRef(const char *, int32 n = 0) const;
224 		bool			HasMessage(const char *, int32 n = 0) const;
225 		bool			HasFlat(const char *, const BFlattenable *) const;
226 		bool			HasFlat(const char *, int32 n, const BFlattenable *) const;
227 		bool			HasData(const char *, type_code , int32 n = 0) const;
228 		BRect			FindRect(const char *, int32 n = 0) const;
229 		BPoint			FindPoint(const char *, int32 n = 0) const;
230 		const char		*FindString(const char *, int32 n = 0) const;
231 		int8			FindInt8(const char *, int32 n = 0) const;
232 		int16			FindInt16(const char *, int32 n = 0) const;
233 		int32			FindInt32(const char *, int32 n = 0) const;
234 		int64			FindInt64(const char *, int32 n = 0) const;
235 		bool			FindBool(const char *, int32 n = 0) const;
236 		float			FindFloat(const char *, int32 n = 0) const;
237 		double			FindDouble(const char *, int32 n = 0) const;
238 
239 		class Private;
240 		struct message_header;
241 		struct field_header;
242 
243 	private:
244 		friend class Private;
245 		friend class BMessageQueue;
246 
247 		status_t		_InitCommon();
248 		status_t		_InitHeader();
249 		status_t		_Clear();
250 
251 		status_t		_ResizeData(int32 offset, int32 change);
252 
253 		uint32			_HashName(const char* name) const;
254 		status_t		_FindField(const char* name, type_code type,
255 							field_header** _result) const;
256 		status_t		_AddField(const char* name, type_code type,
257 							bool isFixedSize, field_header** _result);
258 		status_t		_RemoveField(field_header* field);
259 
260 		ssize_t			_NativeFlattenedSize() const;
261 		status_t		_NativeFlatten(char *buffer, ssize_t size) const;
262 		status_t		_NativeFlatten(BDataIO *stream, ssize_t *size = NULL) const;
263 		void			_PrintToStream(const char* indent) const;
264 
265 	private:
266 		message_header*	fHeader;
267 		field_header*	fFields;
268 		uint8*			fData;
269 		area_id			fClonedArea;
270 
271 		mutable	BMessage* fOriginal;
272 
273 		BMessage*		fQueueLink;
274 			// fQueueLink is used by BMessageQueue to build a linked list
275 
276 						// deprecated
277 						BMessage(BMessage *message);
278 
279 		static void		_StaticCacheCleanup();
280 
281 		static BBlockCache* sMsgCache;
282 };
283 
284 #endif	// _MESSAGE_H
285