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