1 /* 2 * Copyright 2005-2010, 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 BAlignment; 22 class BBlockCache; 23 class BMessenger; 24 class BHandler; 25 class BString; 26 struct 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 33 enum { 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 47 class 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 status_t GetInfo(const char *name, type_code *typeFound, 67 int32 *countFound, 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 AddAlignment(const char* name, 121 const BAlignment& alignment); 122 status_t AddRect(const char *name, BRect aRect); 123 status_t AddPoint(const char *name, BPoint aPoint); 124 status_t AddSize(const char* name, BSize aSize); 125 status_t AddString(const char *name, const char *aString); 126 status_t AddString(const char *name, const BString &aString); 127 status_t AddInt8(const char *name, int8 value); 128 status_t AddUInt8(const char *name, uint8 value); 129 status_t AddInt16(const char *name, int16 value); 130 status_t AddUInt16(const char *name, uint16 value); 131 status_t AddInt32(const char *name, int32 value); 132 status_t AddUInt32(const char *name, uint32 value); 133 status_t AddInt64(const char *name, int64 value); 134 status_t AddUInt64(const char *name, uint64 value); 135 status_t AddBool(const char *name, bool aBoolean); 136 status_t AddFloat(const char *name, float aFloat); 137 status_t AddDouble(const char *name, double aDouble); 138 status_t AddPointer(const char *name, const void *aPointer); 139 status_t AddMessenger(const char *name, BMessenger messenger); 140 status_t AddRef(const char *name, const entry_ref *ref); 141 status_t AddMessage(const char *name, const BMessage *message); 142 status_t AddFlat(const char *name, BFlattenable *object, 143 int32 count = 1); 144 status_t AddData(const char *name, type_code type, 145 const void *data, ssize_t numBytes, 146 bool isFixedSize = true, int32 count = 1); 147 148 // Removing data 149 status_t RemoveData(const char *name, int32 index = 0); 150 status_t RemoveName(const char *name); 151 status_t MakeEmpty(); 152 153 // Finding data 154 status_t FindAlignment(const char* name, 155 BAlignment* alignment) const; 156 status_t FindAlignment(const char* name, int32 index, 157 BAlignment* alignment) const; 158 159 status_t FindRect(const char *name, BRect *rect) const; 160 status_t FindRect(const char *name, int32 index, BRect *rect) const; 161 status_t FindPoint(const char *name, BPoint *point) const; 162 status_t FindPoint(const char *name, int32 index, BPoint *point) const; 163 164 status_t FindSize(const char *name, BSize* size) const; 165 status_t FindSize(const char *name, 166 int32 index, BSize* size) const; 167 168 status_t FindString(const char *name, const char **string) const; 169 status_t FindString(const char *name, int32 index, const char **string) const; 170 status_t FindString(const char *name, BString *string) const; 171 status_t FindString(const char *name, int32 index, BString *string) const; 172 status_t FindInt8(const char *name, int8 *value) const; 173 status_t FindInt8(const char *name, int32 index, int8 *value) const; 174 status_t FindUInt8(const char *name, uint8 *value) const; 175 status_t FindUInt8(const char *name, int32 index, uint8 *value) const; 176 status_t FindInt16(const char *name, int16 *value) const; 177 status_t FindInt16(const char *name, int32 index, int16 *value) const; 178 status_t FindUInt16(const char *name, uint16 *value) const; 179 status_t FindUInt16(const char *name, int32 index, uint16 *value) const; 180 status_t FindInt32(const char *name, int32 *value) const; 181 status_t FindInt32(const char *name, int32 index, int32 *value) const; 182 status_t FindUInt32(const char *name, uint32 *value) const; 183 status_t FindUInt32(const char *name, int32 index, uint32 *value) const; 184 status_t FindInt64(const char *name, int64 *value) const; 185 status_t FindInt64(const char *name, int32 index, int64 *value) const; 186 status_t FindUInt64(const char *name, uint64 *value) const; 187 status_t FindUInt64(const char *name, int32 index, uint64 *value) const; 188 status_t FindBool(const char *name, bool *value) const; 189 status_t FindBool(const char *name, int32 index, bool *value) const; 190 status_t FindFloat(const char *name, float *value) const; 191 status_t FindFloat(const char *name, int32 index, float *value) const; 192 status_t FindDouble(const char *name, double *value) const; 193 status_t FindDouble(const char *name, int32 index, double *value) const; 194 status_t FindPointer(const char *name, void **pointer) const; 195 status_t FindPointer(const char *name, int32 index, void **pointer) const; 196 status_t FindMessenger(const char *name, BMessenger *messenger) const; 197 status_t FindMessenger(const char *name, int32 index, BMessenger *messenger) const; 198 status_t FindRef(const char *name, entry_ref *ref) const; 199 status_t FindRef(const char *name, int32 index, entry_ref *ref) const; 200 status_t FindMessage(const char *name, BMessage *message) const; 201 status_t FindMessage(const char *name, int32 index, BMessage *message) const; 202 status_t FindFlat(const char *name, BFlattenable *object) const; 203 status_t FindFlat(const char *name, int32 index, BFlattenable *object) const; 204 status_t FindData(const char *name, type_code type, 205 const void **data, ssize_t *numBytes) const; 206 status_t FindData(const char *name, type_code type, int32 index, 207 const void **data, ssize_t *numBytes) const; 208 209 // Replacing data 210 status_t ReplaceAlignment(const char* name, 211 const BAlignment& alignment); 212 status_t ReplaceAlignment(const char* name, int32 index, 213 const BAlignment& alignment); 214 215 status_t ReplaceRect(const char *name, BRect aRect); 216 status_t ReplaceRect(const char *name, int32 index, BRect aRect); 217 218 status_t ReplacePoint(const char *name, BPoint aPoint); 219 status_t ReplacePoint(const char *name, int32 index, BPoint aPoint); 220 status_t ReplaceSize(const char* name, BSize aSize); 221 status_t ReplaceSize(const char* name, int32 index, BSize aSize); 222 223 status_t ReplaceString(const char *name, const char *aString); 224 status_t ReplaceString(const char *name, int32 index, const char *aString); 225 status_t ReplaceString(const char *name, const BString &aString); 226 status_t ReplaceString(const char *name, int32 index, const BString &aString); 227 status_t ReplaceInt8(const char *name, int8 value); 228 status_t ReplaceInt8(const char *name, int32 index, int8 value); 229 status_t ReplaceUInt8(const char *name, uint8 value); 230 status_t ReplaceUInt8(const char *name, int32 index, uint8 value); 231 status_t ReplaceInt16(const char *name, int16 value); 232 status_t ReplaceInt16(const char *name, int32 index, int16 value); 233 status_t ReplaceUInt16(const char *name, uint16 value); 234 status_t ReplaceUInt16(const char *name, int32 index, uint16 value); 235 status_t ReplaceInt32(const char *name, int32 value); 236 status_t ReplaceInt32(const char *name, int32 index, int32 value); 237 status_t ReplaceUInt32(const char *name, uint32 value); 238 status_t ReplaceUInt32(const char *name, int32 index, uint32 value); 239 status_t ReplaceInt64(const char *name, int64 value); 240 status_t ReplaceInt64(const char *name, int32 index, int64 value); 241 status_t ReplaceUInt64(const char *name, uint64 value); 242 status_t ReplaceUInt64(const char *name, int32 index, uint64 value); 243 status_t ReplaceBool(const char *name, bool aBoolean); 244 status_t ReplaceBool(const char *name, int32 index, bool aBoolean); 245 status_t ReplaceFloat(const char *name, float aFloat); 246 status_t ReplaceFloat(const char *name, int32 index, float aFloat); 247 status_t ReplaceDouble(const char *name, double aDouble); 248 status_t ReplaceDouble(const char *name, int32 index, double aDouble); 249 status_t ReplacePointer(const char *name, const void *pointer); 250 status_t ReplacePointer(const char *name,int32 index,const void *pointer); 251 status_t ReplaceMessenger(const char *name, BMessenger messenger); 252 status_t ReplaceMessenger(const char *name, int32 index, BMessenger messenger); 253 status_t ReplaceRef( const char *name,const entry_ref *ref); 254 status_t ReplaceRef( const char *name, int32 index, const entry_ref *ref); 255 status_t ReplaceMessage(const char *name, const BMessage *message); 256 status_t ReplaceMessage(const char *name, int32 index, const BMessage *message); 257 status_t ReplaceFlat(const char *name, BFlattenable *object); 258 status_t ReplaceFlat(const char *name, int32 index, BFlattenable *object); 259 status_t ReplaceData(const char *name, type_code type, 260 const void *data, ssize_t numBytes); 261 status_t ReplaceData(const char *name, type_code type, int32 index, 262 const void *data, ssize_t numBytes); 263 264 // Comparing data - Haiku experimental API 265 bool HasSameData(const BMessage &other, 266 bool ignoreFieldOrder = true, bool deep = false) const; 267 268 void *operator new(size_t size); 269 void *operator new(size_t, void *pointer); 270 void operator delete(void *pointer, size_t size); 271 272 // Private, reserved, or obsolete 273 bool HasAlignment(const char*, int32 n = 0) const; 274 bool HasRect(const char *, int32 n = 0) const; 275 bool HasPoint(const char *, int32 n = 0) const; 276 bool HasSize(const char*, int32 n = 0) const; 277 bool HasString(const char *, int32 n = 0) const; 278 bool HasInt8(const char *, int32 n = 0) const; 279 bool HasUInt8(const char *, int32 n = 0) const; 280 bool HasInt16(const char *, int32 n = 0) const; 281 bool HasUInt16(const char *, int32 n = 0) const; 282 bool HasInt32(const char *, int32 n = 0) const; 283 bool HasUInt32(const char *, int32 n = 0) const; 284 bool HasInt64(const char *, int32 n = 0) const; 285 bool HasUInt64(const char *, int32 n = 0) const; 286 bool HasBool(const char *, int32 n = 0) const; 287 bool HasFloat(const char *, int32 n = 0) const; 288 bool HasDouble(const char *, int32 n = 0) const; 289 bool HasPointer(const char *, int32 n = 0) const; 290 bool HasMessenger(const char *, int32 n = 0) const; 291 bool HasRef(const char *, int32 n = 0) const; 292 bool HasMessage(const char *, int32 n = 0) const; 293 bool HasFlat(const char *, const BFlattenable *) const; 294 bool HasFlat(const char *, int32 n, const BFlattenable *) const; 295 bool HasData(const char *, type_code , int32 n = 0) const; 296 BRect FindRect(const char *, int32 n = 0) const; 297 BPoint FindPoint(const char *, int32 n = 0) const; 298 const char *FindString(const char *, int32 n = 0) const; 299 int8 FindInt8(const char *, int32 n = 0) const; 300 int16 FindInt16(const char *, int32 n = 0) const; 301 int32 FindInt32(const char *, int32 n = 0) const; 302 int64 FindInt64(const char *, int32 n = 0) const; 303 bool FindBool(const char *, int32 n = 0) const; 304 float FindFloat(const char *, int32 n = 0) const; 305 double FindDouble(const char *, int32 n = 0) const; 306 307 class Private; 308 struct message_header; 309 struct field_header; 310 311 private: 312 friend class Private; 313 friend class BMessageQueue; 314 315 status_t _InitCommon(bool initHeader); 316 status_t _InitHeader(); 317 status_t _Clear(); 318 319 status_t _FlattenToArea(message_header **_header) const; 320 status_t _CopyForWrite(); 321 status_t _Reference(); 322 status_t _Dereference(); 323 324 status_t _ValidateMessage(); 325 326 status_t _ResizeData(uint32 offset, int32 change); 327 328 uint32 _HashName(const char* name) const; 329 status_t _FindField(const char* name, type_code type, 330 field_header** _result) const; 331 status_t _AddField(const char* name, type_code type, 332 bool isFixedSize, field_header** _result); 333 status_t _RemoveField(field_header* field); 334 335 void _PrintToStream(const char* indent) const; 336 337 private: 338 message_header* fHeader; 339 field_header* fFields; 340 uint8* fData; 341 342 uint32 fFieldsAvailable; 343 size_t fDataAvailable; 344 345 mutable BMessage* fOriginal; 346 347 BMessage* fQueueLink; 348 // fQueueLink is used by BMessageQueue to build a linked list 349 350 void* fArchivingPointer; 351 352 uint32 fReserved[8]; 353 354 // deprecated 355 BMessage(BMessage *message); 356 357 virtual void _ReservedMessage1(); 358 virtual void _ReservedMessage2(); 359 virtual void _ReservedMessage3(); 360 361 status_t _SendMessage(port_id port, team_id portOwner, int32 token, 362 bigtime_t timeout, bool replyRequired, 363 BMessenger &replyTo) const; 364 status_t _SendMessage(port_id port, team_id portOwner, 365 int32 token, BMessage *reply, bigtime_t sendTimeout, 366 bigtime_t replyTimeout) const; 367 static status_t _SendFlattenedMessage(void *data, int32 size, 368 port_id port, int32 token, bigtime_t timeout); 369 370 static void _StaticInit(); 371 static void _StaticReInitForkedChild(); 372 static void _StaticCleanup(); 373 static void _StaticCacheCleanup(); 374 static int32 _StaticGetCachedReplyPort(); 375 376 enum { sNumReplyPorts = 3 }; 377 static port_id sReplyPorts[sNumReplyPorts]; 378 static long sReplyPortInUse[sNumReplyPorts]; 379 static int32 sGetCachedReplyPort(); 380 381 static BBlockCache* sMsgCache; 382 }; 383 384 #endif // _MESSAGE_H 385