1 /* 2 * Copyright 2005-2014 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 AddFlat(const char* name, 167 const BFlattenable* object, int32 count = 1); 168 status_t AddData(const char* name, type_code type, 169 const void* data, ssize_t numBytes, 170 bool isFixedSize = true, int32 count = 1); 171 172 status_t Append(const BMessage& message); 173 174 // Removing data 175 status_t RemoveData(const char* name, int32 index = 0); 176 status_t RemoveName(const char* name); 177 status_t MakeEmpty(); 178 179 // Finding data 180 status_t FindAlignment(const char* name, 181 BAlignment* alignment) const; 182 status_t FindAlignment(const char* name, int32 index, 183 BAlignment* alignment) const; 184 185 status_t FindRect(const char* name, BRect* rect) const; 186 status_t FindRect(const char* name, int32 index, 187 BRect* rect) const; 188 status_t FindPoint(const char* name, 189 BPoint* point) const; 190 status_t FindPoint(const char* name, int32 index, 191 BPoint* point) const; 192 193 status_t FindSize(const char* name, BSize* size) const; 194 status_t FindSize(const char* name, int32 index, 195 BSize* size) const; 196 197 status_t FindString(const char* name, 198 const char** string) const; 199 status_t FindString(const char* name, int32 index, 200 const char** string) const; 201 status_t FindString(const char* name, 202 BString* string) const; 203 status_t FindString(const char* name, int32 index, 204 BString* string) const; 205 status_t FindStrings(const char* name, 206 BStringList* list) const; 207 status_t FindInt8(const char* name, int8* value) const; 208 status_t FindInt8(const char* name, int32 index, 209 int8* value) const; 210 status_t FindUInt8(const char* name, uint8* value) const; 211 status_t FindUInt8(const char* name, int32 index, 212 uint8* value) const; 213 status_t FindInt16(const char* name, int16* value) const; 214 status_t FindInt16(const char* name, int32 index, 215 int16* value) const; 216 status_t FindUInt16(const char* name, 217 uint16* value) const; 218 status_t FindUInt16(const char* name, int32 index, 219 uint16* value) const; 220 status_t FindInt32(const char* name, int32* value) const; 221 status_t FindInt32(const char* name, int32 index, 222 int32* value) const; 223 status_t FindUInt32(const char* name, 224 uint32* value) const; 225 status_t FindUInt32(const char* name, int32 index, 226 uint32* value) const; 227 status_t FindInt64(const char* name, int64* value) const; 228 status_t FindInt64(const char* name, int32 index, 229 int64* value) const; 230 status_t FindUInt64(const char* name, 231 uint64* value) const; 232 status_t FindUInt64(const char* name, int32 index, 233 uint64* value) const; 234 status_t FindBool(const char* name, bool* value) const; 235 status_t FindBool(const char* name, int32 index, 236 bool* value) const; 237 status_t FindFloat(const char* name, float* value) const; 238 status_t FindFloat(const char* name, int32 index, 239 float* value) const; 240 status_t FindDouble(const char* name, 241 double* value) const; 242 status_t FindDouble(const char* name, int32 index, 243 double* value) const; 244 status_t FindPointer(const char* name, 245 void** pointer) const; 246 status_t FindPointer(const char* name, int32 index, 247 void** pointer) const; 248 status_t FindMessenger(const char* name, 249 BMessenger* messenger) const; 250 status_t FindMessenger(const char* name, int32 index, 251 BMessenger* messenger) const; 252 status_t FindRef(const char* name, entry_ref* ref) const; 253 status_t FindRef(const char* name, int32 index, 254 entry_ref* ref) const; 255 status_t FindMessage(const char* name, 256 BMessage* message) const; 257 status_t FindMessage(const char* name, int32 index, 258 BMessage* message) const; 259 status_t FindFlat(const char* name, 260 BFlattenable* object) const; 261 status_t FindFlat(const char* name, int32 index, 262 BFlattenable* object) const; 263 status_t FindData(const char* name, type_code type, 264 const void** data, ssize_t* numBytes) const; 265 status_t FindData(const char* name, type_code type, 266 int32 index, const void** data, 267 ssize_t* numBytes) const; 268 269 // Replacing data 270 status_t ReplaceAlignment(const char* name, 271 const BAlignment& alignment); 272 status_t ReplaceAlignment(const char* name, int32 index, 273 const BAlignment& alignment); 274 275 status_t ReplaceRect(const char* name, BRect rect); 276 status_t ReplaceRect(const char* name, int32 index, 277 BRect rect); 278 279 status_t ReplacePoint(const char* name, BPoint aPoint); 280 status_t ReplacePoint(const char* name, int32 index, 281 BPoint aPoint); 282 status_t ReplaceSize(const char* name, BSize aSize); 283 status_t ReplaceSize(const char* name, int32 index, 284 BSize aSize); 285 286 status_t ReplaceString(const char* name, 287 const char* string); 288 status_t ReplaceString(const char* name, int32 index, 289 const char* string); 290 status_t ReplaceString(const char* name, 291 const BString& string); 292 status_t ReplaceString(const char* name, int32 index, 293 const BString& string); 294 status_t ReplaceInt8(const char* name, int8 value); 295 status_t ReplaceInt8(const char* name, int32 index, 296 int8 value); 297 status_t ReplaceUInt8(const char* name, uint8 value); 298 status_t ReplaceUInt8(const char* name, int32 index, 299 uint8 value); 300 status_t ReplaceInt16(const char* name, int16 value); 301 status_t ReplaceInt16(const char* name, int32 index, 302 int16 value); 303 status_t ReplaceUInt16(const char* name, uint16 value); 304 status_t ReplaceUInt16(const char* name, int32 index, 305 uint16 value); 306 status_t ReplaceInt32(const char* name, int32 value); 307 status_t ReplaceInt32(const char* name, int32 index, 308 int32 value); 309 status_t ReplaceUInt32(const char* name, uint32 value); 310 status_t ReplaceUInt32(const char* name, int32 index, 311 uint32 value); 312 status_t ReplaceInt64(const char* name, int64 value); 313 status_t ReplaceInt64(const char* name, int32 index, 314 int64 value); 315 status_t ReplaceUInt64(const char* name, uint64 value); 316 status_t ReplaceUInt64(const char* name, int32 index, 317 uint64 value); 318 status_t ReplaceBool(const char* name, bool aBoolean); 319 status_t ReplaceBool(const char* name, int32 index, 320 bool value); 321 status_t ReplaceFloat(const char* name, float value); 322 status_t ReplaceFloat(const char* name, int32 index, 323 float value); 324 status_t ReplaceDouble(const char* name, double value); 325 status_t ReplaceDouble(const char* name, int32 index, 326 double value); 327 status_t ReplacePointer(const char* name, 328 const void* pointer); 329 status_t ReplacePointer(const char* name, int32 index, 330 const void* pointer); 331 status_t ReplaceMessenger(const char* name, 332 BMessenger messenger); 333 status_t ReplaceMessenger(const char* name, int32 index, 334 BMessenger messenger); 335 status_t ReplaceRef(const char* name, 336 const entry_ref* ref); 337 status_t ReplaceRef(const char* name, int32 index, 338 const entry_ref* ref); 339 status_t ReplaceMessage(const char* name, 340 const BMessage* message); 341 status_t ReplaceMessage(const char* name, int32 index, 342 const BMessage* message); 343 status_t ReplaceFlat(const char* name, 344 BFlattenable* object); 345 status_t ReplaceFlat(const char* name, int32 index, 346 BFlattenable* object); 347 status_t ReplaceData(const char* name, type_code type, 348 const void* data, ssize_t numBytes); 349 status_t ReplaceData(const char* name, type_code type, 350 int32 index, const void* data, 351 ssize_t numBytes); 352 353 // Comparing data - Haiku experimental API 354 bool HasSameData(const BMessage& other, 355 bool ignoreFieldOrder = true, 356 bool deep = false) const; 357 358 void* operator new(size_t size); 359 void* operator new(size_t, void* pointer); 360 void* operator new(size_t, 361 const std::nothrow_t& noThrow); 362 void operator delete(void* pointer, size_t size); 363 364 // Private, reserved, or obsolete 365 bool HasAlignment(const char* name, 366 int32 n = 0) const; 367 bool HasRect(const char* name, int32 n = 0) const; 368 bool HasPoint(const char* name, int32 n = 0) const; 369 bool HasSize(const char* name, int32 n = 0) const; 370 bool HasString(const char* name, int32 n = 0) const; 371 bool HasInt8(const char* name, int32 n = 0) const; 372 bool HasUInt8(const char* name, int32 n = 0) const; 373 bool HasInt16(const char* name, int32 n = 0) const; 374 bool HasUInt16(const char* name, int32 n = 0) const; 375 bool HasInt32(const char* name, int32 n = 0) const; 376 bool HasUInt32(const char* name, int32 n = 0) const; 377 bool HasInt64(const char* name, int32 n = 0) const; 378 bool HasUInt64(const char* name, int32 n = 0) const; 379 bool HasBool(const char* name, int32 n = 0) const; 380 bool HasFloat(const char* name, int32 n = 0) const; 381 bool HasDouble(const char* name, int32 n = 0) const; 382 bool HasPointer(const char* name, int32 n = 0) const; 383 bool HasMessenger(const char* name, 384 int32 n = 0) const; 385 bool HasRef(const char* name, int32 n = 0) const; 386 bool HasMessage(const char* name, int32 n = 0) const; 387 bool HasFlat(const char* name, 388 const BFlattenable* object) const; 389 bool HasFlat(const char* name, int32 n, 390 const BFlattenable* object) const; 391 bool HasData(const char* name, type_code , 392 int32 n = 0) const; 393 BRect FindRect(const char* name, int32 n = 0) const; 394 BPoint FindPoint(const char* name, int32 n = 0) const; 395 const char* FindString(const char* name, int32 n = 0) const; 396 int8 FindInt8(const char* name, int32 n = 0) const; 397 int16 FindInt16(const char* name, int32 n = 0) const; 398 int32 FindInt32(const char* name, int32 n = 0) const; 399 int64 FindInt64(const char* name, int32 n = 0) const; 400 bool FindBool(const char* name, int32 n = 0) const; 401 float FindFloat(const char* name, int32 n = 0) const; 402 double FindDouble(const char* name, int32 n = 0) const; 403 404 // Convenience methods 405 bool GetBool(const char* name, 406 bool defaultValue) const; 407 bool GetBool(const char* name, int32 index, 408 bool defaultValue) const; 409 int8 GetInt8(const char* name, 410 int8 defaultValue) const; 411 int8 GetInt8(const char* name, int32 index, 412 int8 defaultValue) const; 413 uint8 GetUInt8(const char* name, 414 uint8 defaultValue) const; 415 uint8 GetUInt8(const char* name, int32 index, 416 uint8 defaultValue) const; 417 int16 GetInt16(const char* name, 418 int16 defaultValue) const; 419 int16 GetInt16(const char* name, int32 index, 420 int16 defaultValue) const; 421 uint16 GetUInt16(const char* name, 422 uint16 defaultValue) const; 423 uint16 GetUInt16(const char* name, int32 index, 424 uint16 defaultValue) const; 425 int32 GetInt32(const char* name, 426 int32 defaultValue) const; 427 int32 GetInt32(const char* name, int32 index, 428 int32 defaultValue) const; 429 uint32 GetUInt32(const char* name, 430 uint32 defaultValue) const; 431 uint32 GetUInt32(const char* name, int32 index, 432 uint32 defaultValue) const; 433 int64 GetInt64(const char* name, 434 int64 defaultValue) const; 435 int64 GetInt64(const char* name, int32 index, 436 int64 defaultValue) const; 437 uint64 GetUInt64(const char* name, 438 uint64 defaultValue) const; 439 uint64 GetUInt64(const char* name, int32 index, 440 uint64 defaultValue) const; 441 float GetFloat(const char* name, 442 float defaultValue) const; 443 float GetFloat(const char* name, int32 index, 444 float defaultValue) const; 445 double GetDouble(const char* name, 446 double defaultValue) const; 447 double GetDouble(const char* name, int32 index, 448 double defaultValue) const; 449 const char* GetString(const char* name, 450 const char* defaultValue) const; 451 const char* GetString(const char* name, int32 index, 452 const char* defaultValue) const; 453 BAlignment GetAlignment(const char* name, int32 index, 454 const BAlignment& defaultValue) const; 455 BAlignment GetAlignment(const char* name, 456 const BAlignment& defaultValue) const; 457 BRect GetRect(const char* name, int32 index, 458 const BRect& defaultValue) const; 459 BRect GetRect(const char* name, 460 const BRect& defaultValue) const; 461 BPoint GetPoint(const char* name, int32 index, 462 const BPoint& defaultValue) const; 463 BPoint GetPoint(const char* name, 464 const BPoint& defaultValue) const; 465 BSize GetSize(const char* name, int32 index, 466 const BSize& defaultValue) const; 467 BSize GetSize(const char* name, 468 const BSize& defaultValue) const; 469 470 // fixed size fields only 471 status_t SetBool(const char* name, bool value); 472 status_t SetInt8(const char* name, int8 value); 473 status_t SetUInt8(const char* name, uint8 value); 474 status_t SetInt16(const char* name, int16 value); 475 status_t SetUInt16(const char* name, uint16 value); 476 status_t SetInt32(const char* name, int32 value); 477 status_t SetUInt32(const char* name, uint32 value); 478 status_t SetInt64(const char* name, int64 value); 479 status_t SetUInt64(const char* name, uint64 value); 480 status_t SetPointer(const char* name, const void* value); 481 status_t SetString(const char* name, const char* string); 482 status_t SetString(const char* name, 483 const BString& string); 484 status_t SetFloat(const char* name, float value); 485 status_t SetDouble(const char* name, double value); 486 status_t SetAlignment(const char* name, 487 const BAlignment& value); 488 status_t SetPoint(const char* name, const BPoint& value); 489 status_t SetRect(const char* name, const BRect& value); 490 status_t SetSize(const char* name, const BSize& value); 491 status_t SetData(const char* name, type_code type, 492 const void* data, ssize_t numBytes); 493 494 class Private; 495 struct message_header; 496 struct field_header; 497 498 private: 499 friend class Private; 500 friend class BMessageQueue; 501 502 status_t _InitCommon(bool initHeader); 503 status_t _InitHeader(); 504 status_t _Clear(); 505 506 status_t _FlattenToArea(message_header** _header) const; 507 status_t _CopyForWrite(); 508 status_t _Reference(); 509 status_t _Dereference(); 510 511 status_t _ValidateMessage(); 512 513 void _UpdateOffsets(uint32 offset, int32 change); 514 status_t _ResizeData(uint32 offset, int32 change); 515 516 uint32 _HashName(const char* name) const; 517 status_t _FindField(const char* name, type_code type, 518 field_header** _result) const; 519 status_t _AddField(const char* name, type_code type, 520 bool isFixedSize, field_header** _result); 521 status_t _RemoveField(field_header* field); 522 523 void _PrintToStream(const char* indent) const; 524 525 private: 526 BMessage(BMessage* message); 527 // deprecated 528 529 virtual void _ReservedMessage1(); 530 virtual void _ReservedMessage2(); 531 virtual void _ReservedMessage3(); 532 533 status_t _SendMessage(port_id port, team_id portOwner, 534 int32 token, bigtime_t timeout, 535 bool replyRequired, 536 BMessenger& replyTo) const; 537 status_t _SendMessage(port_id port, team_id portOwner, 538 int32 token, BMessage* reply, 539 bigtime_t sendTimeout, 540 bigtime_t replyTimeout) const; 541 static status_t _SendFlattenedMessage(void* data, int32 size, 542 port_id port, int32 token, 543 bigtime_t timeout); 544 545 static void _StaticInit(); 546 static void _StaticReInitForkedChild(); 547 static void _StaticCleanup(); 548 static void _StaticCacheCleanup(); 549 static int32 _StaticGetCachedReplyPort(); 550 551 private: 552 message_header* fHeader; 553 field_header* fFields; 554 uint8* fData; 555 556 uint32 fFieldsAvailable; 557 size_t fDataAvailable; 558 559 mutable BMessage* fOriginal; 560 561 BMessage* fQueueLink; 562 // fQueueLink is used by BMessageQueue to build a linked list 563 564 void* fArchivingPointer; 565 566 uint32 fReserved[8]; 567 568 enum { sNumReplyPorts = 3 }; 569 static port_id sReplyPorts[sNumReplyPorts]; 570 static int32 sReplyPortInUse[sNumReplyPorts]; 571 static int32 sGetCachedReplyPort(); 572 573 static BBlockCache* sMsgCache; 574 }; 575 576 577 #endif // _MESSAGE_H 578