hrev44739 adds 1 changeset to branch 'master' old head: 4373e37dd8d9e3b6f1c7b303c74dde27c3e282db new head: 6e50e79e543b83ed2983b23a018abd3caa93efaf ---------------------------------------------------------------------------- 6e50e79: Added Get*()/Set*() methods like the ones from KMessage. [ Axel DÃrfler <axeld@xxxxxxxxxxxxxxxx> ] ---------------------------------------------------------------------------- Revision: hrev44739 Commit: 6e50e79e543b83ed2983b23a018abd3caa93efaf URL: http://cgit.haiku-os.org/haiku/commit/?id=6e50e79 Author: Axel DÃrfler <axeld@xxxxxxxxxxxxxxxx> Date: Sat Nov 3 10:52:25 2012 UTC ---------------------------------------------------------------------------- 2 files changed, 202 insertions(+), 9 deletions(-) headers/os/app/Message.h | 95 ++++++++++++++++++++++++++++++++++- src/kits/app/Message.cpp | 116 ++++++++++++++++++++++++++++++++++++++++--- ---------------------------------------------------------------------------- diff --git a/headers/os/app/Message.h b/headers/os/app/Message.h index 7b3b9f6..c4c72df 100644 --- a/headers/os/app/Message.h +++ b/headers/os/app/Message.h @@ -378,9 +378,9 @@ public: bool HasRef(const char* name, int32 n = 0) const; bool HasMessage(const char* name, int32 n = 0) const; bool HasFlat(const char* name, - const BFlattenable* ) const; + const BFlattenable* object) const; bool HasFlat(const char* name, int32 n, - const BFlattenable* ) const; + const BFlattenable* object) const; bool HasData(const char* name, type_code , int32 n = 0) const; BRect FindRect(const char* name, int32 n = 0) const; @@ -394,6 +394,97 @@ public: float FindFloat(const char* name, int32 n = 0) const; double FindDouble(const char* name, int32 n = 0) const; + // Convenience methods + bool GetBool(const char* name, + bool defaultValue) const; + bool GetBool(const char* name, int32 index, + bool defaultValue) const; + int8 GetInt8(const char* name, + int8 defaultValue) const; + int8 GetInt8(const char* name, int32 index, + int8 defaultValue) const; + uint8 GetUInt8(const char* name, + uint8 defaultValue) const; + uint8 GetUInt8(const char* name, int32 index, + uint8 defaultValue) const; + int16 GetInt16(const char* name, + int16 defaultValue) const; + int16 GetInt16(const char* name, int32 index, + int16 defaultValue) const; + uint16 GetUInt16(const char* name, + uint16 defaultValue) const; + uint16 GetUInt16(const char* name, int32 index, + uint16 defaultValue) const; + int32 GetInt32(const char* name, + int32 defaultValue) const; + int32 GetInt32(const char* name, int32 index, + int32 defaultValue) const; + uint32 GetUInt32(const char* name, + uint32 defaultValue) const; + uint32 GetUInt32(const char* name, int32 index, + uint32 defaultValue) const; + int64 GetInt64(const char* name, + int64 defaultValue) const; + int64 GetInt64(const char* name, int32 index, + int64 defaultValue) const; + uint64 GetUInt64(const char* name, + uint64 defaultValue) const; + uint64 GetUInt64(const char* name, int32 index, + uint64 defaultValue) const; + float GetFloat(const char* name, + float defaultValue) const; + float GetFloat(const char* name, int32 index, + float defaultValue) const; + double GetDouble(const char* name, + double defaultValue) const; + double GetDouble(const char* name, int32 index, + double defaultValue) const; + void* GetPointer(const char* name, + const void* defaultValue) const; + void* GetPointer(const char* name, int32 index, + const void* defaultValue) const; + const char* GetString(const char* name, + const char* defaultValue) const; + const char* GetString(const char* name, int32 index, + const char* defaultValue) const; + BAlignment GetAlignment(const char* name, int32 index, + const BAlignment& defaultValue) const; + BAlignment GetAlignment(const char* name, + const BAlignment& defaultValue) const; + BRect GetRect(const char* name, int32 index, + const BRect& defaultValue) const; + BRect GetRect(const char* name, + const BRect& defaultValue) const; + BPoint GetPoint(const char* name, int32 index, + const BPoint& defaultValue) const; + BPoint GetPoint(const char* name, + const BPoint& defaultValue) const; + BSize GetSize(const char* name, int32 index, + const BSize& defaultValue) const; + BSize GetSize(const char* name, + const BSize& defaultValue) const; + + // fixed size fields only + status_t SetBool(const char* name, bool value); + status_t SetInt8(const char* name, int8 value); + status_t SetUInt8(const char* name, uint8 value); + status_t SetInt16(const char* name, int16 value); + status_t SetUInt16(const char* name, uint16 value); + status_t SetInt32(const char* name, int32 value); + status_t SetUInt32(const char* name, uint32 value); + status_t SetInt64(const char* name, int64 value); + status_t SetUInt64(const char* name, uint64 value); + status_t SetPointer(const char* name, const void* value); + status_t SetFloat(const char* name, float value); + status_t SetDouble(const char* name, double value); + status_t SetAlignment(const char* name, + const BAlignment& value); + status_t SetPoint(const char* name, const BPoint& value); + status_t SetRect(const char* name, const BRect& value); + status_t SetSize(const char* name, const BSize& value); + status_t SetData(const char* name, type_code type, + const void* data, ssize_t numBytes); + class Private; struct message_header; struct field_header; diff --git a/src/kits/app/Message.cpp b/src/kits/app/Message.cpp index 4b9e5db..ecfa625 100644 --- a/src/kits/app/Message.cpp +++ b/src/kits/app/Message.cpp @@ -2395,9 +2395,12 @@ BMessage::_SendFlattenedMessage(void *data, int32 size, port_id port, } -void BMessage::_ReservedMessage1(void) {}; -void BMessage::_ReservedMessage2(void) {}; -void BMessage::_ReservedMessage3(void) {}; +void BMessage::_ReservedMessage1() {} +void BMessage::_ReservedMessage2() {} +void BMessage::_ReservedMessage3() {} + + +// #pragma mark - Macro definitions for data access methods /* Relay functions from here on (Add... -> AddData, Find... -> FindData) */ @@ -2409,6 +2412,7 @@ BMessage::Add##typeName(const char *name, type val) \ return AddData(name, typeCode, &val, sizeof(type), true); \ } \ \ + \ status_t \ BMessage::Find##typeName(const char *name, type *p) const \ { \ @@ -2425,6 +2429,7 @@ BMessage::Find##typeName(const char *name, type *p) const \ return error; \ } \ \ + \ status_t \ BMessage::Find##typeName(const char *name, int32 index, type *p) const \ { \ @@ -2441,18 +2446,21 @@ BMessage::Find##typeName(const char *name, int32 index, type *p) const \ return error; \ } \ \ + \ status_t \ -BMessage::Replace##typeName(const char *name, type val) \ +BMessage::Replace##typeName(const char *name, type value) \ { \ - return ReplaceData(name, typeCode, 0, &val, sizeof(type)); \ + return ReplaceData(name, typeCode, 0, &value, sizeof(type)); \ } \ \ + \ status_t \ -BMessage::Replace##typeName(const char *name, int32 index, type val) \ +BMessage::Replace##typeName(const char *name, int32 index, type value) \ { \ - return ReplaceData(name, typeCode, index, &val, sizeof(type)); \ + return ReplaceData(name, typeCode, index, &value, sizeof(type)); \ } \ \ + \ bool \ BMessage::Has##typeName(const char *name, int32 index) const \ { \ @@ -2483,6 +2491,7 @@ BMessage::Has##typeName(const char *name, int32 index) const \ return HasData(name, typeCode, index); \ } + DEFINE_HAS_FUNCTION(Alignment, B_ALIGNMENT_TYPE); DEFINE_HAS_FUNCTION(String, B_STRING_TYPE); DEFINE_HAS_FUNCTION(Pointer, B_POINTER_TYPE); @@ -2492,6 +2501,7 @@ DEFINE_HAS_FUNCTION(Message, B_MESSAGE_TYPE); #undef DEFINE_HAS_FUNCTION + #define DEFINE_LAZY_FIND_FUNCTION(type, typeName, initialize) \ type \ BMessage::Find##typeName(const char *name, int32 index) const \ @@ -2501,6 +2511,7 @@ BMessage::Find##typeName(const char *name, int32 index) const \ return val; \ } + DEFINE_LAZY_FIND_FUNCTION(BRect, Rect, BRect()); DEFINE_LAZY_FIND_FUNCTION(BPoint, Point, BPoint()); DEFINE_LAZY_FIND_FUNCTION(const char *, String, NULL); @@ -2514,6 +2525,83 @@ DEFINE_LAZY_FIND_FUNCTION(double, Double, 0); #undef DEFINE_LAZY_FIND_FUNCTION + +#define DEFINE_SET_GET_FUNCTIONS(type, typeName, typeCode) \ +type \ +BMessage::Get##typeName(const char *name, type defaultValue) const \ +{ \ + return Get##typeName(name, 0, defaultValue); \ +} \ + \ + \ +type \ +BMessage::Get##typeName(const char *name, int32 index, \ + type defaultValue) const \ +{ \ + type value; \ + if (Find##typeName(name, index, &value) == B_OK) \ + return value; \ + \ + return defaultValue; \ +} \ + \ + \ +status_t \ +BMessage::Set##typeName(const char *name, type value) \ +{ \ + return SetData(name, typeCode, &value, sizeof(type)); \ +} \ + + +DEFINE_SET_GET_FUNCTIONS(int8, Int8, B_INT8_TYPE); +DEFINE_SET_GET_FUNCTIONS(uint8, UInt8, B_UINT8_TYPE); +DEFINE_SET_GET_FUNCTIONS(int16, Int16, B_INT16_TYPE); +DEFINE_SET_GET_FUNCTIONS(uint16, UInt16, B_UINT16_TYPE); +DEFINE_SET_GET_FUNCTIONS(int32, Int32, B_INT32_TYPE); +DEFINE_SET_GET_FUNCTIONS(uint32, UInt32, B_UINT32_TYPE); +DEFINE_SET_GET_FUNCTIONS(int64, Int64, B_INT64_TYPE); +DEFINE_SET_GET_FUNCTIONS(uint64, UInt64, B_UINT64_TYPE); +DEFINE_SET_GET_FUNCTIONS(bool, Bool, B_BOOL_TYPE); +DEFINE_SET_GET_FUNCTIONS(float, Float, B_FLOAT_TYPE); +DEFINE_SET_GET_FUNCTIONS(double, Double, B_DOUBLE_TYPE); + +#undef DEFINE_SET_GET_FUNCTION + + +#define DEFINE_SET_GET_BY_REFERENCE_FUNCTIONS(type, typeName, typeCode) \ +type \ +BMessage::Get##typeName(const char *name, const type& defaultValue) const \ +{ \ + return Get##typeName(name, 0, defaultValue); \ +} \ + \ + \ +type \ +BMessage::Get##typeName(const char *name, int32 index, \ + const type& defaultValue) const \ +{ \ + type value; \ + if (Find##typeName(name, index, &value) == B_OK) \ + return value; \ + \ + return defaultValue; \ +} \ + \ + \ +status_t \ +BMessage::Set##typeName(const char *name, const type& value) \ +{ \ + return SetData(name, typeCode, &value, sizeof(type)); \ +} \ + + +DEFINE_SET_GET_BY_REFERENCE_FUNCTIONS(BPoint, Point, B_POINT_TYPE); +DEFINE_SET_GET_BY_REFERENCE_FUNCTIONS(BRect, Rect, B_RECT_TYPE); +DEFINE_SET_GET_BY_REFERENCE_FUNCTIONS(BSize, Size, B_SIZE_TYPE); + +#undef DEFINE_SET_GET_BY_REFERENCE_FUNCTIONS + + status_t BMessage::AddAlignment(const char *name, const BAlignment &alignment) { @@ -3044,3 +3132,17 @@ BMessage::HasFlat(const char *name, int32 index, const BFlattenable *object) { return HasData(name, object->TypeCode(), index); } + + +status_t +BMessage::SetData(const char* name, type_code type, const void* data, + ssize_t numBytes) +{ + if (numBytes <= 0 || data == NULL) + return B_BAD_VALUE; + + if (ReplaceData(name, type, data, numBytes) == B_OK) + return B_OK; + + return AddData(name, type, data, numBytes); +}