Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(532)

Unified Diff: ui/base/glib/scoped_gobject.h

Issue 2803873003: Linux native notifications: Support closing and updating notifications (Closed)
Patch Set: glib_signals.h -> glib_signal.h to fix gn gen --check Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/base/BUILD.gn ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/base/glib/scoped_gobject.h
diff --git a/ui/base/glib/scoped_gobject.h b/ui/base/glib/scoped_gobject.h
new file mode 100644
index 0000000000000000000000000000000000000000..abd81b86ccda222f40c032df43a5b9db8a7796ff
--- /dev/null
+++ b/ui/base/glib/scoped_gobject.h
@@ -0,0 +1,58 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef UI_BASE_GLIB_SCOPED_GOBJECT_H_
+#define UI_BASE_GLIB_SCOPED_GOBJECT_H_
+
+// Similar in spirit to a std::unique_ptr.
+template <typename T>
+class ScopedGObject {
+ public:
+ ScopedGObject() : obj_(nullptr) {}
+
+ explicit ScopedGObject(T* obj) : obj_(obj) { Ref(); }
+
+ ScopedGObject(const ScopedGObject<T>& other) = delete;
+
+ ScopedGObject(ScopedGObject<T>&& other) : obj_(other.obj_) {
+ other.obj_ = nullptr;
+ }
+
+ ~ScopedGObject() { reset(); }
+
+ ScopedGObject<T>& operator=(const ScopedGObject<T>& other) = delete;
+
+ ScopedGObject<T>& operator=(ScopedGObject<T>&& other) {
+ reset();
+ obj_ = other.obj_;
+ other.obj_ = nullptr;
+ return *this;
+ }
+
+ operator T*() { return obj_; }
+
+ void reset(T* obj = nullptr) {
+ Unref();
+ obj_ = obj;
+ Ref();
+ }
+
+ private:
+ void Ref() {
+ // Remove the floating reference from |obj_| if it has one.
+ if (obj_ && g_object_is_floating(obj_))
+ g_object_ref_sink(obj_);
+ }
+
+ // This function is necessary so that libgtkui can overload it in
+ // the case of T = GtkStyleContext.
+ void Unref() {
+ if (obj_)
+ g_object_unref(obj_);
+ }
+
+ T* obj_;
+};
+
+#endif // UI_BASE_GLIB_SCOPED_GOBJECT_H_
« no previous file with comments | « ui/base/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698