Blame gdata/gdata-comparable.c

Packit 4b6dd7
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
Packit 4b6dd7
/*
Packit 4b6dd7
 * GData Client
Packit 4b6dd7
 * Copyright (C) Philip Withnall 2010 <philip@tecnocode.co.uk>
Packit 4b6dd7
 *
Packit 4b6dd7
 * GData Client is free software; you can redistribute it and/or
Packit 4b6dd7
 * modify it under the terms of the GNU Lesser General Public
Packit 4b6dd7
 * License as published by the Free Software Foundation; either
Packit 4b6dd7
 * version 2.1 of the License, or (at your option) any later version.
Packit 4b6dd7
 *
Packit 4b6dd7
 * GData Client is distributed in the hope that it will be useful,
Packit 4b6dd7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4b6dd7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 4b6dd7
 * Lesser General Public License for more details.
Packit 4b6dd7
 *
Packit 4b6dd7
 * You should have received a copy of the GNU Lesser General Public
Packit 4b6dd7
 * License along with GData Client.  If not, see <http://www.gnu.org/licenses/>.
Packit 4b6dd7
 */
Packit 4b6dd7
Packit 4b6dd7
/**
Packit 4b6dd7
 * SECTION:gdata-comparable
Packit 4b6dd7
 * @short_description: GData comparable interface
Packit 4b6dd7
 * @stability: Stable
Packit 4b6dd7
 * @include: gdata/gdata-comparable.h
Packit 4b6dd7
 *
Packit 4b6dd7
 * #GDataComparable is an interface which can be implemented by any object which needs to be compared to another object of the same type or of a
Packit 4b6dd7
 * derived type.
Packit 4b6dd7
 *
Packit 4b6dd7
 * When implementing the interface, classes must implement the <function>compare_with</function> function, and the implementation must be
Packit 4b6dd7
 * <ulink type="http" url="http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#index-g_t_0040code_007bpure_007d-function-attribute-2413">pure
Packit 4b6dd7
 * </ulink>.
Packit 4b6dd7
 *
Packit 4b6dd7
 * Since: 0.7.0
Packit 4b6dd7
 */
Packit 4b6dd7
Packit 4b6dd7
#include <glib.h>
Packit 4b6dd7
#include <glib-object.h>
Packit 4b6dd7
Packit 4b6dd7
#include "gdata-comparable.h"
Packit 4b6dd7
Packit 4b6dd7
GType
Packit 4b6dd7
gdata_comparable_get_type (void)
Packit 4b6dd7
{
Packit 4b6dd7
	static GType comparable_type = 0;
Packit 4b6dd7
Packit 4b6dd7
	if (!comparable_type) {
Packit 4b6dd7
		comparable_type = g_type_register_static_simple (G_TYPE_INTERFACE, "GDataComparable",
Packit 4b6dd7
		                                                 sizeof (GDataComparableIface),
Packit 4b6dd7
		                                                 NULL, 0, NULL, 0);
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	return comparable_type;
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/**
Packit 4b6dd7
 * gdata_comparable_compare:
Packit 4b6dd7
 * @self: (allow-none): a #GDataComparable, or %NULL
Packit 4b6dd7
 * @other: (allow-none): another #GDataComparable of the same type, or %NULL
Packit 4b6dd7
 *
Packit 4b6dd7
 * Compares the two objects, returning -1 if @self is "less than" @other by some metric, 0
Packit 4b6dd7
 * if they're equal, or 1 if @self is "greater than" @other.
Packit 4b6dd7
 *
Packit 4b6dd7
 * %NULL values are handled gracefully, with 0 returned if both @self and @other are %NULL,
Packit 4b6dd7
 * -1 if @self is %NULL and 1 if @other is %NULL.
Packit 4b6dd7
 *
Packit 4b6dd7
 * The @other object must be of the same type as @self, or of a type derived from @self's type.
Packit 4b6dd7
 *
Packit 4b6dd7
 * Return value: %TRUE on success, %FALSE otherwise
Packit 4b6dd7
 *
Packit 4b6dd7
 * Since: 0.7.0
Packit 4b6dd7
 */
Packit 4b6dd7
gint
Packit 4b6dd7
gdata_comparable_compare (GDataComparable *self, GDataComparable *other)
Packit 4b6dd7
{
Packit 4b6dd7
	GDataComparableIface *iface;
Packit 4b6dd7
Packit 4b6dd7
	g_return_val_if_fail (self == NULL || GDATA_IS_COMPARABLE (self), 0);
Packit 4b6dd7
	g_return_val_if_fail (other == NULL || GDATA_IS_COMPARABLE (other), 0);
Packit 4b6dd7
	g_return_val_if_fail (self == NULL || other == NULL || g_type_is_a (G_OBJECT_TYPE (other), G_OBJECT_TYPE (self)), 0);
Packit 4b6dd7
Packit 4b6dd7
	/* Deal with NULL values */
Packit 4b6dd7
	if (self == NULL && other != NULL)
Packit 4b6dd7
		return -1;
Packit 4b6dd7
	else if (self != NULL && other == NULL)
Packit 4b6dd7
		return 1;
Packit 4b6dd7
Packit 4b6dd7
	if (self == other)
Packit 4b6dd7
		return 0;
Packit 4b6dd7
Packit 4b6dd7
	/* Use the comparator method for non-NULL values */
Packit 4b6dd7
	iface = GDATA_COMPARABLE_GET_IFACE (self);
Packit 4b6dd7
	g_assert (iface->compare_with != NULL);
Packit 4b6dd7
Packit 4b6dd7
	return iface->compare_with (self, other);
Packit 4b6dd7
}