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

Side by Side Diff: ui/base/glib/scoped_gobject.h

Issue 2803873003: Linux native notifications: Support closing and updating notifications (Closed)
Patch Set: Address peter@'s comments 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef UI_BASE_GLIB_SCOPED_GOBJECT_H_
6 #define UI_BASE_GLIB_SCOPED_GOBJECT_H_
7
8 #include "base/logging.h"
9
10 // Similar in spirit to a std::unique_ptr.
11 template <typename T>
12 class ScopedGObject {
13 public:
14 ScopedGObject() : obj_(nullptr) {}
15
16 explicit ScopedGObject(T* obj) : obj_(obj) {
17 // Remove the floating reference from |obj_| if it has one.
sky 2017/04/06 20:44:46 Should reset() get this logic too? If not, why?
Tom (Use chromium acct) 2017/04/06 22:28:57 Yes it should! Thanks for catching this!
18 if (g_object_is_floating(obj_))
19 g_object_ref_sink(obj_);
20 DCHECK(G_OBJECT(obj_)->ref_count == 1);
21 }
22
23 ScopedGObject(const ScopedGObject<T>& other) = delete;
24
25 ScopedGObject(ScopedGObject<T>&& other) : obj_(other.obj_) {
26 other.obj_ = nullptr;
27 }
28
29 ~ScopedGObject() { reset(); }
30
31 ScopedGObject<T>& operator=(const ScopedGObject<T>& other) = delete;
32
33 ScopedGObject<T>& operator=(ScopedGObject<T>&& other) {
34 g_object_unref(obj_);
sky 2017/04/06 20:44:46 reset() ?
Tom (Use chromium acct) 2017/04/06 22:28:57 Done.
35 obj_ = other.obj_;
36 other.obj_ = nullptr;
37 return *this;
38 }
39
40 operator T*() { return obj_; }
41
42 void reset(T* obj = nullptr) {
43 if (obj_)
44 Unref();
sky 2017/04/06 20:44:46 It's mildly confusing that you have both reset and
Tom (Use chromium acct) 2017/04/06 22:28:57 Can't unfortunately. We require a template overri
sky 2017/04/06 23:30:17 Subtle! Please add a comment about that as it's ea
45 obj_ = obj;
46 }
47
48 private:
49 void Unref() { g_object_unref(obj_); }
50
51 T* obj_;
52 };
53
54 #endif // UI_BASE_GLIB_SCOPED_GOBJECT_H_
OLDNEW
« chrome/browser/notifications/notification_platform_bridge_linux.cc ('K') | « ui/base/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698