protobuf_1.0.0_43962766/source/runtime/generated/google-protobuf-any.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
with Ada.Unchecked_Deallocation;
with PB_Support.IO;
with PB_Support.Internal;

package body Google.Protobuf.Any is

   function Length (Self : Any_Vector) return Natural is
   begin
      return Self.Length;
   end Length;

   procedure Clear (Self : in out Any_Vector) is
   begin
      Self.Length := 0;
   end Clear;

   procedure Free is new Ada.Unchecked_Deallocation
     (Any_Array, Any_Array_Access);

   procedure Append (Self : in out Any_Vector; V    : Any) is
      Init_Length : constant Positive := Positive'Max (1, 256 / Any'Size);
   begin
      if Self.Length = 0 then
         Self.Data :=  new Any_Array (1 .. Init_Length);

      elsif Self.Length = Self.Data'Last then
         Self.Data :=
           new Any_Array'(Self.Data.all & Any_Array'(1 .. Self.Length => <>));
      end if;
      Self.Length := Self.Length + 1;
      Self.Data (Self.Length) := V;
   end Append;

   overriding procedure Adjust (Self : in out Any_Vector) is
   begin
      if Self.Length > 0 then
         Self.Data := new Any_Array'(Self.Data (1 .. Self.Length));
      end if;
   end Adjust;

   overriding procedure Finalize (Self : in out Any_Vector) is
   begin
      if Self.Data /= null then
         Free (Self.Data);
      end if;
   end Finalize;

   not overriding function Get_Any_Variable_Reference
    (Self  : aliased in out Any_Vector;
     Index : Positive)
      return Any_Variable_Reference is
   begin
      return (Element => Self.Data (Index)'Access);
   end Get_Any_Variable_Reference;

   not overriding function Get_Any_Constant_Reference
    (Self  : aliased Any_Vector;
     Index : Positive)
      return Any_Constant_Reference is
   begin
      return (Element => Self.Data (Index)'Access);
   end Get_Any_Constant_Reference;

   procedure Read_Any
    (Stream : access Ada.Streams.Root_Stream_Type'Class;
     V      : out Any) is
      Key : aliased PB_Support.IO.Key;
   begin
      while PB_Support.IO.Read_Key (Stream, Key'Access) loop
         case Key.Field is
            when 1 =>
               PB_Support.IO.Read (Stream, Key.Encoding, V.Type_Url);
            when 2 =>
               PB_Support.IO.Read (Stream, Key.Encoding, V.Value);
            when others =>
               PB_Support.IO.Unknown_Field (Stream, Key.Encoding);
         end case;
      end loop;
   end Read_Any;

   procedure Write_Any
    (Stream : access Ada.Streams.Root_Stream_Type'Class;
     V      : Any) is
   begin
      if Stream.all not in PB_Support.Internal.Stream then
         declare
            WS : aliased PB_Support.Internal.Stream (Stream);
         begin
            Write_Any (WS'Access, V);
            return;
         end;
      end if;
      declare
         WS : PB_Support.Internal.Stream renames
           PB_Support.Internal.Stream (Stream.all);
      begin
         WS.Start_Message;
         WS.Write_Option (1, V.Type_Url);
         WS.Write_Option (2, V.Value);
         if WS.End_Message then
            Write_Any (WS'Access, V);
         end if;
      end;
   end Write_Any;

end Google.Protobuf.Any;