------------------------------------------------------------------------------
--                                                                          --
--                An Ada2005 binding to the Qt C++ framework                --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2008, 2009 Vadim Godunko <vgodunko@gmail.com>                --
--                                                                          --
-- QtAda is free software;  you can  redistribute it and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
-- sion. QtAda is distributed in the hope that it will be useful, but WITH- --
-- OUT 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 distributed with QtAda;  see file COPYING.  If not, write --
-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
-- Boston, MA 02110-1301, USA.                                              --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable  to  be --
-- covered  by the  GNU  General  Public  License.  This exception does not --
-- however invalidate  any other reasons why  the executable file  might be --
-- covered by the  GNU Public License.                                      --
--                                                                          --
------------------------------------------------------------------------------
--  $Revision: 3239 $ $Date: 2009-04-10 15:48:51 +0400 (Fri, 10 Apr 2009) $
------------------------------------------------------------------------------
with Qt_Ada.Internals.Object_Registry;

package body Qt_Ada.Generic_Object_Pointers is

   use type Qt_Ada.Internals.Objects.QObject_Access;

   procedure changeGuard
    (Guarded : not null access Qt_Ada.Internals.Objects.QObject_Access;
     Object  : Qt_Ada.Internals.Objects.QObject_Access);
   pragma Import (C, changeGuard, "__QtAda_QMetaObject_changeGuard");

   procedure removeGuard
    (Guarded : not null access Qt_Ada.Internals.Objects.QObject_Access);
   pragma Import (C, removeGuard, "__QtAda_QMetaObject_removeGuard");

   procedure addGuard
    (Guarded : not null access Qt_Ada.Internals.Objects.QObject_Access);
   pragma Import (C, addGuard, "__QtAda_QMetaObject_addGuard");

   ---------
   -- "=" --
   ---------

   overriding function "="
    (Left  : Q_Pointer;
     Right : Q_Pointer)
       return Boolean
   is
   begin
      return Left.Reference = Right.Reference;
   end "=";

   ---------
   -- "=" --
   ---------

   function "=" (Left : Q_Pointer; Right : Object_Access)
     return Boolean
   is
   begin
      if Left.Reference = null and then Right = null then
         return True;

      elsif Left.Reference /= null and then Right /= null then
         return Left.Reference = Right.QObject_View;

      else
         return False;
      end if;
   end "=";

   ---------
   -- "=" --
   ---------

   function "=" (Left : Object_Access; Right : Q_Pointer)
     return Boolean
   is
   begin
      if Left = null and Right.Reference = null then
         return True;

      elsif Left /= null and then Right.Reference /= null then
         return Left.QObject_View = Right.Reference;

      else
         return False;
      end if;
   end "=";

   ------------
   -- Adjust --
   ------------

   overriding
   procedure Adjust (Self : in out Q_Pointer) is
   begin
      addGuard (Self.Reference'Access);
   end Adjust;

   --------------
   -- Finalize --
   --------------

   overriding
   procedure Finalize (Self : in out Q_Pointer) is
   begin
      removeGuard (Self.Reference'Access);
   end Finalize;

   ---------
   -- Get --
   ---------

   function Get (Self : Q_Pointer) return Object_Access is
   begin
      if Self.Reference = null then
         return null;

      else
         return
           Object_Access
            (Qt_Ada.Internals.Object_Registry.To_Q_Object (Self.Reference));
      end if;
   end Get;

   -------------
   -- Is_Null --
   -------------

   function Is_Null (Self : Q_Pointer) return Boolean is
   begin
      return Self.Reference = null;
   end Is_Null;

   ---------
   -- Set --
   ---------

   procedure Set (Self : in out Q_Pointer; To : Object_Access) is
      use type Qt_Ada.Internals.Objects.QObject_Access;

      Aux : Qt_Ada.Internals.Objects.QObject_Access;

   begin
      if To /= null then
         Aux := Qt_Ada.Internals.Objects.QObject_Access (To.QObject_View);
      end if;

      if Self.Reference /= Aux then
         changeGuard (Self.Reference'Access, Aux);
      end if;
   end Set;

end Qt_Ada.Generic_Object_Pointers;
