4 * Copyright (C) 2010 Stefan Walter
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 #include "gcr-comparable.h"
29 * SECTION:gcr-comparable
30 * @title: GcrComparable
31 * @short_description: Interface for comparing objects
33 * The #GcrComparable interface is implemented by objects when they should be
34 * comparable against one another.
40 * The #GcrComparable interface is implemented by comparable objects.
46 * The interface to implement for #GcrComparable
49 /* ---------------------------------------------------------------------------------
54 gcr_comparable_base_init (gpointer g_class)
56 static volatile gsize initialized = 0;
58 if (g_once_init_enter (&initialized)) {
59 /* Add properties and signals to the interface */
60 g_once_init_leave (&initialized, 1);
65 gcr_comparable_get_type (void)
67 static GType type = 0;
69 static const GTypeInfo info = {
70 sizeof (GcrComparableIface),
71 gcr_comparable_base_init, /* base init */
72 NULL, /* base finalize */
73 NULL, /* class_init */
74 NULL, /* class finalize */
75 NULL, /* class data */
78 NULL, /* instance init */
80 type = g_type_register_static (G_TYPE_INTERFACE, "GcrComparableIface", &info, 0);
81 g_type_interface_add_prerequisite (type, G_TYPE_OBJECT);
88 /* -----------------------------------------------------------------------------
93 * gcr_comparable_compare:
94 * @self: The comparable object
95 * @other: Another comparable object
97 * Compare whether two objects represent the same thing. The return value can
98 * also be used to sort the objects.
100 * Returns: Zero if the two objects represent the same thing, non-zero if not.
103 gcr_comparable_compare (GcrComparable *self, GcrComparable *other)
105 g_return_val_if_fail (GCR_IS_COMPARABLE (self), -1);
106 g_return_val_if_fail (GCR_COMPARABLE_GET_INTERFACE (self)->compare, -1);
107 g_return_val_if_fail (G_IS_OBJECT (self), -1);
108 return GCR_COMPARABLE_GET_INTERFACE (self)->compare (self, other);
112 * gcr_comparable_memcmp: (skip)
113 * @mem1: First block of memory
114 * @size1: Length of first block
115 * @mem2: Second lock of memory
116 * @size2: Length of second block
118 * Compare two blocks of memory. The return value can be used to sort
119 * the blocks of memory.
121 * Returns: Zero if the blocks are identical, non-zero if not.
124 gcr_comparable_memcmp (gconstpointer mem1, gsize size1,
125 gconstpointer mem2, gsize size2)
129 if (mem1 == mem2 && size1 == size2)
137 result = memcmp (mem1, mem2, MIN (size1, size2));