1 /* 2 * Copyright 2005-2012, 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 <new> 13 14 #include <BeBuild.h> 15 #include <DataIO.h> 16 #include <Flattenable.h> 17 #include <OS.h> 18 #include <Rect.h> 19 #include <Size.h> 20 21 #include <AppDefs.h> /* For convenience */ 22 #include <TypeConstants.h> /* For convenience */ 23 24 25 class BAlignment; 26 class BBlockCache; 27 class BMessenger; 28 class BHandler; 29 class BString; 30 class BStringList; 31 struct entry_ref; 32 33 34 // Name lengths and Scripting specifiers 35 #define B_FIELD_NAME_LENGTH 255 36 #define B_PROPERTY_NAME_LENGTH 255 37 38 enum { 39 B_NO_SPECIFIER = 0, 40 B_DIRECT_SPECIFIER = 1, 41 B_INDEX_SPECIFIER, 42 B_REVERSE_INDEX_SPECIFIER, 43 B_RANGE_SPECIFIER, 44 B_REVERSE_RANGE_SPECIFIER, 45 B_NAME_SPECIFIER, 46 B_ID_SPECIFIER, 47 48 B_SPECIFIERS_END = 128 49 // app-defined specifiers start at B_SPECIFIERS_END + 1 50 }; 51 52 53 class BMessage { 54 public: 55 uint32 what; 56 57 BMessage(); 58 BMessage(uint32 what); 59 BMessage(const BMessage& other); 60 virtual ~BMessage(); 61 62 BMessage& operator=(const BMessage& other); 63 64 // Statistics and misc info 65 status_t GetInfo(type_code typeRequested, int32 index, 66 char** nameFound, type_code* typeFound, 67 int32* countFound = NULL) const; 68 status_t GetInfo(const char* name, type_code* typeFound, 69 int32* countFound = NULL) const; 70 status_t GetInfo(const char* name, type_code* typeFound, 71 bool* fixedSize) const; 72 status_t GetInfo(const char* name, type_code* typeFound, 73 int32* countFound, bool* fixedSize) const; 74 75 int32 CountNames(type_code type) const; 76 bool IsEmpty() const; 77 bool IsSystem() const; 78 bool IsReply() const; 79 void PrintToStream() const; 80 81 status_t Rename(const char* oldEntry, 82 const char* newEntry); 83 84 // Delivery info 85 bool WasDelivered() const; 86 bool IsSourceWaiting() const; 87 bool IsSourceRemote() const; 88 BMessenger ReturnAddress() const; 89 const BMessage* Previous() const; 90 bool WasDropped() const; 91 BPoint DropPoint(BPoint* offset = NULL) const; 92 93 // Replying 94 status_t SendReply(uint32 command, 95 BHandler* replyTo = NULL); 96 status_t SendReply(BMessage* reply, 97 BHandler* replyTo = NULL, 98 bigtime_t timeout = B_INFINITE_TIMEOUT); 99 status_t SendReply(BMessage* reply, BMessenger replyTo, 100 bigtime_t timeout = B_INFINITE_TIMEOUT); 101 102 status_t SendReply(uint32 command, 103 BMessage* replyToReply); 104 status_t SendReply(BMessage* reply, 105 BMessage* replyToReply, 106 bigtime_t sendTimeout = B_INFINITE_TIMEOUT, 107 bigtime_t replyTimeout 108 = B_INFINITE_TIMEOUT); 109 110 // Flattening data 111 ssize_t FlattenedSize() const; 112 status_t Flatten(char* buffer, ssize_t size) const; 113 status_t Flatten(BDataIO* stream, 114 ssize_t* size = NULL) const; 115 status_t Unflatten(const char* flatBuffer); 116 status_t Unflatten(BDataIO* stream); 117 118 // Specifiers (scripting) 119 status_t AddSpecifier(const char* property); 120 status_t AddSpecifier(const char* property, int32 index); 121 status_t AddSpecifier(const char* property, int32 index, 122 int32 range); 123 status_t AddSpecifier(const char* property, 124 const char* name); 125 status_t AddSpecifier(const BMessage* specifier); 126 127 status_t SetCurrentSpecifier(int32 index); 128 status_t GetCurrentSpecifier(int32* index, 129 BMessage* specifier = NULL, 130 int32* what = NULL, 131 const char** property = NULL) const; 132 bool HasSpecifiers() const; 133 status_t PopSpecifier(); 134 135 // Adding data 136 status_t AddAlignment(const char* name, 137 const BAlignment& alignment); 138 status_t AddRect(const char* name, BRect rect); 139 status_t AddPoint(const char* name, BPoint point); 140 status_t AddSize(const char* name, BSize size); 141 status_t AddString(const char* name, const char* string); 142 status_t AddString(const char* name, 143 const BString& string); 144 status_t AddStrings(const char *name, 145 const BStringList& list); 146 status_t AddInt8(const char* name, int8 value); 147 status_t AddUInt8(const char* name, uint8 value); 148 status_t AddInt16(const char* name, int16 value); 149 status_t AddUInt16(const char* name, uint16 value); 150 status_t AddInt32(const char* name, int32 value); 151 status_t AddUInt32(const char* name, uint32 value); 152 status_t AddInt64(const char* name, int64 value); 153 status_t AddUInt64(const char* name, uint64 value); 154 status_t AddBool(const char* name, bool value); 155 status_t AddFloat(const char* name, float value); 156 status_t AddDouble(const char* name, double value); 157 status_t AddPointer(const char* name, 158 const void* pointer); 159 status_t AddMessenger(const char* name, 160 BMessenger messenger); 161 status_t AddRef(const char* name, const entry_ref* ref); 162 status_t AddMessage(const char* name, 163 const BMessage* message); 164 status_t AddFlat(const char* name, BFlattenable* object, 165 int32 count = 1); 166 status_t AddData(const char* name, type_code type, 167 const void* data, ssize_t numBytes, 168 bool isFixedSize = true, int32 count = 1); 169 170 status_t Append(const BMessage& message); 171 172 // Removing data 173 status_t RemoveData(const char* name, int32 index = 0); 174 status_t RemoveName(const char* name); 175 status_t MakeEmpty(); 176 177 // Finding data 178 status_t FindAlignment(const char* name, 179 BAlignment* alignment) const; 180 status_t FindAlignment(const char* name, int32 index, 181 BAlignment* alignment) const; 182 183 status_t FindRect(const char* name, BRect* rect) const; 184 status_t FindRect(const char* name, int32 index, 185 BRect* rect) const; 186 status_t FindPoint(const char* name, 187 BPoint* point) const; 188 status_t FindPoint(const char* name, int32 index, 189 BPoint* point) const; 190 191 status_t FindSize(const char* name, BSize* size) const; 192 status_t FindSize(const char* name, int32 index, 193 BSize* size) const; 194 195 status_t FindString(const char* name, 196 const char** string) const; 197 status_t FindString(const char* name, int32 index, 198 const char** string) const; 199 status_t FindString(const char* name, 200 BString* string) const; 201 status_t FindString(const char* name, int32 index, 202 BString* string) const; 203 status_t FindStrings(const char* name, 204 BStringList* list) const; 205 status_t FindInt8(const char* name, int8* value) const; 206 status_t FindInt8(const char* name, int32 index, 207 int8* value) const; 208 status_t FindUInt8(const char* name, uint8* value) const; 209 status_t FindUInt8(const char* name, int32 index, 210 uint8* value) const; 211 status_t FindInt16(const char* name, int16* value) const; 212 status_t FindInt16(const char* name, int32 index, 213 int16* value) const; 214 status_t FindUInt16(const char* name, 215 uint16* value) const; 216 status_t FindUInt16(const char* name, int32 index, 217 uint16* value) const; 218 status_t FindInt32(const char* name, int32* value) const; 219 status_t FindInt32(const char* name, int32 index, 220 int32* value) const; 221 status_t FindUInt32(const char* name, 222 uint32* value) const; 223 status_t FindUInt32(const char* name, int32 index, 224 uint32* value) const; 225 status_t FindInt64(const char* name, int64* value) const; 226 status_t FindInt64(const char* name, int32 index, 227 int64* value) const; 228 status_t FindUInt64(const char* name, 229 uint64* value) const; 230 status_t FindUInt64(const char* name, int32 index, 231 uint64* value) const; 232 status_t FindBool(const char* name, bool* value) const; 233 status_t FindBool(const char* name, int32 index, 234 bool* value) const; 235 status_t FindFloat(const char* name, float* value) const; 236 status_t FindFloat(const char* name, int32 index, 237 float* value) const; 238 status_t FindDouble(const char* name, 239 double* value) const; 240 status_t FindDouble(const char* name, int32 index, 241 double* value) const; 242 status_t FindPointer(const char* name, 243 void** pointer) const; 244 status_t FindPointer(const char* name, int32 index, 245 void** pointer) const; 246 status_t FindMessenger(const char* name, 247 BMessenger* messenger) const; 248 status_t FindMessenger(const char* name, int32 index, 249 BMessenger* messenger) const; 250 status_t FindRef(const char* name, entry_ref* ref) const; 251 status_t FindRef(const char* name, int32 index, 252 entry_ref* ref) const; 253 status_t FindMessage(const char* name, 254 BMessage* message) const; 255 status_t FindMessage(const char* name, int32 index, 256 BMessage* message) const; 257 status_t FindFlat(const char* name, 258 BFlattenable* object) const; 259 status_t FindFlat(const char* name, int32 index, 260 BFlattenable* object) const; 261 status_t FindData(const char* name, type_code type, 262 const void** data, ssize_t* numBytes) const; 263 status_t FindData(const char* name, type_code type, 264 int32 index, const void** data, 265 ssize_t* numBytes) const; 266 267 // Replacing data 268 status_t ReplaceAlignment(const char* name, 269 const BAlignment& alignment); 270 status_t ReplaceAlignment(const char* name, int32 index, 271 const BAlignment& alignment); 272 273 status_t ReplaceRect(const char* name, BRect rect); 274 status_t ReplaceRect(const char* name, int32 index, 275 BRect rect); 276 277 status_t ReplacePoint(const char* name, BPoint aPoint); 278 status_t ReplacePoint(const char* name, int32 index, 279 BPoint aPoint); 280 status_t ReplaceSize(const char* name, BSize aSize); 281 status_t ReplaceSize(const char* name, int32 index, 282 BSize aSize); 283 284 status_t ReplaceString(const char* name, 285 const char* string); 286 status_t ReplaceString(const char* name, int32 index, 287 const char* string); 288 status_t ReplaceString(const char* name, 289 const BString& string); 290 status_t ReplaceString(const char* name, int32 index, 291 const BString& string); 292 status_t ReplaceInt8(const char* name, int8 value); 293 status_t ReplaceInt8(const char* name, int32 index, 294 int8 value); 295 status_t ReplaceUInt8(const char* name, uint8 value); 296 status_t ReplaceUInt8(const char* name, int32 index, 297 uint8 value); 298 status_t ReplaceInt16(const char* name, int16 value); 299 status_t ReplaceInt16(const char* name, int32 index, 300 int16 value); 301 status_t ReplaceUInt16(const char* name, uint16 value); 302 status_t ReplaceUInt16(const char* name, int32 index, 303 uint16 value); 304 status_t ReplaceInt32(const char* name, int32 value); 305 status_t ReplaceInt32(const char* name, int32 index, 306 int32 value); 307 status_t ReplaceUInt32(const char* name, uint32 value); 308 status_t ReplaceUInt32(const char* name, int32 index, 309 uint32 value); 310 status_t ReplaceInt64(const char* name, int64 value); 311 status_t ReplaceInt64(const char* name, int32 index, 312 int64 value); 313 status_t ReplaceUInt64(const char* name, uint64 value); 314 status_t ReplaceUInt64(const char* name, int32 index, 315 uint64 value); 316 status_t ReplaceBool(const char* name, bool aBoolean); 317 status_t ReplaceBool(const char* name, int32 index, 318 bool value); 319 status_t ReplaceFloat(const char* name, float value); 320 status_t ReplaceFloat(const char* name, int32 index, 321 float value); 322 status_t ReplaceDouble(const char* name, double value); 323 status_t ReplaceDouble(const char* name, int32 index, 324 double value); 325 status_t ReplacePointer(const char* name, 326 const void* pointer); 327 status_t ReplacePointer(const char* name, int32 index, 328 const void* pointer); 329 status_t ReplaceMessenger(const char* name, 330 BMessenger messenger); 331 status_t ReplaceMessenger(const char* name, int32 index, 332 BMessenger messenger); 333 status_t ReplaceRef(const char* name, 334 const entry_ref* ref); 335 status_t ReplaceRef(const char* name, int32 index, 336 const entry_ref* ref); 337 status_t ReplaceMessage(const char* name, 338 const BMessage* message); 339 status_t ReplaceMessage(const char* name, int32 index, 340 const BMessage* message); 341 status_t ReplaceFlat(const char* name, 342 BFlattenable* object); 343 status_t ReplaceFlat(const char* name, int32 index, 344 BFlattenable* object); 345 status_t ReplaceData(const char* name, type_code type, 346 const void* data, ssize_t numBytes); 347 status_t ReplaceData(const char* name, type_code type, 348 int32 index, const void* data, 349 ssize_t numBytes); 350 351 // Comparing data - Haiku experimental API 352 bool HasSameData(const BMessage& other, 353 bool ignoreFieldOrder = true, 354 bool deep = false) const; 355 356 void* operator new(size_t size); 357 void* operator new(size_t, void* pointer); 358 void* operator new(size_t, 359 const std::nothrow_t& noThrow); 360 void operator delete(void* pointer, size_t size); 361 362 // Private, reserved, or obsolete 363 bool HasAlignment(const char* name, 364 int32 n = 0) const; 365 bool HasRect(const char* name, int32 n = 0) const; 366 bool HasPoint(const char* name, int32 n = 0) const; 367 bool HasSize(const char* name, int32 n = 0) const; 368 bool HasString(const char* name, int32 n = 0) const; 369 bool HasInt8(const char* name, int32 n = 0) const; 370 bool HasUInt8(const char* name, int32 n = 0) const; 371 bool HasInt16(const char* name, int32 n = 0) const; 372 bool HasUInt16(const char* name, int32 n = 0) const; 373 bool HasInt32(const char* name, int32 n = 0) const; 374 bool HasUInt32(const char* name, int32 n = 0) const; 375 bool HasInt64(const char* name, int32 n = 0) const; 376 bool HasUInt64(const char* name, int32 n = 0) const; 377 bool HasBool(const char* name, int32 n = 0) const; 378 bool HasFloat(const char* name, int32 n = 0) const; 379 bool HasDouble(const char* name, int32 n = 0) const; 380 bool HasPointer(const char* name, int32 n = 0) const; 381 bool HasMessenger(const char* name, 382 int32 n = 0) const; 383 bool HasRef(const char* name, int32 n = 0) const; 384 bool HasMessage(const char* name, int32 n = 0) const; 385 bool HasFlat(const char* name, 386 const BFlattenable* object) const; 387 bool HasFlat(const char* name, int32 n, 388 const BFlattenable* object) const; 389 bool HasData(const char* name, type_code , 390 int32 n = 0) const; 391 BRect FindRect(const char* name, int32 n = 0) const; 392 BPoint FindPoint(const char* name, int32 n = 0) const; 393 const char* FindString(const char* name, int32 n = 0) const; 394 int8 FindInt8(const char* name, int32 n = 0) const; 395 int16 FindInt16(const char* name, int32 n = 0) const; 396 int32 FindInt32(const char* name, int32 n = 0) const; 397 int64 FindInt64(const char* name, int32 n = 0) const; 398 bool FindBool(const char* name, int32 n = 0) const; 399 float FindFloat(const char* name, int32 n = 0) const; 400 double FindDouble(const char* name, int32 n = 0) const; 401 402 // Convenience methods 403 bool GetBool(const char* name, 404 bool defaultValue) const; 405 bool GetBool(const char* name, int32 index, 406 bool defaultValue) const; 407 int8 GetInt8(const char* name, 408 int8 defaultValue) const; 409 int8 GetInt8(const char* name, int32 index, 410 int8 defaultValue) const; 411 uint8 GetUInt8(const char* name, 412 uint8 defaultValue) const; 413 uint8 GetUInt8(const char* name, int32 index, 414 uint8 defaultValue) const; 415 int16 GetInt16(const char* name, 416 int16 defaultValue) const; 417 int16 GetInt16(const char* name, int32 index, 418 int16 defaultValue) const; 419 uint16 GetUInt16(const char* name, 420 uint16 defaultValue) const; 421 uint16 GetUInt16(const char* name, int32 index, 422 uint16 defaultValue) const; 423 int32 GetInt32(const char* name, 424 int32 defaultValue) const; 425 int32 GetInt32(const char* name, int32 index, 426 int32 defaultValue) const; 427 uint32 GetUInt32(const char* name, 428 uint32 defaultValue) const; 429 uint32 GetUInt32(const char* name, int32 index, 430 uint32 defaultValue) const; 431 int64 GetInt64(const char* name, 432 int64 defaultValue) const; 433 int64 GetInt64(const char* name, int32 index, 434 int64 defaultValue) const; 435 uint64 GetUInt64(const char* name, 436 uint64 defaultValue) const; 437 uint64 GetUInt64(const char* name, int32 index, 438 uint64 defaultValue) const; 439 float GetFloat(const char* name, 440 float defaultValue) const; 441 float GetFloat(const char* name, int32 index, 442 float defaultValue) const; 443 double GetDouble(const char* name, 444 double defaultValue) const; 445 double GetDouble(const char* name, int32 index, 446 double defaultValue) const; 447 void* GetPointer(const char* name, 448 const void* defaultValue) const; 449 void* GetPointer(const char* name, int32 index, 450 const void* defaultValue) const; 451 const char* GetString(const char* name, 452 const char* defaultValue) const; 453 const char* GetString(const char* name, int32 index, 454 const char* defaultValue) const; 455 BAlignment GetAlignment(const char* name, int32 index, 456 const BAlignment& defaultValue) const; 457 BAlignment GetAlignment(const char* name, 458 const BAlignment& defaultValue) const; 459 BRect GetRect(const char* name, int32 index, 460 const BRect& defaultValue) const; 461 BRect GetRect(const char* name, 462 const BRect& defaultValue) const; 463 BPoint GetPoint(const char* name, int32 index, 464 const BPoint& defaultValue) const; 465 BPoint GetPoint(const char* name, 466 const BPoint& defaultValue) const; 467 BSize GetSize(const char* name, int32 index, 468 const BSize& defaultValue) const; 469 BSize GetSize(const char* name, 470 const BSize& defaultValue) const; 471 472 // fixed size fields only 473 status_t SetBool(const char* name, bool value); 474 status_t SetInt8(const char* name, int8 value); 475 status_t SetUInt8(const char* name, uint8 value); 476 status_t SetInt16(const char* name, int16 value); 477 status_t SetUInt16(const char* name, uint16 value); 478 status_t SetInt32(const char* name, int32 value); 479 status_t SetUInt32(const char* name, uint32 value); 480 status_t SetInt64(const char* name, int64 value); 481 status_t SetUInt64(const char* name, uint64 value); 482 status_t SetPointer(const char* name, const void* value); 483 status_t SetString(const char* name, const char* string); 484 status_t SetString(const char* name, 485 const BString& string); 486 status_t SetFloat(const char* name, float value); 487 status_t SetDouble(const char* name, double value); 488 status_t SetAlignment(const char* name, 489 const BAlignment& value); 490 status_t SetPoint(const char* name, const BPoint& value); 491 status_t SetRect(const char* name, const BRect& value); 492 status_t SetSize(const char* name, const BSize& value); 493 status_t SetData(const char* name, type_code type, 494 const void* data, ssize_t numBytes); 495 496 class Private; 497 struct message_header; 498 struct field_header; 499 500 private: 501 friend class Private; 502 friend class BMessageQueue; 503 504 status_t _InitCommon(bool initHeader); 505 status_t _InitHeader(); 506 status_t _Clear(); 507 508 status_t _FlattenToArea(message_header** _header) const; 509 status_t _CopyForWrite(); 510 status_t _Reference(); 511 status_t _Dereference(); 512 513 status_t _ValidateMessage(); 514 515 status_t _ResizeData(uint32 offset, int32 change); 516 517 uint32 _HashName(const char* name) const; 518 status_t _FindField(const char* name, type_code type, 519 field_header** _result) const; 520 status_t _AddField(const char* name, type_code type, 521 bool isFixedSize, field_header** _result); 522 status_t _RemoveField(field_header* field); 523 524 void _PrintToStream(const char* indent) const; 525 526 private: 527 BMessage(BMessage* message); 528 // deprecated 529 530 virtual void _ReservedMessage1(); 531 virtual void _ReservedMessage2(); 532 virtual void _ReservedMessage3(); 533 534 status_t _SendMessage(port_id port, team_id portOwner, 535 int32 token, bigtime_t timeout, 536 bool replyRequired, 537 BMessenger& replyTo) const; 538 status_t _SendMessage(port_id port, team_id portOwner, 539 int32 token, BMessage* reply, 540 bigtime_t sendTimeout, 541 bigtime_t replyTimeout) const; 542 static status_t _SendFlattenedMessage(void* data, int32 size, 543 port_id port, int32 token, 544 bigtime_t timeout); 545 546 static void _StaticInit(); 547 static void _StaticReInitForkedChild(); 548 static void _StaticCleanup(); 549 static void _StaticCacheCleanup(); 550 static int32 _StaticGetCachedReplyPort(); 551 552 private: 553 message_header* fHeader; 554 field_header* fFields; 555 uint8* fData; 556 557 uint32 fFieldsAvailable; 558 size_t fDataAvailable; 559 560 mutable BMessage* fOriginal; 561 562 BMessage* fQueueLink; 563 // fQueueLink is used by BMessageQueue to build a linked list 564 565 void* fArchivingPointer; 566 567 uint32 fReserved[8]; 568 569 enum { sNumReplyPorts = 3 }; 570 static port_id sReplyPorts[sNumReplyPorts]; 571 static int32 sReplyPortInUse[sNumReplyPorts]; 572 static int32 sGetCachedReplyPort(); 573 574 static BBlockCache* sMsgCache; 575 }; 576 577 578 #endif // _MESSAGE_H 579