/* Chrysalide - Outil d'analyse de fichiers binaires
 * comparable.c - opérations de comparaison d'objets
 *
 * Copyright (C) 2022-2025 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chrysalide is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "comparable.h"


#include "comparable-int.h"
#include "../common/sort.h"



/* Procède à l'initialisation de l'interface de comparaison. */
static void g_comparable_object_default_init(GComparableObjectInterface *);



/* Détermine le type d'une interface pour un objet comparable. */
G_DEFINE_INTERFACE(GComparableObject, g_comparable_object, G_TYPE_OBJECT)


/******************************************************************************
*                                                                             *
*  Paramètres  : iface = interface GLib à initialiser.                        *
*                                                                             *
*  Description : Procède à l'initialisation de l'interface de comparaison.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_comparable_object_default_init(GComparableObjectInterface *iface)
{
    iface->compare = NULL;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : object = premier objet à consulter pour une comparaison.     *
*                other  = second objet à consulter pour une comparaison.      *
*                                                                             *
*  Description : Réalise une comparaison étendue entre objets.                *
*                                                                             *
*  Retour      : Bilan de la comparaison.                                     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

int g_comparable_object_compare(const GComparableObject *object, const GComparableObject *other)
{
    int result;                             /* Bilan à retourner           */
    GType type_a;                           /* Type de l'object A          */
    GType type_b;                           /* Type de l'object B          */
    GComparableObjectInterface *iface;      /* Interface utilisée          */

    type_a = G_OBJECT_TYPE(G_OBJECT(object));
    type_b = G_OBJECT_TYPE(G_OBJECT(other));

    assert(sizeof(GType) <= sizeof(unsigned long));

    result = sort_unsigned_long(type_a, type_b);

    if (result == 0)
    {
        iface = G_COMPARABLE_OBJECT_GET_IFACE(object);

        result = iface->compare(object, other);

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : object = premier objet à consulter pour une comparaison.     *
*                other  = second objet à consulter pour une comparaison.      *
*                                                                             *
*  Description : Détermine si deux objets sont fonctionnellement identiques.  *
*                                                                             *
*  Retour      : Bilan de la comparaison.                                     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

gboolean g_comparable_object_is_equal(const GComparableObject *object, const GComparableObject *other)
{
    gboolean result;                        /* Bilan à renvoyer            */
    int ret;                                /* Bilan d'une comparaison     */

    ret = g_comparable_object_compare(object, other);

    result = (ret == 0);

    return result;

}