So Now I have extend the BMessage with Save essage stuff from showimage. This are how it would look like. Sorry for posting this here and not on track but I think it's easier to comment things here :). What I would like to know are. Are this good enough to be commitded :) This have not been tested more that I can build it.. git diff any got me the diff for existing files, the one I added with git add [file] was not showd with diff.. The SaveMessage class was added to storage kit. -- MVH Fredrik Modèen
diff --git a/headers/os/app/Message.h b/headers/os/app/Message.h index 2920a1a..dcac766 100644 --- a/headers/os/app/Message.h +++ b/headers/os/app/Message.h @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010, Haiku Inc. All Rights Reserved. + * Copyright 2005-2012, Haiku Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -206,6 +206,53 @@ class BMessage { const void **data, ssize_t *numBytes) const; status_t FindData(const char *name, type_code type, int32 index, const void **data, ssize_t *numBytes) const; + //Get Data + 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; + int16 GetInt16(const char* name, + int16 defaultValue) const; + int16 GetInt16(const char* name, int32 index, + int16 defaultValue) const; + int32 GetInt32(const char* name, + int32 defaultValue) const; + int32 GetInt32(const char* name, int32 index, + int32 defaultValue) const; + int64 GetInt64(const char* name, + int64 defaultValue) const; + int64 GetInt64(const char* name, int32 index, + int64 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; + + //Set Data + void SetBool(const char* name, bool value); + void SetInt8(const char* name, int8 value); + void SetUInt8(const char* name, uint8 value); + void SetInt16(const char* name, int16 value); + void SetUInt16(const char* name, uint16 value); + void SetInt32(const char* name, int32 value); + void SetUInt32(const char* name, uint32 value); + void SetInt64(const char* name, int64 value); + void SetUInt64(const char* name, uint64 value); + void SetFloat(const char* name, float value); + void SetRect(const char* name, BRect value); + void SetTime(const char* name, bigtime_t value); + void SetString(const char* name, const char* value); + + bool HasChanged(){ return fUpdated;} + // Replacing data status_t ReplaceAlignment(const char* name, @@ -334,11 +381,19 @@ class BMessage { status_t _RemoveField(field_header* field); void _PrintToStream(const char* indent) const; - + + template<typename T> + inline status_t _FindType(const char* name, type_code type, + int32 index, T* value) const; + + template<typename T> + inline T _GetType(const char* name, type_code type, + int32 index, const T& defaultValue) const; private: message_header* fHeader; field_header* fFields; uint8* fData; + bool fUpdated; // used for easy checking Set## methods. uint32 fFieldsAvailable; size_t fDataAvailable; diff --git a/src/kits/app/Message.cpp b/src/kits/app/Message.cpp index a4af465..225f9ba 100644 --- a/src/kits/app/Message.cpp +++ b/src/kits/app/Message.cpp @@ -1,9 +1,11 @@ /* - * Copyright 2005-2011, Haiku Inc. All rights reserved. + * Copyright 2005-2012, Haiku Inc. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: * Michael Lotz <mmlr@xxxxxxxx> + * Michael Pfeiffer, laplace@xxxxxxxxxxxx (using his code for get/set) + * Fredrik Modéen, [firstname]@[lastname].se */ @@ -341,6 +343,7 @@ BMessage::_InitCommon(bool initHeader) fHeader = NULL; fFields = NULL; fData = NULL; + fUpdated = false; fFieldsAvailable = 0; fDataAvailable = 0; @@ -3009,3 +3012,298 @@ BMessage::HasFlat(const char *name, int32 index, const BFlattenable *object) { return HasData(name, object->TypeCode(), index); } + + +// #pragma mark - +// Get Data // +template<typename T> +status_t +BMessage::_FindType(const char* name, type_code type, int32 index, + T* value) const +{ + const void* data; + int32 size; + status_t error = FindData(name, type, index, &data, &size); + if (error != B_OK) + return error; + + if (size != sizeof(T)) + return B_BAD_DATA; + + *value = *(T*)data; + + return B_OK; +} + + +template<typename T> +inline T +BMessage::_GetType(const char* name, type_code type, int32 index, + const T& defaultValue) const +{ + T value; + if (_FindType(name, type, index, &value) == B_OK) + return value; + return defaultValue; +} + + +inline bool +BMessage::GetBool(const char* name, bool defaultValue) const +{ + return _GetType(name, B_BOOL_TYPE, 0, defaultValue); +} + + +inline bool +BMessage::GetBool(const char* name, int32 index, bool defaultValue) const +{ + return _GetType(name, B_BOOL_TYPE, index, defaultValue); +} + + +int8 +BMessage::GetInt8(const char* name, int8 defaultValue) const +{ + return _GetType(name, B_INT8_TYPE, 0, defaultValue); +} + + +int8 +BMessage::GetInt8(const char* name, int32 index, int8 defaultValue) const +{ + return _GetType(name, B_INT8_TYPE, index, defaultValue); +} + + +int16 +BMessage::GetInt16(const char* name, int16 defaultValue) const +{ + return _GetType(name, B_INT16_TYPE, 0, defaultValue); +} + + +int16 +BMessage::GetInt16(const char* name, int32 index, int16 defaultValue) const +{ + return _GetType(name, B_INT16_TYPE, index, defaultValue); +} + + +int32 +BMessage::GetInt32(const char* name, int32 defaultValue) const +{ + return _GetType(name, B_INT32_TYPE, 0, defaultValue); +} + + +int32 +BMessage::GetInt32(const char* name, int32 index, int32 defaultValue) const +{ + return _GetType(name, B_INT32_TYPE, index, defaultValue); +} + + +int64 +BMessage::GetInt64(const char* name, int64 defaultValue) const +{ + return _GetType(name, B_INT64_TYPE, 0, defaultValue); +} + + +int64 +BMessage::GetInt64(const char* name, int32 index, int64 defaultValue) const +{ + return _GetType(name, B_INT64_TYPE, index, defaultValue); +} + + +void* +BMessage::GetPointer(const char* name, const void* defaultValue) const +{ + return const_cast<void*>(_GetType(name, B_POINTER_TYPE, 0, defaultValue)); +} + + +void* +BMessage::GetPointer(const char* name, int32 index, + const void* defaultValue) const +{ + return const_cast<void*>(_GetType(name, B_POINTER_TYPE, index, + defaultValue)); +} + + +const char* +BMessage::GetString(const char* name, int32 index, + const char* defaultValue) const +{ + // don't use _GetType() here, since it checks field size == sizeof(T) + int32 size; + const char* value; + if (FindData(name, B_STRING_TYPE, index, (const void**)&value, &size) + == B_OK) { + return value; + } + return defaultValue; +} + + +const char* +BMessage::GetString(const char* name, const char* defaultValue) const +{ + return GetString(name, 0, defaultValue); +} + +void +BMessage::SetBool(const char* name, bool value) +{ + if (HasBool(name)) + ReplaceBool(name, value); + else + AddBool(name, value); + + fUpdated = true; +} + + +void +BMessage::SetInt8(const char* name, int8 value) +{ + if (HasInt8(name)) + ReplaceInt8(name, value); + else + AddInt8(name, value); + + fUpdated = true; +} + + +void +BMessage::SetUInt8(const char* name, uint8 value) +{ + if (HasUInt8(name)) + ReplaceUInt8(name, value); + else + AddUInt8(name, value); + + fUpdated = true; +} + + +void +BMessage::SetInt16(const char* name, int16 value) +{ + if (HasInt16(name)) + ReplaceInt16(name, value); + else + AddInt16(name, value); + + fUpdated = true; +} + + +void +BMessage::SetUInt16(const char* name, uint16 value) +{ + if (HasUInt16(name)) + ReplaceUInt16(name, value); + else + AddUInt16(name, value); + + fUpdated = true; +} + + +void +BMessage::SetInt32(const char* name, int32 value) +{ + if (HasInt32(name)) + ReplaceInt32(name, value); + else + AddInt32(name, value); + + fUpdated = true; +} + + +void +BMessage::SetUInt32(const char* name, uint32 value) +{ + if (HasUInt32(name)) + ReplaceUInt32(name, value); + else + AddUInt32(name, value); + + fUpdated = true; +} + + +void +BMessage::SetInt64(const char* name, int64 value) +{ + if (HasInt64(name)) + ReplaceInt64(name, value); + else + AddInt64(name, value); + + fUpdated = true; +} + + +void +BMessage::SetUInt64(const char* name, uint64 value) +{ + if (HasUInt64(name)) + ReplaceUInt64(name, value); + else + AddUInt64(name, value); + + fUpdated = true; +} + + +void +BMessage::SetFloat(const char* name, float value) +{ + if (HasFloat(name)) + ReplaceFloat(name, value); + else + AddFloat(name, value); + + fUpdated = true; +} + + +void +BMessage::SetRect(const char* name, BRect value) +{ + if (HasRect(name)) + ReplaceRect(name, value); + else + AddRect(name, value); + + fUpdated = true; +} + + +void +BMessage::SetTime(const char* name, bigtime_t value) +{ + if (ReplaceInt64(name, value) != B_OK) + AddInt64(name, value); + + fUpdated = true; +} + + +void +BMessage::SetString(const char* name, const char* value) +{ + if (HasString(name)) + ReplaceString(name, value); + else + AddString(name, value); + + fUpdated = true; +} diff --git a/src/kits/storage/Jamfile b/src/kits/storage/Jamfile index ea34109..058c56c 100644 --- a/src/kits/storage/Jamfile +++ b/src/kits/storage/Jamfile @@ -38,6 +38,7 @@ MergeObject <libbe>storage_kit.o : ResourcesContainer.cpp ResourceStrings.cpp Statable.cpp + SaveMessage.cpp SymLink.cpp Volume.cpp VolumeRoster.cpp
/* * Copyright 2003-2012 Haiku Inc. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: * Michael Pfeiffer, laplace@xxxxxxxxxxxx * Fredrik Modéen, [firstname]@[lastname].se */ #ifndef SAVE_MESSAGE_H #define SAVE_MESSAGE_H #include <Locker.h> #include <Message.h> class BFile; class SaveMessage { public: SaveMessage(); virtual ~SaveMessage(); static BMessage* Load(char* appname); static bool Save(BMessage* settings, char* appname); private: static bool _OpenSettingsFile(BFile* file, bool forReading, char* appname); }; #endif // SAVE_MESSAGE_H
/* * Copyright 2003-2012 Haiku Inc. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: * Michael Pfeiffer, laplace@xxxxxxxxxxxx * Fredrik Modéen, [firstname]@[lastname].se */ #include <SaveMessage.h> #include <File.h> #include <FindDirectory.h> #include <Path.h> SaveMessage::SaveMessage() { } SaveMessage::~SaveMessage() { } BMessage* SaveMessage::Load(char* appname) { BFile file; BMessage* settings = NULL; if (_OpenSettingsFile(&file, true, appname)) { settings->Unflatten(&file); return settings; } else return NULL; } bool SaveMessage::Save(BMessage* settings, char* appname) { BFile file; if (_OpenSettingsFile(&file, false, appname)) { settings->Flatten(&file); return true; } else return false; } bool SaveMessage::_OpenSettingsFile(BFile* file, bool forReading, char* appname) { BPath path; status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path); if (status != B_OK) return false; path.Append(appname); return file->SetTo(path.Path(), forReading ? B_READ_ONLY : B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE) == B_OK; }