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