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