diff options
author | Tobias Markmann <tm@ayena.de> | 2017-03-30 07:32:28 (GMT) |
---|---|---|
committer | Tobias Markmann <tm@ayena.de> | 2017-03-31 08:45:43 (GMT) |
commit | 7663ca75731c4313dddbcde4d85f10383644a67a (patch) | |
tree | ecb1ed4c08b71bb58efc61980166e5064fefe68e /Swift/Controllers/Roster/GroupRosterItem.cpp | |
parent | 4d0391d824aaf94fbe152778581d51fecd588f6c (diff) | |
download | swift-7663ca75731c4313dddbcde4d85f10383644a67a.zip swift-7663ca75731c4313dddbcde4d85f10383644a67a.tar.bz2 |
Return unique_ptr instead of pointer to deleted object
Coverity raised this issue.
Test-Information:
Code builds on macOS 10.12.4 and all unit tests pass; Swift
runs fine.
Change-Id: I8fb0805f6b2e0a21674ea32c0b1aee9e7b985639
Diffstat (limited to 'Swift/Controllers/Roster/GroupRosterItem.cpp')
-rw-r--r-- | Swift/Controllers/Roster/GroupRosterItem.cpp | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/Swift/Controllers/Roster/GroupRosterItem.cpp b/Swift/Controllers/Roster/GroupRosterItem.cpp index ac40afd..af5d0ca 100644 --- a/Swift/Controllers/Roster/GroupRosterItem.cpp +++ b/Swift/Controllers/Roster/GroupRosterItem.cpp @@ -1,40 +1,41 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2017 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swift/Controllers/Roster/GroupRosterItem.h> + +#include <memory> + #include <boost/bind.hpp> -//#include <boost/algorithm.hpp> -#include <iostream> namespace Swift { GroupRosterItem::GroupRosterItem(const std::string& name, GroupRosterItem* parent, bool sortByStatus) : RosterItem(name, parent), sortByStatus_(sortByStatus), manualSort_(false) { expanded_ = true; } GroupRosterItem::~GroupRosterItem() { } void GroupRosterItem::setManualSort(const std::string& manualSortValue) { manualSort_ = true; bool changed = manualSortValue_ != manualSortValue; manualSortValue_ = manualSortValue; if (changed) { onChildrenChanged(); onDataChanged(); } } const std::string& GroupRosterItem::getSortableDisplayName() const { return manualSort_ ? manualSortValue_ : RosterItem::getSortableDisplayName(); } bool GroupRosterItem::isExpanded() const { return expanded_; } /** @@ -70,95 +71,93 @@ void GroupRosterItem::addChild(RosterItem* item) { onChildrenChanged(); onDataChanged(); } /** * Does not emit a changed signal. */ void GroupRosterItem::removeAll() { std::vector<RosterItem*>::iterator it = children_.begin(); displayedChildren_.clear(); while (it != children_.end()) { ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(*it); if (contact) { delete contact; } else { GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it); if (group) { group->removeAll(); delete group; } } ++it; } children_.clear(); } /** * Returns the removed item - but only if it's the only one, otherwise * the return result is undefined. */ -ContactRosterItem* GroupRosterItem::removeChild(const JID& jid) { - std::vector<RosterItem*>::iterator it = children_.begin(); - ContactRosterItem* removed = nullptr; +std::unique_ptr<ContactRosterItem> GroupRosterItem::removeChild(const JID& jid) { + auto it = children_.begin(); + std::unique_ptr<ContactRosterItem> removed; while (it != children_.end()) { ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(*it); if (contact && contact->getJID() == jid) { displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), contact), displayedChildren_.end()); - removed = contact; - delete contact; + removed = std::unique_ptr<ContactRosterItem>(contact); it = children_.erase(it); continue; } GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it); if (group) { - ContactRosterItem* groupRemoved = group->removeChild(jid); + auto groupRemoved = group->removeChild(jid); if (groupRemoved) { - removed = groupRemoved; + removed = std::move(groupRemoved); } } ++it; } onChildrenChanged(); onDataChanged(); return removed; } -GroupRosterItem* GroupRosterItem::removeGroupChild(const std::string& groupName) { +std::unique_ptr<GroupRosterItem> GroupRosterItem::removeGroupChild(const std::string& groupName) { std::vector<RosterItem*>::iterator it = children_.begin(); - GroupRosterItem* removed = nullptr; + std::unique_ptr<GroupRosterItem> removed; while (it != children_.end()) { GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it); if (group && group->getDisplayName() == groupName) { displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), group), displayedChildren_.end()); - removed = group; - delete group; + removed = std::unique_ptr<GroupRosterItem>(group); it = children_.erase(it); continue; } ++it; } onChildrenChanged(); onDataChanged(); return removed; } /** * Returns false if the list didn't need a resort */ bool GroupRosterItem::sortDisplayed() { /* Not doing this until we import boost::algorithm*/ // if (boost::is_sorted(displayedChildren_begin(), displayedChildren_.end(), itemLessThan)) { // return false; // } //Sholudn't need stable_sort here std::sort(displayedChildren_.begin(), displayedChildren_.end(), sortByStatus_? itemLessThanWithStatus : itemLessThanWithoutStatus); return true; } bool GroupRosterItem::itemLessThanWithoutStatus(const RosterItem* left, const RosterItem* right) { return left->getSortableDisplayName() < right->getSortableDisplayName(); } bool GroupRosterItem::itemLessThanWithStatus(const RosterItem* left, const RosterItem* right) { const ContactRosterItem* leftContact = dynamic_cast<const ContactRosterItem*>(left); const ContactRosterItem* rightContact = dynamic_cast<const ContactRosterItem*>(right); |