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