1 // Copyright (c) 2012 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_GFX_SCOPED_GOBJECT_H_ 6 #define UI_GFX_SCOPED_GOBJECT_H_ 7 8 #include <glib-object.h> 9 10 #include "base/memory/scoped_ptr.h" 11 12 namespace ui { 13 14 // It's not legal C++ to have a templatized typedefs, so we wrap it in a 15 // struct. When using this, you need to include ::Type. E.g., 16 // ScopedGObject<GdkPixbufLoader>::Type loader(gdk_pixbuf_loader_new()); 17 template<class T> 18 struct ScopedGObject { 19 // A helper class that will g_object_unref |p| when it goes out of scope. 20 // This never adds a ref, it only unrefs. 21 template<class U> 22 struct GObjectUnrefer { 23 void operator()(U* ptr) const { 24 if (ptr) 25 g_object_unref(ptr); 26 } 27 }; 28 29 typedef scoped_ptr_malloc<T, GObjectUnrefer<T> > Type; 30 }; 31 32 } // namespace ui 33 34 #endif // UI_GFX_SCOPED_GOBJECT_H_ 35