Use unique_ptr for GtkSelectionData

This commit is contained in:
Ilya Fedin 2021-07-08 20:10:13 +04:00 committed by John Preston
parent 75090dedaa
commit 2c6e4eed19

View file

@ -68,6 +68,16 @@ constexpr auto kIntrospectionXML = R"INTROSPECTION(<node>
</interface> </interface>
</node>)INTROSPECTION"_cs; </node>)INTROSPECTION"_cs;
struct GtkSelectionDataDeleter {
void operator()(GtkSelectionData *gsel) {
if (gsel) {
gtk_selection_data_free(gsel);
}
}
};
using GtkSelectionDataPointer = std::unique_ptr<GtkSelectionData, GtkSelectionDataDeleter>;
Glib::ustring ServiceName; Glib::ustring ServiceName;
bool GetImageFromClipboardSupported() { bool GetImageFromClipboardSupported() {
@ -96,11 +106,11 @@ std::vector<uchar> GetImageFromClipboard() {
gdk_atom_intern("image/bmp", true), gdk_atom_intern("image/bmp", true),
}; };
const auto gsel = [&]() -> GtkSelectionData* { const auto gsel = [&]() -> GtkSelectionDataPointer {
for (const auto &format : supportedFormats) { for (const auto &format : supportedFormats) {
if (const auto result = gtk_clipboard_wait_for_contents( if (auto result = GtkSelectionDataPointer(
clipboard, gtk_clipboard_wait_for_contents(clipboard, format))
format); gtk_selection_data_get_length(result) > 0) { ; gtk_selection_data_get_length(result.get()) > 0) {
return result; return result;
} }
} }
@ -111,12 +121,8 @@ std::vector<uchar> GetImageFromClipboard() {
return {}; return {};
} }
const auto guard = gsl::finally([&] { const auto data = gtk_selection_data_get_data(gsel.get());
gtk_selection_data_free(gsel); const auto length = gtk_selection_data_get_length(gsel.get());
});
const auto data = gtk_selection_data_get_data(gsel);
const auto length = gtk_selection_data_get_length(gsel);
return std::vector<uchar>(data, data + length); return std::vector<uchar>(data, data + length);
} }