------------------------------------------------------------------------------
--                                                                          --
--                           POLYORB COMPONENTS                             --
--                                                                          --
--                    C O R B A . F I X E D _ P O I N T                     --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--         Copyright (C) 2001-2006, Free Software Foundation, Inc.          --
--                                                                          --
-- PolyORB is free software; you  can  redistribute  it and/or modify it    --
-- under terms of the  GNU General Public License as published by the  Free --
-- Software Foundation;  either version 2,  or (at your option)  any  later --
-- version. PolyORB is distributed  in the hope that it will be  useful,    --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY 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 PolyORB; see file COPYING. If    --
-- not, write to the Free Software Foundation, 51 Franklin Street, Fifth    --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
--
--
--
--
--
--
--
--                  PolyORB is maintained by AdaCore                        --
--                     (email: sales@adacore.com)                           --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Streams;

with PolyORB.Log;

with PolyORB.Representations.CDR.Common;

package body CORBA.Fixed_Point is

   use PolyORB.Log;

   -----------
   -- Debug --
   -----------

   package L is new PolyORB.Log.Facility_Log ("corba.fixed_point");
   procedure O (Message : Standard.String; Level : Log_Level := Debug)
     renames L.Output;
   function C (Level : Log_Level := Debug) return Boolean
     renames L.Enabled;
   pragma Unreferenced (C); --  For conditional pragma Debug

   ---------------------
   -- this is to help --
   ---------------------

   package CDR_Fixed_F is
      new PolyORB.Representations.CDR.Common.Fixed_Point (F);

   ------------
   -- To_Any --
   ------------

   function To_Any (Item : F) return CORBA.Any is
      Tco : CORBA.TypeCode.Object;

   begin
      CORBA.TypeCode.Internals.Set_Kind (Tco, PolyORB.Any.Tk_Fixed);
      CORBA.TypeCode.Internals.Add_Parameter
        (Tco, CORBA.To_Any (CORBA.Unsigned_Short (F'Digits)));
      CORBA.TypeCode.Internals.Add_Parameter
        (Tco, CORBA.To_Any (CORBA.Short (F'Scale)));

      declare
         Result : Any := CORBA.Internals.Get_Empty_Any_Aggregate (Tco);
         Octets : constant Ada.Streams.Stream_Element_Array
           := CDR_Fixed_F.Fixed_To_Octets (Item);

      begin
         for I in Octets'Range loop
            CORBA.Internals.Add_Aggregate_Element
              (Result,
               CORBA.To_Any (CORBA.Octet (Octets (I))));
         end loop;

         return Result;
      end;
   end To_Any;

   --------------
   -- From_Any --
   --------------

   function From_Any (Item : Any) return F is
      use type PolyORB.Any.TCKind;

   begin
      pragma Debug (O ("From_Any (Fixed) : enter"));
      if TypeCode.Kind (Internals.Get_Unwound_Type (Item))
        /= PolyORB.Any.Tk_Fixed
      then
         pragma Debug
           (O ("From_Any (Fixed) : Bad_TypeCode, type is " &
               CORBA.TCKind'Image
               (TypeCode.Kind (Internals.Get_Unwound_Type (Item)))));
         raise Bad_TypeCode;
      end if;

      declare
         use Ada.Streams;

         Nb : constant CORBA.Unsigned_Long :=
           CORBA.Internals.Get_Aggregate_Count (Item);
         Octets : Stream_Element_Array (1 .. Stream_Element_Offset (Nb)) :=
           (others => 0);
         Element : CORBA.Any;
      begin
         for I in Octets'Range loop
            pragma Debug (O ("From_Any (Fixed) : yet another octet"));
            Element :=
              CORBA.Internals.Get_Aggregate_Element
              (Item,
               CORBA.TC_Octet,
               CORBA.Unsigned_Long (I - 1));
            Octets (I) := Stream_Element
              (CORBA.Octet'(CORBA.From_Any (Element)));
         end loop;
         pragma Debug (O ("From_Any (Fixed) : return"));
         return CDR_Fixed_F.Octets_To_Fixed (Octets);

      exception when CORBA.Marshal =>
         pragma Debug (O ("From_Any (Fixed) : exception catched" &
                          "while returning"));
         raise CORBA.Bad_TypeCode;
      end;
   end From_Any;

end CORBA.Fixed_Point;
