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;
25struct 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
32enum {
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
46class 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		AddUInt8(const char *name, uint8 value);
110		status_t		AddInt16(const char *name, int16 value);
111		status_t		AddUInt16(const char *name, uint16 value);
112		status_t		AddInt32(const char *name, int32 value);
113		status_t		AddUInt32(const char *name, uint32 value);
114		status_t		AddInt64(const char *name, int64 value);
115		status_t		AddUInt64(const char *name, uint64 value);
116		status_t		AddBool(const char *name, bool aBoolean);
117		status_t		AddFloat(const char *name, float aFloat);
118		status_t		AddDouble(const char *name, double aDouble);
119		status_t		AddPointer(const char *name, const void *aPointer);
120		status_t		AddMessenger(const char *name, BMessenger messenger);
121		status_t		AddRef(const char *name, const entry_ref *ref);
122		status_t		AddMessage(const char *name, const BMessage *message);
123		status_t		AddFlat(const char *name, BFlattenable *object,
124							int32 count = 1);
125		status_t		AddData(const char *name, type_code type,
126							const void *data, ssize_t numBytes,
127							bool isFixedSize = true, int32 count = 1);
128
129		// Removing data
130		status_t		RemoveData(const char *name, int32 index = 0);
131		status_t		RemoveName(const char *name);
132		status_t		MakeEmpty();
133
134		// Finding data
135		status_t		FindRect(const char *name, BRect *rect) const;
136		status_t		FindRect(const char *name, int32 index, BRect *rect) const;
137		status_t		FindPoint(const char *name, BPoint *point) const;
138		status_t		FindPoint(const char *name, int32 index, BPoint *point) const;
139		status_t		FindString(const char *name, const char **string) const;
140		status_t		FindString(const char *name, int32 index, const char **string) const;
141		status_t		FindString(const char *name, BString *string) const;
142		status_t		FindString(const char *name, int32 index, BString *string) const;
143		status_t		FindInt8(const char *name, int8 *value) const;
144		status_t		FindInt8(const char *name, int32 index, int8 *value) const;
145		status_t		FindUInt8(const char *name, uint8 *value) const;
146		status_t		FindUInt8(const char *name, int32 index, uint8 *value) const;
147		status_t		FindInt16(const char *name, int16 *value) const;
148		status_t		FindInt16(const char *name, int32 index, int16 *value) const;
149		status_t		FindUInt16(const char *name, uint16 *value) const;
150		status_t		FindUInt16(const char *name, int32 index, uint16 *value) const;
151		status_t		FindInt32(const char *name, int32 *value) const;
152		status_t		FindInt32(const char *name, int32 index, int32 *value) const;
153		status_t		FindUInt32(const char *name, uint32 *value) const;
154		status_t		FindUInt32(const char *name, int32 index, uint32 *value) const;
155		status_t		FindInt64(const char *name, int64 *value) const;
156		status_t		FindInt64(const char *name, int32 index, int64 *value) const;
157		status_t		FindUInt64(const char *name, uint64 *value) const;
158		status_t		FindUInt64(const char *name, int32 index, uint64 *value) const;
159		status_t		FindBool(const char *name, bool *value) const;
160		status_t		FindBool(const char *name, int32 index, bool *value) const;
161		status_t		FindFloat(const char *name, float *value) const;
162		status_t		FindFloat(const char *name, int32 index, float *value) const;
163		status_t		FindDouble(const char *name, double *value) const;
164		status_t		FindDouble(const char *name, int32 index, double *value) const;
165		status_t		FindPointer(const char *name, void **pointer) const;
166		status_t		FindPointer(const char *name, int32 index,  void **pointer) const;
167		status_t		FindMessenger(const char *name, BMessenger *messenger) const;
168		status_t		FindMessenger(const char *name, int32 index, BMessenger *messenger) const;
169		status_t		FindRef(const char *name, entry_ref *ref) const;
170		status_t		FindRef(const char *name, int32 index, entry_ref *ref) const;
171		status_t		FindMessage(const char *name, BMessage *message) const;
172		status_t		FindMessage(const char *name, int32 index, BMessage *message) const;
173		status_t		FindFlat(const char *name, BFlattenable *object) const;
174		status_t		FindFlat(const char *name, int32 index, BFlattenable *object) const;
175		status_t		FindData(const char *name, type_code type,
176							const void **data, ssize_t *numBytes) const;
177		status_t		FindData(const char *name, type_code type, int32 index,
178							const void **data, ssize_t *numBytes) const;
179
180		// Replacing data
181		status_t		ReplaceRect(const char *name, BRect aRect);
182		status_t		ReplaceRect(const char *name, int32 index, BRect aRect);
183		status_t		ReplacePoint(const char *name, BPoint aPoint);
184		status_t		ReplacePoint(const char *name, int32 index, BPoint aPoint);
185		status_t		ReplaceString(const char *name, const char *aString);
186		status_t		ReplaceString(const char *name, int32 index, const char *aString);
187		status_t		ReplaceString(const char *name, const BString &aString);
188		status_t		ReplaceString(const char *name, int32 index, const BString &aString);
189		status_t		ReplaceInt8(const char *name, int8 value);
190		status_t		ReplaceInt8(const char *name, int32 index, int8 value);
191		status_t		ReplaceUInt8(const char *name, uint8 value);
192		status_t		ReplaceUInt8(const char *name, int32 index, uint8 value);
193		status_t		ReplaceInt16(const char *name, int16 value);
194		status_t		ReplaceInt16(const char *name, int32 index, int16 value);
195		status_t		ReplaceUInt16(const char *name, uint16 value);
196		status_t		ReplaceUInt16(const char *name, int32 index, uint16 value);
197		status_t		ReplaceInt32(const char *name, int32 value);
198		status_t		ReplaceInt32(const char *name, int32 index, int32 value);
199		status_t		ReplaceUInt32(const char *name, uint32 value);
200		status_t		ReplaceUInt32(const char *name, int32 index, uint32 value);
201		status_t		ReplaceInt64(const char *name, int64 value);
202		status_t		ReplaceInt64(const char *name, int32 index, int64 value);
203		status_t		ReplaceUInt64(const char *name, uint64 value);
204		status_t		ReplaceUInt64(const char *name, int32 index, uint64 value);
205		status_t		ReplaceBool(const char *name, bool aBoolean);
206		status_t		ReplaceBool(const char *name, int32 index, bool aBoolean);
207		status_t		ReplaceFloat(const char *name, float aFloat);
208		status_t		ReplaceFloat(const char *name, int32 index, float aFloat);
209		status_t		ReplaceDouble(const char *name, double aDouble);
210		status_t		ReplaceDouble(const char *name, int32 index, double aDouble);
211		status_t		ReplacePointer(const char *name, const void *pointer);
212		status_t		ReplacePointer(const char *name,int32 index,const void *pointer);
213		status_t		ReplaceMessenger(const char *name, BMessenger messenger);
214		status_t		ReplaceMessenger(const char *name, int32 index, BMessenger messenger);
215		status_t		ReplaceRef(	const char *name,const entry_ref *ref);
216		status_t		ReplaceRef(	const char *name, int32 index, const entry_ref *ref);
217		status_t		ReplaceMessage(const char *name, const BMessage *message);
218		status_t		ReplaceMessage(const char *name, int32 index, const BMessage *message);
219		status_t		ReplaceFlat(const char *name, BFlattenable *object);
220		status_t		ReplaceFlat(const char *name, int32 index, BFlattenable *object);
221		status_t		ReplaceData(const char *name, type_code type,
222							const void *data, ssize_t numBytes);
223		status_t		ReplaceData(const char *name, type_code type, int32 index,
224							const void *data, ssize_t numBytes);
225
226		// Comparing data - Haiku experimental API
227		bool			HasSameData(const BMessage &other,
228							bool ignoreFieldOrder = true, bool deep = false) const;
229
230		void			*operator new(size_t size);
231		void			*operator new(size_t, void *pointer);
232		void			operator delete(void *pointer, size_t size);
233
234		// Private, reserved, or obsolete
235		bool			HasRect(const char *, int32 n = 0) const;
236		bool			HasPoint(const char *, int32 n = 0) const;
237		bool			HasString(const char *, int32 n = 0) const;
238		bool			HasInt8(const char *, int32 n = 0) const;
239		bool			HasUInt8(const char *, int32 n = 0) const;
240		bool			HasInt16(const char *, int32 n = 0) const;
241		bool			HasUInt16(const char *, int32 n = 0) const;
242		bool			HasInt32(const char *, int32 n = 0) const;
243		bool			HasUInt32(const char *, int32 n = 0) const;
244		bool			HasInt64(const char *, int32 n = 0) const;
245		bool			HasUInt64(const char *, int32 n = 0) const;
246		bool			HasBool(const char *, int32 n = 0) const;
247		bool			HasFloat(const char *, int32 n = 0) const;
248		bool			HasDouble(const char *, int32 n = 0) const;
249		bool			HasPointer(const char *, int32 n = 0) const;
250		bool			HasMessenger(const char *, int32 n = 0) const;
251		bool			HasRef(const char *, int32 n = 0) const;
252		bool			HasMessage(const char *, int32 n = 0) const;
253		bool			HasFlat(const char *, const BFlattenable *) const;
254		bool			HasFlat(const char *, int32 n, const BFlattenable *) const;
255		bool			HasData(const char *, type_code , int32 n = 0) const;
256		BRect			FindRect(const char *, int32 n = 0) const;
257		BPoint			FindPoint(const char *, int32 n = 0) const;
258		const char		*FindString(const char *, int32 n = 0) const;
259		int8			FindInt8(const char *, int32 n = 0) const;
260		int16			FindInt16(const char *, int32 n = 0) const;
261		int32			FindInt32(const char *, int32 n = 0) const;
262		int64			FindInt64(const char *, int32 n = 0) const;
263		bool			FindBool(const char *, int32 n = 0) const;
264		float			FindFloat(const char *, int32 n = 0) const;
265		double			FindDouble(const char *, int32 n = 0) const;
266
267		class Private;
268		struct message_header;
269		struct field_header;
270
271	private:
272		friend class Private;
273		friend class BMessageQueue;
274
275		status_t		_InitCommon(bool initHeader);
276		status_t		_InitHeader();
277		status_t		_Clear();
278
279		status_t		_ValidateMessage();
280
281		status_t		_ResizeData(uint32 offset, int32 change);
282
283		uint32			_HashName(const char* name) const;
284		status_t		_FindField(const char* name, type_code type,
285							field_header** _result) const;
286		status_t		_AddField(const char* name, type_code type,
287							bool isFixedSize, field_header** _result);
288		status_t		_RemoveField(field_header* field);
289
290		void			_PrintToStream(const char* indent) const;
291
292	private:
293		message_header*	fHeader;
294		field_header*	fFields;
295		uint8*			fData;
296
297		uint32			fFieldsAvailable;
298		size_t			fDataAvailable;
299
300		mutable	BMessage* fOriginal;
301
302		BMessage*		fQueueLink;
303			// fQueueLink is used by BMessageQueue to build a linked list
304
305		uint32			fReserved[9];
306
307						// deprecated
308						BMessage(BMessage *message);
309
310		virtual	void	_ReservedMessage1();
311		virtual	void	_ReservedMessage2();
312		virtual	void	_ReservedMessage3();
313
314		static BBlockCache* sMsgCache;
315};
316
317#endif	// _MESSAGE_H
318