Blame libglnx/glnx-backport-autoptr.h

rpm-build c487f7
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
rpm-build c487f7
 *
rpm-build c487f7
 * Copyright (C) 2015 Colin Walters <walters@verbum.org>
rpm-build c487f7
 * 
rpm-build c487f7
 * GLIB - Library of useful routines for C programming
rpm-build c487f7
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
rpm-build c487f7
 *
rpm-build c487f7
 * This library is free software; you can redistribute it and/or
rpm-build c487f7
 * modify it under the terms of the GNU Lesser General Public
rpm-build c487f7
 * License as published by the Free Software Foundation; either
rpm-build c487f7
 * version 2 of the License, or (at your option) any later version.
rpm-build c487f7
 *
rpm-build c487f7
 * This library is distributed in the hope that it will be useful,
rpm-build c487f7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build c487f7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build c487f7
 * Lesser General Public License for more details.
rpm-build c487f7
 *
rpm-build c487f7
 * You should have received a copy of the GNU Lesser General Public
rpm-build c487f7
 * License along with this library; if not, write to the
rpm-build c487f7
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rpm-build c487f7
 * Boston, MA 02111-1307, USA.
rpm-build c487f7
 */
rpm-build c487f7
rpm-build c487f7
#pragma once
rpm-build c487f7
rpm-build c487f7
#include <gio/gio.h>
rpm-build c487f7
rpm-build c487f7
G_BEGIN_DECLS
rpm-build c487f7
rpm-build c487f7
#if !GLIB_CHECK_VERSION(2, 43, 4)
rpm-build c487f7
rpm-build c487f7
#define _GLIB_AUTOPTR_FUNC_NAME(TypeName) glib_autoptr_cleanup_##TypeName
rpm-build c487f7
#define _GLIB_AUTOPTR_TYPENAME(TypeName)  TypeName##_autoptr
rpm-build c487f7
#define _GLIB_AUTO_FUNC_NAME(TypeName)    glib_auto_cleanup_##TypeName
rpm-build c487f7
#define _GLIB_CLEANUP(func)               __attribute__((cleanup(func)))
rpm-build c487f7
#define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName) \
rpm-build c487f7
  typedef ModuleObjName *_GLIB_AUTOPTR_TYPENAME(ModuleObjName);                                          \
rpm-build c487f7
  static inline void _GLIB_AUTOPTR_FUNC_NAME(ModuleObjName) (ModuleObjName **_ptr) {                     \
rpm-build c487f7
    _GLIB_AUTOPTR_FUNC_NAME(ParentName) ((ParentName **) _ptr); }                                        \
rpm-build c487f7
rpm-build c487f7
rpm-build c487f7
/* these macros are API */
rpm-build c487f7
#define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) \
rpm-build c487f7
  typedef TypeName *_GLIB_AUTOPTR_TYPENAME(TypeName);                                                           \
rpm-build c487f7
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS                                                                              \
rpm-build c487f7
  static inline void _GLIB_AUTOPTR_FUNC_NAME(TypeName) (TypeName **_ptr) { if (*_ptr) (func) (*_ptr); }         \
rpm-build c487f7
  G_GNUC_END_IGNORE_DEPRECATIONS
rpm-build c487f7
#define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func) \
rpm-build c487f7
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS                                                                              \
rpm-build c487f7
  static inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { (func) (_ptr); }                         \
rpm-build c487f7
  G_GNUC_END_IGNORE_DEPRECATIONS
rpm-build c487f7
#define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none) \
rpm-build c487f7
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS                                                                              \
rpm-build c487f7
  static inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { if (*_ptr != none) (func) (*_ptr); }     \
rpm-build c487f7
  G_GNUC_END_IGNORE_DEPRECATIONS
rpm-build c487f7
#define g_autoptr(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_TYPENAME(TypeName)
rpm-build c487f7
#define g_auto(TypeName) _GLIB_CLEANUP(_GLIB_AUTO_FUNC_NAME(TypeName)) TypeName
rpm-build c487f7
#define g_autofree _GLIB_CLEANUP(g_autoptr_cleanup_generic_gfree)
rpm-build c487f7
rpm-build c487f7
/**
rpm-build c487f7
 * g_steal_pointer:
rpm-build c487f7
 * @pp: a pointer to a pointer
rpm-build c487f7
 *
rpm-build c487f7
 * Sets @pp to %NULL, returning the value that was there before.
rpm-build c487f7
 *
rpm-build c487f7
 * Conceptually, this transfers the ownership of the pointer from the
rpm-build c487f7
 * referenced variable to the "caller" of the macro (ie: "steals" the
rpm-build c487f7
 * reference).
rpm-build c487f7
 *
rpm-build c487f7
 * The return value will be properly typed, according to the type of
rpm-build c487f7
 * @pp.
rpm-build c487f7
 *
rpm-build c487f7
 * This can be very useful when combined with g_autoptr() to prevent the
rpm-build c487f7
 * return value of a function from being automatically freed.  Consider
rpm-build c487f7
 * the following example (which only works on GCC and clang):
rpm-build c487f7
 *
rpm-build c487f7
 * |[
rpm-build c487f7
 * GObject *
rpm-build c487f7
 * create_object (void)
rpm-build c487f7
 * {
rpm-build c487f7
 *   g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
rpm-build c487f7
 *
rpm-build c487f7
 *   if (early_error_case)
rpm-build c487f7
 *     return NULL;
rpm-build c487f7
 *
rpm-build c487f7
 *   return g_steal_pointer (&obj);
rpm-build c487f7
 * }
rpm-build c487f7
 * ]|
rpm-build c487f7
 *
rpm-build c487f7
 * It can also be used in similar ways for 'out' parameters and is
rpm-build c487f7
 * particularly useful for dealing with optional out parameters:
rpm-build c487f7
 *
rpm-build c487f7
 * |[
rpm-build c487f7
 * gboolean
rpm-build c487f7
 * get_object (GObject **obj_out)
rpm-build c487f7
 * {
rpm-build c487f7
 *   g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
rpm-build c487f7
 *
rpm-build c487f7
 *   if (early_error_case)
rpm-build c487f7
 *     return FALSE;
rpm-build c487f7
 *
rpm-build c487f7
 *   if (obj_out)
rpm-build c487f7
 *     *obj_out = g_steal_pointer (&obj);
rpm-build c487f7
 *
rpm-build c487f7
 *   return TRUE;
rpm-build c487f7
 * }
rpm-build c487f7
 * ]|
rpm-build c487f7
 *
rpm-build c487f7
 * In the above example, the object will be automatically freed in the
rpm-build c487f7
 * early error case and also in the case that %NULL was given for
rpm-build c487f7
 * @obj_out.
rpm-build c487f7
 *
rpm-build c487f7
 * Since: 2.44
rpm-build c487f7
 */
rpm-build c487f7
static inline gpointer
rpm-build c487f7
(g_steal_pointer) (gpointer pp)
rpm-build c487f7
{
rpm-build c487f7
  gpointer *ptr = (gpointer *) pp;
rpm-build c487f7
  gpointer ref;
rpm-build c487f7
rpm-build c487f7
  ref = *ptr;
rpm-build c487f7
  *ptr = NULL;
rpm-build c487f7
rpm-build c487f7
  return ref;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
/* type safety */
rpm-build c487f7
#define g_steal_pointer(pp) \
rpm-build c487f7
  (0 ? (*(pp)) : (g_steal_pointer) (pp))
rpm-build c487f7
rpm-build c487f7
#endif /* !GLIB_CHECK_VERSION(2, 43, 3) */
rpm-build c487f7
rpm-build c487f7
G_END_DECLS