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