From 200ca388246e525f6e8f909766977f534c0c897e Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Thu, 21 Jun 2018 15:32:52 +0200 Subject: teach useuniqueptr loplugin about calling delete on a param which is often a useful indicator that the callers can be made to use std::unique_ptr Change-Id: Idb1745d1f58dbcf389c9f60f1a5728d7d092ade5 Reviewed-on: https://gerrit.libreoffice.org/56238 Tested-by: Jenkins Reviewed-by: Noel Grandin --- .../unix/sysshell/recently_used_file_handler.cxx | 42 +++++++--------------- 1 file changed, 12 insertions(+), 30 deletions(-) (limited to 'shell') diff --git a/shell/source/unix/sysshell/recently_used_file_handler.cxx b/shell/source/unix/sysshell/recently_used_file_handler.cxx index 6ca13932143c..ced51eda9787 100644 --- a/shell/source/unix/sysshell/recently_used_file_handler.cxx +++ b/shell/source/unix/sysshell/recently_used_file_handler.cxx @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -194,7 +195,7 @@ namespace /* private */ { string_container_t groups_; }; - typedef std::vector recently_used_item_list_t; + typedef std::vector> recently_used_item_list_t; typedef void (recently_used_item::* SET_COMMAND)(const string_t&); // thrown if we encounter xml tags that we do not know @@ -205,7 +206,6 @@ namespace /* private */ { { public: explicit recently_used_file_filter(recently_used_item_list_t& item_list) : - item_(nullptr), item_list_(item_list) { named_command_map_[TAG_RECENT_FILES] = &recently_used_item::set_nothing; @@ -227,7 +227,7 @@ namespace /* private */ { const xml_tag_attribute_container_t& /*attributes*/) override { if ((local_name == TAG_RECENT_ITEM) && (nullptr == item_)) - item_ = new recently_used_item; + item_.reset(new recently_used_item); } virtual void end_element(const string_t& /*raw_name*/, const string_t& local_name) override @@ -237,17 +237,16 @@ namespace /* private */ { return; // will result in an XML parser error anyway if (named_command_map_.find(local_name) != named_command_map_.end()) - (item_->*named_command_map_[local_name])(current_element_); + (item_.get()->*named_command_map_[local_name])(current_element_); else { - delete item_; + item_.reset(); throw unknown_xml_format_exception(); } if (local_name == TAG_RECENT_ITEM) { - item_list_.push_back(item_); - item_ = nullptr; + item_list_.push_back(std::move(item_)); } current_element_.clear(); } @@ -264,7 +263,7 @@ namespace /* private */ { virtual void comment(const string_t& /*comment*/) override {} private: - recently_used_item* item_; + std::unique_ptr item_; std::map named_command_map_; string_t current_element_; recently_used_item_list_t& item_list_; @@ -301,7 +300,7 @@ namespace /* private */ { items_written_(0) {} - void operator() (const recently_used_item* item) + void operator() (const std::unique_ptr & item) { if (items_written_++ < MAX_RECENTLY_USED_ITEMS) item->write_xml(file_); @@ -338,23 +337,6 @@ namespace /* private */ { } - struct delete_recently_used_item - { - void operator() (const recently_used_item* item) const - { delete item; } - }; - - - void recently_used_item_list_clear(recently_used_item_list_t& item_list) - { - std::for_each( - item_list.begin(), - item_list.end(), - delete_recently_used_item()); - item_list.clear(); - } - - class find_item_predicate { public: @@ -362,7 +344,7 @@ namespace /* private */ { uri_(uri) {} - bool operator() (const recently_used_item* item) const + bool operator() (const std::unique_ptr & item) const { return (item->uri_ == uri_); } private: string_t uri_; @@ -371,7 +353,7 @@ namespace /* private */ { struct greater_recently_used_item { - bool operator ()(const recently_used_item* lhs, const recently_used_item* rhs) const + bool operator ()(const std::unique_ptr & lhs, const std::unique_ptr & rhs) const { return (lhs->timestamp_ > rhs->timestamp_); } }; @@ -416,7 +398,7 @@ namespace /* private */ { if (mimetype.length() == 0) mimetype = "application/octet-stream"; - item_list.push_back(new recently_used_item(uri, mimetype, groups)); + item_list.emplace_back(new recently_used_item(uri, mimetype, groups)); } // sort decreasing after the timestamp @@ -434,7 +416,7 @@ namespace /* private */ { item_list_(item_list) {} ~cleanup_guard() - { recently_used_item_list_clear(item_list_); } + { item_list_.clear(); } recently_used_item_list_t& item_list_; }; -- cgit v1.2.3