Skip to content

Commit

Permalink
Add autoUnref function to permit easy RAII on g_objects
Browse files Browse the repository at this point in the history
instead of GObject* object = g_whatever_function(..), do
auto object = autoUnref(g_whatever_function(..)) and object will be
unrefed at scope end.
  • Loading branch information
Maxime Coste committed Feb 25, 2011
1 parent 6a2e208 commit 773c0a7
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/util.hh
Expand Up @@ -121,6 +121,24 @@ template<typename Function>
return OnScopeEnd<Function>(function);
}

template<typename T>
class AutoUnref
{
T* _g_object;

public:
AutoUnref(T* g_object) : _g_object(g_object) {}
~AutoUnref() { if (_g_object) g_object_unref(_g_object); }

operator T*() { return _g_object; }
};

template<typename T>
AutoUnref<T> autoUnref(T* g_object)
{
return AutoUnref<T>(g_object);
}

#endif

// vim: fdm=syntax fo=croql et sw=4 sts=4 ts=8
Expand Down

0 comments on commit 773c0a7

Please sign in to comment.