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