1 /* 2 * Copyright 2005-2007, 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 AddInt16(const char *name, int16 value); 123 status_t AddInt32(const char *name, int32 value); 124 status_t AddInt64(const char *name, int64 value); 125 status_t AddBool(const char *name, bool aBoolean); 126 status_t AddFloat(const char *name, float aFloat); 127 status_t AddDouble(const char *name, double aDouble); 128 status_t AddPointer(const char *name, const void *aPointer); 129 status_t AddMessenger(const char *name, BMessenger messenger); 130 status_t AddRef(const char *name, const entry_ref *ref); 131 status_t AddMessage(const char *name, const BMessage *message); 132 status_t AddFlat(const char *name, BFlattenable *object, 133 int32 count = 1); 134 status_t AddData(const char *name, type_code type, 135 const void *data, ssize_t numBytes, 136 bool isFixedSize = true, int32 count = 1); 137 138 // Removing data 139 status_t RemoveData(const char *name, int32 index = 0); 140 status_t RemoveName(const char *name); 141 status_t MakeEmpty(); 142 143 // Finding data 144 status_t FindRect(const char *name, BRect *rect) const; 145 status_t FindRect(const char *name, int32 index, BRect *rect) const; 146 status_t FindPoint(const char *name, BPoint *point) const; 147 status_t FindPoint(const char *name, int32 index, BPoint *point) const; 148 status_t FindString(const char *name, const char **string) const; 149 status_t FindString(const char *name, int32 index, const char **string) const; 150 status_t FindString(const char *name, BString *string) const; 151 status_t FindString(const char *name, int32 index, BString *string) const; 152 status_t FindInt8(const char *name, int8 *value) const; 153 status_t FindInt8(const char *name, int32 index, int8 *value) const; 154 status_t FindInt16(const char *name, int16 *value) const; 155 status_t FindInt16(const char *name, int32 index, int16 *value) const; 156 status_t FindInt32(const char *name, int32 *value) const; 157 status_t FindInt32(const char *name, int32 index, int32 *value) const; 158 status_t FindInt64(const char *name, int64 *value) const; 159 status_t FindInt64(const char *name, int32 index, int64 *value) const; 160 status_t FindBool(const char *name, bool *value) const; 161 status_t FindBool(const char *name, int32 index, bool *value) const; 162 status_t FindFloat(const char *name, float *value) const; 163 status_t FindFloat(const char *name, int32 index, float *value) const; 164 status_t FindDouble(const char *name, double *value) const; 165 status_t FindDouble(const char *name, int32 index, double *value) const; 166 status_t FindPointer(const char *name, void **pointer) const; 167 status_t FindPointer(const char *name, int32 index, void **pointer) const; 168 status_t FindMessenger(const char *name, BMessenger *messenger) const; 169 status_t FindMessenger(const char *name, int32 index, BMessenger *messenger) const; 170 status_t FindRef(const char *name, entry_ref *ref) const; 171 status_t FindRef(const char *name, int32 index, entry_ref *ref) const; 172 status_t FindMessage(const char *name, BMessage *message) const; 173 status_t FindMessage(const char *name, int32 index, BMessage *message) const; 174 status_t FindFlat(const char *name, BFlattenable *object) const; 175 status_t FindFlat(const char *name, int32 index, BFlattenable *object) const; 176 status_t FindData(const char *name, type_code type, 177 const void **data, ssize_t *numBytes) const; 178 status_t FindData(const char *name, type_code type, int32 index, 179 const void **data, ssize_t *numBytes) const; 180 181 // Replacing data 182 status_t ReplaceRect(const char *name, BRect aRect); 183 status_t ReplaceRect(const char *name, int32 index, BRect aRect); 184 status_t ReplacePoint(const char *name, BPoint aPoint); 185 status_t ReplacePoint(const char *name, int32 index, BPoint aPoint); 186 status_t ReplaceString(const char *name, const char *aString); 187 status_t ReplaceString(const char *name, int32 index, const char *aString); 188 status_t ReplaceString(const char *name, const BString &aString); 189 status_t ReplaceString(const char *name, int32 index, const BString &aString); 190 status_t ReplaceInt8(const char *name, int8 value); 191 status_t ReplaceInt8(const char *name, int32 index, int8 value); 192 status_t ReplaceInt16(const char *name, int16 value); 193 status_t ReplaceInt16(const char *name, int32 index, int16 value); 194 status_t ReplaceInt32(const char *name, int32 value); 195 status_t ReplaceInt32(const char *name, int32 index, int32 value); 196 status_t ReplaceInt64(const char *name, int64 value); 197 status_t ReplaceInt64(const char *name, int32 index, int64 value); 198 status_t ReplaceBool(const char *name, bool aBoolean); 199 status_t ReplaceBool(const char *name, int32 index, bool aBoolean); 200 status_t ReplaceFloat(const char *name, float aFloat); 201 status_t ReplaceFloat(const char *name, int32 index, float aFloat); 202 status_t ReplaceDouble(const char *name, double aDouble); 203 status_t ReplaceDouble(const char *name, int32 index, double aDouble); 204 status_t ReplacePointer(const char *name, const void *pointer); 205 status_t ReplacePointer(const char *name,int32 index,const void *pointer); 206 status_t ReplaceMessenger(const char *name, BMessenger messenger); 207 status_t ReplaceMessenger(const char *name, int32 index, BMessenger messenger); 208 status_t ReplaceRef( const char *name,const entry_ref *ref); 209 status_t ReplaceRef( const char *name, int32 index, const entry_ref *ref); 210 status_t ReplaceMessage(const char *name, const BMessage *message); 211 status_t ReplaceMessage(const char *name, int32 index, const BMessage *message); 212 status_t ReplaceFlat(const char *name, BFlattenable *object); 213 status_t ReplaceFlat(const char *name, int32 index, BFlattenable *object); 214 status_t ReplaceData(const char *name, type_code type, 215 const void *data, ssize_t numBytes); 216 status_t ReplaceData(const char *name, type_code type, int32 index, 217 const void *data, ssize_t numBytes); 218 219 // Comparing data - Haiku experimental API 220 bool HasSameData(const BMessage &other, 221 bool ignoreFieldOrder = true, bool deep = false) const; 222 223 void *operator new(size_t size); 224 void *operator new(size_t, void *pointer); 225 void operator delete(void *pointer, size_t size); 226 227 // Private, reserved, or obsolete 228 bool HasRect(const char *, int32 n = 0) const; 229 bool HasPoint(const char *, int32 n = 0) const; 230 bool HasString(const char *, int32 n = 0) const; 231 bool HasInt8(const char *, int32 n = 0) const; 232 bool HasInt16(const char *, int32 n = 0) const; 233 bool HasInt32(const char *, int32 n = 0) const; 234 bool HasInt64(const char *, int32 n = 0) const; 235 bool HasBool(const char *, int32 n = 0) const; 236 bool HasFloat(const char *, int32 n = 0) const; 237 bool HasDouble(const char *, int32 n = 0) const; 238 bool HasPointer(const char *, int32 n = 0) const; 239 bool HasMessenger(const char *, int32 n = 0) const; 240 bool HasRef(const char *, int32 n = 0) const; 241 bool HasMessage(const char *, int32 n = 0) const; 242 bool HasFlat(const char *, const BFlattenable *) const; 243 bool HasFlat(const char *, int32 n, const BFlattenable *) const; 244 bool HasData(const char *, type_code , int32 n = 0) const; 245 BRect FindRect(const char *, int32 n = 0) const; 246 BPoint FindPoint(const char *, int32 n = 0) const; 247 const char *FindString(const char *, int32 n = 0) const; 248 int8 FindInt8(const char *, int32 n = 0) const; 249 int16 FindInt16(const char *, int32 n = 0) const; 250 int32 FindInt32(const char *, int32 n = 0) const; 251 int64 FindInt64(const char *, int32 n = 0) const; 252 bool FindBool(const char *, int32 n = 0) const; 253 float FindFloat(const char *, int32 n = 0) const; 254 double FindDouble(const char *, int32 n = 0) const; 255 256 class Private; 257 struct message_header; 258 struct field_header; 259 260 private: 261 friend class Private; 262 friend class BMessageQueue; 263 264 status_t _InitCommon(bool initHeader); 265 status_t _InitHeader(); 266 status_t _Clear(); 267 268 status_t _FlattenToArea(message_header **_header) const; 269 status_t _CopyForWrite(); 270 status_t _Reference(message_header *header); 271 status_t _Dereference(); 272 273 status_t _ResizeData(int32 offset, int32 change); 274 275 uint32 _HashName(const char* name) const; 276 status_t _FindField(const char* name, type_code type, 277 field_header** _result) const; 278 status_t _AddField(const char* name, type_code type, 279 bool isFixedSize, field_header** _result); 280 status_t _RemoveField(field_header* field); 281 282 ssize_t _NativeFlattenedSize() const; 283 status_t _NativeFlatten(char *buffer, ssize_t size) const; 284 status_t _NativeFlatten(BDataIO *stream, ssize_t *size = NULL) const; 285 void _PrintToStream(const char* indent) const; 286 287 private: 288 message_header* fHeader; 289 field_header* fFields; 290 uint8* fData; 291 292 mutable BMessage* fOriginal; 293 294 BMessage* fQueueLink; 295 // fQueueLink is used by BMessageQueue to build a linked list 296 297 uint32 fReserved[11]; 298 299 // deprecated 300 BMessage(BMessage *message); 301 302 virtual void _ReservedMessage1(); 303 virtual void _ReservedMessage2(); 304 virtual void _ReservedMessage3(); 305 306 status_t _SendMessage(port_id port, team_id portOwner, int32 token, 307 bigtime_t timeout, bool replyRequired, 308 BMessenger &replyTo) const; 309 status_t _SendMessage(port_id port, team_id portOwner, 310 int32 token, BMessage *reply, bigtime_t sendTimeout, 311 bigtime_t replyTimeout) const; 312 static status_t _SendFlattenedMessage(void *data, int32 size, 313 port_id port, int32 token, bigtime_t timeout); 314 315 static void _StaticInit(); 316 static void _StaticCleanup(); 317 static void _StaticCacheCleanup(); 318 static int32 _StaticGetCachedReplyPort(); 319 320 enum { sNumReplyPorts = 3 }; 321 static port_id sReplyPorts[sNumReplyPorts]; 322 static long sReplyPortInUse[sNumReplyPorts]; 323 static int32 sGetCachedReplyPort(); 324 325 static BBlockCache* sMsgCache; 326 }; 327 328 #endif // _MESSAGE_H 329