From 84dc21e19e21cf033f3c29e0526893623df7a562 Mon Sep 17 00:00:00 2001 From: Leo Rouleau Date: Sun, 5 Apr 2026 17:16:00 -0400 Subject: [PATCH] Devices: Make get methods of Device const adds const qualifiers to the devices class getter methods. Resolves the TODO: Make Get methods of device const in documentation/Todo.txt Changes: - Added const qualifiers to getter methods in Device.h and Device.cpp - replaced operator[] with find() in Device.h and Device.cpp to prevent map modification in const - Removed the corresponding Todo entry in documentation/Todo.txt Change-Id: I78d5ea31b2d7539bcefda7b3e57a484d16103c25 Reviewed-on: https://review.haiku-os.org/c/haiku/+/10666 Tested-by: Commit checker robot Reviewed-by: Adrien Destugues --- src/apps/devices/Device.cpp | 32 +++++++++++++++++-------- src/apps/devices/Device.h | 22 ++++++++++------- src/apps/devices/Documentation/Todo.txt | 1 - 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/apps/devices/Device.cpp b/src/apps/devices/Device.cpp index 09fc202bcd..8cccaf19d4 100644 --- a/src/apps/devices/Device.cpp +++ b/src/apps/devices/Device.cpp @@ -74,30 +74,42 @@ Device::~Device() BString -Device::GetName() +Device::GetName() const { - return fAttributeMap[B_TRANSLATE("Device name")]; + AttributeMapIterator it = fAttributeMap.find(B_TRANSLATE("Device name")); + if (it != fAttributeMap.end()) + return it->second; + return BString("unkown"); } BString -Device::GetManufacturer() +Device::GetManufacturer() const { - return fAttributeMap[B_TRANSLATE("Manufacturer")]; + AttributeMapIterator it = fAttributeMap.find(B_TRANSLATE("Manufacturer")); + if (it != fAttributeMap.end()) + return it->second; + return BString("Unknown"); } BString -Device::GetDriverUsed() +Device::GetDriverUsed() const { - return fAttributeMap[B_TRANSLATE("Driver used")]; + AttributeMapIterator it = fAttributeMap.find(B_TRANSLATE("Driver used")); + if (it != fAttributeMap.end()) + return it->second; + return BString("Unknown"); } BString -Device::GetDevPathsPublished() +Device::GetDevPathsPublished() const { - return fAttributeMap[B_TRANSLATE("Device paths")]; + AttributeMapIterator it = fAttributeMap.find(B_TRANSLATE("Device paths")); + if (it != fAttributeMap.end()) + return it->second; + return BString("Unknown"); } @@ -112,7 +124,7 @@ Device::SetAttribute(const BString& name, const BString& value) Attributes -Device::GetAllAttributes() +Device::GetAllAttributes() const { Attributes attributes; AttributeMapIterator iter; @@ -124,7 +136,7 @@ Device::GetAllAttributes() BString -Device::GetAllStrings() +Device::GetAllStrings() const { BString str; AttributeMapIterator iter; diff --git a/src/apps/devices/Device.h b/src/apps/devices/Device.h index 7df1af5323..83969f84b6 100644 --- a/src/apps/devices/Device.h +++ b/src/apps/devices/Device.h @@ -83,10 +83,10 @@ public: const BString& devPathsPublished = "unknown"); virtual ~Device(); - virtual BString GetName(); - virtual BString GetManufacturer(); - virtual BString GetDriverUsed(); - virtual BString GetDevPathsPublished(); + virtual BString GetName() const; + virtual BString GetManufacturer() const; + virtual BString GetDriverUsed() const; + virtual BString GetDevPathsPublished() const; virtual Category GetCategory() const { return fCategory; } virtual Device* GetPhysicalParent() const @@ -94,12 +94,16 @@ public: virtual BusType GetBusType() const { return fBusType; } - virtual Attributes GetAllAttributes(); - virtual BString GetAllStrings(); + virtual Attributes GetAllAttributes() const; + virtual BString GetAllStrings() const; - virtual Attribute GetAttribute(const BString& name) - { return Attribute(name.String(), - fAttributeMap[name]); } + virtual Attribute GetAttribute(const BString& name) const + { + AttributeMapIterator it = fAttributeMap.find(name); + if (it != fAttributeMap.end()) + return Attribute(name.String(), it->second); + return Attribute(name.String(), ""); + } virtual void SetAttribute(const BString& name, const BString& value); diff --git a/src/apps/devices/Documentation/Todo.txt b/src/apps/devices/Documentation/Todo.txt index 89f01106fb..04e76ba6d3 100644 --- a/src/apps/devices/Documentation/Todo.txt +++ b/src/apps/devices/Documentation/Todo.txt @@ -1,6 +1,5 @@ TODO: * There still is a memory leak somewhere, hunt it down - * Make Get methods of Device const * Fix view color? * Fix forwarding of messages to view * Bottom scrollbar strangeness