From 2986bb489382abb879d98a7b6e8645fb9309c999 Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Mon, 1 Mar 2021 22:43:22 +0400 Subject: [PATCH] Ported hz::scoped_ptr to std::unique_ptr and removed it. --- src/gsc_add_device_window.cpp | 7 +- src/gsc_executor_log_window.cpp | 12 +- src/gsc_info_window.cpp | 7 +- src/gsc_main_window.cpp | 10 +- src/gsc_preferences_window.cpp | 7 +- src/gsc_text_window.h | 7 +- src/gsc_winres.in.rc | 2 +- src/hz/CMakeLists.txt | 1 - src/hz/launch_url.h | 9 +- src/hz/scoped_ptr.h | 216 -------------------------------- 10 files changed, 35 insertions(+), 243 deletions(-) delete mode 100644 src/hz/scoped_ptr.h diff --git a/src/gsc_add_device_window.cpp b/src/gsc_add_device_window.cpp index a1eca5b..f607acc 100644 --- a/src/gsc_add_device_window.cpp +++ b/src/gsc_add_device_window.cpp @@ -15,10 +15,10 @@ Copyright: #include #include #include // GDK_KEY_Escape +#include #include "hz/fs_ns.h" #include "hz/string_sprintf.h" -#include "hz/scoped_ptr.h" #include "applib/app_gtkmm_utils.h" #include "gsc_add_device_window.h" @@ -181,8 +181,9 @@ void GscAddDeviceWindow::on_device_name_browse_button_clicked() int result = 0; #if GTK_CHECK_VERSION(3, 20, 0) - hz::scoped_ptr dialog(gtk_file_chooser_native_new( - _("Choose Device..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_OPEN, nullptr, nullptr), g_object_unref); + std::unique_ptr dialog(gtk_file_chooser_native_new( + _("Choose Device..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_OPEN, nullptr, nullptr), + &g_object_unref); if (path.is_absolute()) gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog.get()), path.u8string().c_str()); diff --git a/src/gsc_executor_log_window.cpp b/src/gsc_executor_log_window.cpp index 2a110ba..95d43d8 100644 --- a/src/gsc_executor_log_window.cpp +++ b/src/gsc_executor_log_window.cpp @@ -17,10 +17,10 @@ Copyright: #include #include #include // GDK_KEY_Escape +#include #include "applib/app_gtkmm_utils.h" // app_gtkmm_create_tree_view_column #include "applib/app_gtkmm_features.h" -#include "hz/scoped_ptr.h" #include "hz/fs.h" #include "rconfig/rconfig.h" @@ -209,8 +209,9 @@ void GscExecutorLogWindow::on_window_save_current_button_clicked() all_filter->add_pattern("*"); #if GTK_CHECK_VERSION(3, 20, 0) - hz::scoped_ptr dialog(gtk_file_chooser_native_new( - _("Save Data As..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr), g_object_unref); + std::unique_ptr dialog(gtk_file_chooser_native_new( + _("Save Data As..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr), + &g_object_unref); gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog.get()), true); @@ -326,8 +327,9 @@ void GscExecutorLogWindow::on_window_save_all_button_clicked() all_filter->add_pattern("*"); #if GTK_CHECK_VERSION(3, 20, 0) - hz::scoped_ptr dialog(gtk_file_chooser_native_new( - _("Save Data As..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr), g_object_unref); + std::unique_ptr dialog(gtk_file_chooser_native_new( + _("Save Data As..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr), + &g_object_unref); gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog.get()), true); diff --git a/src/gsc_info_window.cpp b/src/gsc_info_window.cpp index a003db4..9324cb7 100644 --- a/src/gsc_info_window.cpp +++ b/src/gsc_info_window.cpp @@ -17,8 +17,8 @@ Copyright: #include // GDK_KEY_Escape #include // better use vector, it's needed by others too #include // std::min, std::max +#include -#include "hz/scoped_ptr.h" #include "hz/string_num.h" // number_to_string #include "hz/string_sprintf.h" // string_sprintf #include "hz/string_algo.h" // string_join @@ -752,8 +752,9 @@ void GscInfoWindow::on_save_info_button_clicked() all_filter->add_pattern("*"); #if GTK_CHECK_VERSION(3, 20, 0) - hz::scoped_ptr dialog(gtk_file_chooser_native_new( - _("Save Data As..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr), g_object_unref); + std::unique_ptr dialog(gtk_file_chooser_native_new( + _("Save Data As..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr), + &g_object_unref); gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog.get()), true); diff --git a/src/gsc_main_window.cpp b/src/gsc_main_window.cpp index 82513fe..0a4571d 100644 --- a/src/gsc_main_window.cpp +++ b/src/gsc_main_window.cpp @@ -15,11 +15,11 @@ Copyright: #include #include #include +#include #include "hz/string_algo.h" // string_split #include "hz/string_num.h" #include "hz/debug.h" -#include "hz/scoped_ptr.h" #include "hz/launch_url.h" #include "hz/fs.h" #include "rconfig/rconfig.h" @@ -1300,8 +1300,9 @@ void GscMainWindow::show_load_virtual_file_chooser() all_filter->add_pattern("*"); #if GTK_CHECK_VERSION(3, 20, 0) - hz::scoped_ptr dialog(gtk_file_chooser_native_new( - _("Load Data From..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_OPEN, nullptr, nullptr), g_object_unref); + std::unique_ptr dialog(gtk_file_chooser_native_new( + _("Load Data From..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_OPEN, nullptr, nullptr), + &g_object_unref); gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog.get()), specific_filter->gobj()); gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog.get()), all_filter->gobj()); @@ -1341,7 +1342,8 @@ void GscMainWindow::show_load_virtual_file_chooser() std::vector files; #if GTK_CHECK_VERSION(3, 20, 0) - hz::scoped_ptr file_slist(gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog.get())), g_slist_free); + std::unique_ptr file_slist(gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog.get())), + &g_slist_free); GSList* iterator = file_slist.get(); while(iterator) { files.push_back(app_ustring_from_gchar((gchar*)iterator->data)); diff --git a/src/gsc_preferences_window.cpp b/src/gsc_preferences_window.cpp index 613d574..b3bcc16 100644 --- a/src/gsc_preferences_window.cpp +++ b/src/gsc_preferences_window.cpp @@ -17,11 +17,11 @@ Copyright: #include #include #include // GDK_KEY_Escape +#include #include "build_config.h" #include "hz/fs_ns.h" #include "hz/string_sprintf.h" -#include "hz/scoped_ptr.h" #include "rconfig/rconfig.h" #include "applib/storage_settings.h" #include "applib/app_gtkmm_utils.h" @@ -533,8 +533,9 @@ void GscPreferencesWindow::on_smartctl_binary_browse_button_clicked() #endif #if GTK_CHECK_VERSION(3, 20, 0) - hz::scoped_ptr dialog(gtk_file_chooser_native_new( - _("Choose Smartctl Binary..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_OPEN, nullptr, nullptr), g_object_unref); + std::unique_ptr dialog(gtk_file_chooser_native_new( + _("Choose Smartctl Binary..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_OPEN, nullptr, nullptr), + &g_object_unref); if (path.is_absolute()) gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog.get()), path.u8string().c_str()); diff --git a/src/gsc_text_window.h b/src/gsc_text_window.h index 4860134..fe860e0 100644 --- a/src/gsc_text_window.h +++ b/src/gsc_text_window.h @@ -15,10 +15,10 @@ Copyright: #include #include #include // GDK_KEY_Escape +#include #include "hz/debug.h" #include "hz/fs.h" -#include "hz/scoped_ptr.h" #include "rconfig/rconfig.h" #include "applib/app_gtkmm_features.h" @@ -149,8 +149,9 @@ class GscTextWindow : public AppBuilderWidget, Ins all_filter->add_pattern("*"); #if GTK_CHECK_VERSION(3, 20, 0) - hz::scoped_ptr dialog(gtk_file_chooser_native_new( - _("Save Data As..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr), g_object_unref); + std::unique_ptr dialog(gtk_file_chooser_native_new( + _("Save Data As..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr), + &g_object_unref); gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog.get()), true); diff --git a/src/gsc_winres.in.rc b/src/gsc_winres.in.rc index 15f3b05..1cdf58f 100644 --- a/src/gsc_winres.in.rc +++ b/src/gsc_winres.in.rc @@ -19,7 +19,7 @@ BEGIN VALUE "FileDescription", "GSmartControl - Hard disk drive and SSD health inspection tool" VALUE "FileVersion", "@CMAKE_PROJECT_VERSION@" VALUE "InternalName", "gsmartcontrol.exe" - VALUE "LegalCopyright", "Copyright (C) 2008 - 2021 Alexander Shaduri" + VALUE "LegalCopyright", "Copyright (C) 2008 - 2021 Alexander Shaduri" VALUE "OriginalFilename", "gsmartcontrol.exe" VALUE "ProductName", "GSmartControl" VALUE "ProductVersion", "@CMAKE_PROJECT_VERSION@" diff --git a/src/hz/CMakeLists.txt b/src/hz/CMakeLists.txt index 69aa614..b23c787 100644 --- a/src/hz/CMakeLists.txt +++ b/src/hz/CMakeLists.txt @@ -20,7 +20,6 @@ target_sources(hz INTERFACE launch_url.h locale_tools.h process_signal.h - scoped_ptr.h stream_cast.h string_algo.h string_num.h diff --git a/src/hz/launch_url.h b/src/hz/launch_url.h index 62300d3..37a8968 100644 --- a/src/hz/launch_url.h +++ b/src/hz/launch_url.h @@ -20,7 +20,7 @@ Copyright: #include // ShellExecuteW() #include "win32_tools.h" // hz::win32_utf8_to_utf16 #else - #include "scoped_ptr.h" + #include #endif @@ -49,13 +49,14 @@ inline std::string launch_url(GtkWindow* window, const std::string& link) #else - hz::scoped_ptr error(0, g_error_free); + GError* error = nullptr; #if GTK_CHECK_VERSION(3, 22, 0) - bool status = gtk_show_uri_on_window(window, link.c_str(), GDK_CURRENT_TIME, &error.get_ref()); + bool status = gtk_show_uri_on_window(window, link.c_str(), GDK_CURRENT_TIME, &error); #else GdkScreen* screen = (window ? gtk_window_get_screen(window) : nullptr); - bool status = (bool)gtk_show_uri(screen, link.c_str(), GDK_CURRENT_TIME, &error.get_ref()); + bool status = (bool)gtk_show_uri(screen, link.c_str(), GDK_CURRENT_TIME, &error); #endif + std::unique_ptr uerror(error, &g_error_free); if (!status) { return std::string("Cannot open URL: ") diff --git a/src/hz/scoped_ptr.h b/src/hz/scoped_ptr.h deleted file mode 100644 index ae7b24d..0000000 --- a/src/hz/scoped_ptr.h +++ /dev/null @@ -1,216 +0,0 @@ -/****************************************************************************** -License: Boost Software License 1.0 -Copyright: - (C) 1998 - 2010 Greg Colvin and Beman Dawes - (C) 2001 - 2010 Peter Dimov - (C) 2008 - 2021 Alexander Shaduri -******************************************************************************/ -/// \file -/// \author Greg Colvin and Beman Dawes -/// \author Peter Dimov -/// \author Alexander Shaduri -/// \ingroup hz -/// \weakgroup hz -/// @{ - -#ifndef HZ_SCOPED_PTR_H -#define HZ_SCOPED_PTR_H - -/** -\file -Scoped non-reference-counting smart pointer with custom cleanup -function. Based on boost::scoped_ptr. - -Original notes and copyright info follow: - -// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. -// Copyright (c) 2001, 2002 Peter Dimov -// -// Distributed under the Boost Software License, Version 1.0. (See -// accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// http://www.boost.org/libs/smart_ptr/scoped_ptr.htm -// -*/ - -#include -#ifndef ASSERT // assert() is undefined if NDEBUG is defined. - #define ASSERT(a) assert(a) -#endif - - - -namespace hz { - - - -namespace internal { - - /// Deleter base - template - struct scoped_ptr_cleaner_base { - /// Virtual destructor - virtual ~scoped_ptr_cleaner_base() = default; - - /// Delete the pointer. The actual method of deletion is chosen by the overrider. - virtual void cleanup(T* p) = 0; - }; - - /// Deleter which calls F(p), where p is the pointer. - template - struct scoped_ptr_cleaner : public scoped_ptr_cleaner_base { - /// Constructor - scoped_ptr_cleaner(F f) : clean_func(f) - { } - - void cleanup(T* p) override - { - clean_func(p); - } - - F clean_func; - }; - -} - - - - -template -class scoped_ptr { // non-copyable - - private: - T* ptr = nullptr; - internal::scoped_ptr_cleaner_base* cleaner = nullptr; - - scoped_ptr (const scoped_ptr&); - scoped_ptr& operator= (const scoped_ptr&); - - typedef scoped_ptr this_type; - typedef T* this_type::*unspecified_bool_type; - - void operator== (const scoped_ptr&) const; - void operator!= (const scoped_ptr&) const; - - - public: - - typedef T element_type; - - /// Constructor with default cleaner - explicit scoped_ptr(T* p = nullptr) // never throws - : ptr(p), cleaner(nullptr) - { } - - /// Constructor. Takes ownership of \c p. During destruction - /// \c cleanup_func will be called with p. - template - explicit scoped_ptr(T* p, F cleanup_func) - : ptr(p), cleaner(new internal::scoped_ptr_cleaner(cleanup_func)) - { } - - /// Destructor - deletes the underlying pointer - ~scoped_ptr() // never throws - { - if (cleaner) { - if (ptr) - cleaner->cleanup(ptr); - delete cleaner; - } else { -#ifdef __GNUC__ - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wdelete-incomplete" -#endif - delete ptr; -#ifdef __GNUC__ - #pragma GCC diagnostic pop -#endif - } - } - - /// Delete the old pointer and switch to the new one - void reset(T* p = 0) // never throws - { - ASSERT(p == 0 || p != ptr); // catch self-reset errors - this_type(p).swap(*this); - } - - /// Dereference operator - T& operator*() const // never throws - { - ASSERT(ptr); - return *ptr; - } - - /// Arrow operator - T* operator->() const // never throws - { - ASSERT(ptr); - return ptr; - } - - /// Get the underlying pointer - T* get() const // never throws - { - return ptr; - } - - /// Get a reference to the stored pointer. - /// Useful for passing to functions who expect T**. - T*& get_ref() // never throws - { - return ptr; - } - - - // Bool-like conversion - operator unspecified_bool_type () const - { - return ptr == 0 ? 0 : &this_type::ptr; - } - - /// Null-check - bool operator! () const // never throws - { - return ptr == 0; - } - - - /// Swap with another pointer - void swap(scoped_ptr& b) // never throws - { - T * tmp = b.ptr; - b.ptr = ptr; - ptr = tmp; - } - -}; - - - -/// Swap two pointers -template inline -void swap(scoped_ptr& a, scoped_ptr& b) // never throws -{ - a.swap(b); -} - - -/// get_pointer(p) is a generic way to say p.get() -template inline -T* get_pointer(const scoped_ptr& p) -{ - return p.get(); -} - - - -} // ns - - - - -#endif // hg - -/// @}