[haiku-commits] r43112 - haiku/trunk/src/apps/debugger/settings

  • From: anevilyak@xxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Wed, 2 Nov 2011 15:01:25 +0100 (CET)

Author: anevilyak
Date: 2011-11-02 15:01:24 +0100 (Wed, 02 Nov 2011)
New Revision: 43112
Changeset: https://dev.haiku-os.org/changeset/43112

Modified:
   haiku/trunk/src/apps/debugger/settings/GUITeamUISettings.cpp
   haiku/trunk/src/apps/debugger/settings/GUITeamUISettings.h
Log:
Further flesh out GUITeamUISettings to be able to save/restore. This should be
more or less complete now, or at least far enough along to start doing some
things with it.



Modified: haiku/trunk/src/apps/debugger/settings/GUITeamUISettings.cpp
===================================================================
--- haiku/trunk/src/apps/debugger/settings/GUITeamUISettings.cpp        
2011-11-02 13:28:01 UTC (rev 43111)
+++ haiku/trunk/src/apps/debugger/settings/GUITeamUISettings.cpp        
2011-11-02 14:01:24 UTC (rev 43112)
@@ -23,12 +23,14 @@
 
 GUITeamUISettings::GUITeamUISettings(const GUITeamUISettings& other)
 {
-       fID = other.fID;
+       if (_SetTo(other) != B_OK)
+               throw std::bad_alloc();
 }
 
 
 GUITeamUISettings::~GUITeamUISettings()
 {
+       _Unset();
 }
 
 
@@ -50,7 +52,11 @@
 GUITeamUISettings::SetTo(const BMessage& archive)
 {
        status_t error = archive.FindString("ID", &fID);
-       
+       if (error != B_OK)
+               return error;
+
+       error = archive.FindMessage("values", &fValues);
+
        return error;
 }
 
@@ -61,9 +67,13 @@
        status_t error = archive.AddString("ID", fID);
        if (error != B_OK)
                return error;
-               
+
        error = archive.AddInt32("type", Type());
-       
+       if (error != B_OK)
+               return error;
+
+       error = archive.AddMessage("values", &fValues);
+
        return error;
 }
 
@@ -71,15 +81,110 @@
 TeamUISettings*
 GUITeamUISettings::Clone() const
 {
-       GUITeamUISettings* settings = new GUITeamUISettings(fID.String());
-       
+       GUITeamUISettings* settings = new(std::nothrow) GUITeamUISettings();
+
+       if (settings == NULL)
+               return NULL;
+
+       if (settings->_SetTo(*this) != B_OK) {
+               delete settings;
+               return NULL;
+       }
+
        return settings;
 }
 
 
+status_t
+GUITeamUISettings::AddSetting(Setting* setting)
+{
+       if (!fSettings.AddItem(setting))
+               return B_NO_MEMORY;
+
+       setting->AcquireReference();
+
+       return B_OK;
+}
+
+
+bool
+GUITeamUISettings::SetValue(const Setting* setting, const BVariant& value)
+{
+       const char* fieldName = setting->ID();
+       fValues.RemoveName(fieldName);
+
+       return value.AddToMessage(fValues, fieldName) == B_OK;
+}
+
+
+BVariant
+GUITeamUISettings::Value(const Setting* setting) const
+{
+       BVariant value;
+
+       return value.SetFromMessage(fValues, setting->ID()) == B_OK ?
+               value : setting->DefaultValue();
+}
+
+
+BVariant
+GUITeamUISettings::Value(const char* settingID) const
+{
+       BVariant value;
+
+       status_t result = value.SetFromMessage(fValues, settingID);
+
+       if (result != B_OK) {
+               for (int32 i = 0; i < fSettings.CountItems(); i++) {
+                       Setting* setting = fSettings.ItemAt(i);
+                       if (strcmp(setting->ID(), settingID) == 0) {
+                               value = setting->DefaultValue();
+                               break;
+                       }
+               }
+       }
+
+       return value;
+}
+
+
 GUITeamUISettings&
 GUITeamUISettings::operator=(const GUITeamUISettings& other)
 {
-       fID = other.fID;
+       if (_SetTo(other) != B_OK)
+               throw std::bad_alloc();
+
        return *this;
 }
+
+
+status_t
+GUITeamUISettings::_SetTo(const GUITeamUISettings& other)
+{
+       _Unset();
+
+       for (int32 i = 0; i < other.fSettings.CountItems(); i++) {
+               Setting* setting = other.fSettings.ItemAt(i);
+               if (!fSettings.AddItem(setting))
+                       return B_NO_MEMORY;
+
+               setting->AcquireReference();
+       }
+
+       fValues = other.fValues;
+
+       return B_OK;
+}
+
+
+void
+GUITeamUISettings::_Unset()
+{
+       fID.Truncate(0);
+
+       for (int32 i = 0; i < fSettings.CountItems(); i++)
+               fSettings.ItemAt(i)->ReleaseReference();
+
+       fSettings.MakeEmpty();
+       fValues.MakeEmpty();
+}

Modified: haiku/trunk/src/apps/debugger/settings/GUITeamUISettings.h
===================================================================
--- haiku/trunk/src/apps/debugger/settings/GUITeamUISettings.h  2011-11-02 
13:28:01 UTC (rev 43111)
+++ haiku/trunk/src/apps/debugger/settings/GUITeamUISettings.h  2011-11-02 
14:01:24 UTC (rev 43112)
@@ -6,15 +6,15 @@
 #define GUI_TEAM_UI_SETTINGS_H
 
 
+#include <Message.h>
 #include <String.h>
 
 #include <ObjectList.h>
 
+#include "Setting.h"
 #include "TeamUISettings.h"
 
-class BMessage;
 
-
 class GUITeamUISettings : public TeamUISettings {
 public:
                                                                
GUITeamUISettings();
@@ -30,12 +30,25 @@
        virtual status_t                        WriteTo(BMessage& archive) 
const;
        virtual TeamUISettings*         Clone() const;
 
+                       status_t                        AddSetting(Setting* 
setting);
+                       bool                            SetValue(const Setting* 
setting,
+                                                                       const 
BVariant& value);
+                       BVariant                        Value(const Setting* 
setting) const;
+                       BVariant                        Value(const char* 
settingID) const;
+
                        GUITeamUISettings&      operator=(const 
GUITeamUISettings& other);
                                                                        // 
throws std::bad_alloc
 
 private:
+                       typedef BObjectList<Setting> SettingsList;
+private:
 
-                       BString                         fID;                    
+                       status_t                        _SetTo(const 
GUITeamUISettings& other);
+                       void                            _Unset();
+
+                       SettingsList            fSettings;
+                       BMessage                        fValues;
+                       BString                         fID;
 };
 
 


Other related posts:

  • » [haiku-commits] r43112 - haiku/trunk/src/apps/debugger/settings - anevilyak