Commit 4c18a8f1 authored by Maxime Perrotin's avatar Maxime Perrotin

Iterate on the iterators (work in progress)

parent e3be2a4d
package body ASN1_Iterators.Generic_Iterator is package body ASN1_Iterators.Generic_Iterator is
procedure Initialize (Self: in out Generic_Iterator_Type) is -- procedure Initialize (Self: in out Generic_Iterator_Type) is
begin -- begin
Self.Item.Initialize; -- Self.Item.Initialize;
Self.Item.Value := ASN1_Iterable.First(ASN1_Iterable.Iterable'Class(Self.Item)); -- Self.Item.Value := ASN1_Iterable.First(ASN1_Iterable.Iterable'Class(Self.Item));
end; -- end;
function First (Item : Iterator) return Cursor is function First (Item : Iterator) return Cursor is
C: constant Cursor := Item.Ptr; C: constant Cursor := Item.Ptr;
......
...@@ -9,7 +9,7 @@ generic ...@@ -9,7 +9,7 @@ generic
type Actual_Iterable is new ASN1_Iterable.Iterable with private; type Actual_Iterable is new ASN1_Iterable.Iterable with private;
package ASN1_Iterators.Generic_Iterator is package ASN1_Iterators.Generic_Iterator is
type Generic_Iterator_Type is new Controlled with type Generic_Iterator_Type is tagged --new Controlled with
record record
Item: Actual_Iterable; Item: Actual_Iterable;
end record end record
...@@ -31,8 +31,8 @@ package ASN1_Iterators.Generic_Iterator is ...@@ -31,8 +31,8 @@ package ASN1_Iterators.Generic_Iterator is
Ptr : Cursor; Ptr : Cursor;
end record; end record;
overriding --overriding
procedure Initialize (Self: in out Generic_Iterator_Type); --procedure Initialize (Self: in out Generic_Iterator_Type);
overriding overriding
function First (Item : Iterator) return Cursor; function First (Item : Iterator) return Cursor;
......
...@@ -18,7 +18,7 @@ package ASN1_Iterators.Iterable_Integer is ...@@ -18,7 +18,7 @@ package ASN1_Iterators.Iterable_Integer is
(It.Value <= Max); (It.Value <= Max);
overriding overriding
function First (It: ASN1_Integer) return Integer_64 is function First (It: in out ASN1_Integer) return Integer_64 is
(Min); (Min);
overriding overriding
......
with Ada.Text_IO; with Ada.Text_IO;
use Ada.Text_IO; use Ada.Text_IO;
with Ada.Iterator_Interfaces;
package body ASN1_Iterators.Iterable_SeqOF is package body ASN1_Iterators.Iterable_SeqOF is
procedure Initialize (It: in out ASN1_SequenceOf) is procedure Initialize (It: in out ASN1_SequenceOf) is
begin begin
Put_Line ("Initialize Iterable_SeqOF length " & It.Length'Img); null;
It.Value.Length := It.Length; -- deprecated
if It.Length > 1 then
It.Rest := new Instance; -- not initialized
It.Rest.Item.Length := It.Length - 1;
It.Rest_It := new Cust_Iterator'(Cust_Iterator(It.Rest.Iterate));
It.Rest_It.Ptr := It.Rest_It.First;
end if;
end; end;
function Has_Element (It: ASN1_SequenceOf) return Boolean is function Has_Element (It: ASN1_SequenceOf) return Boolean is
...@@ -21,13 +16,26 @@ package body ASN1_Iterators.Iterable_SeqOF is ...@@ -21,13 +16,26 @@ package body ASN1_Iterators.Iterable_SeqOF is
return False; return False;
end; end;
function First (It: ASN1_SequenceOf) return Generic_SeqOf is function First (It: in out ASN1_SequenceOf) return Generic_SeqOf is
-- Ptr_elem : P.Iterator_Ptr := P.Ptr(Item.Ptr.FirstIt); -- Ptr_elem : P.Iterator_Ptr := P.Ptr(Item.Ptr.FirstIt);
Temp : constant Generic_SeqOf := It.Value; Ret : constant Generic_SeqOf := It.Value;
begin begin
It.Value.Length := It.Length;
Put_Line ("Computing first of length " & It.Length'Img);
-- Initialize the recursive structure (once)
if It.Length > 1 and then It.Rest = Null then
Put_Line ("Initialize Length " & It.Length'Img);
It.Rest := new Instance;
It.Rest.Item.Length := It.Length - 1;
It.Rest_It :=
new Cust_Iterator'(Custom_Iterator.Iterator (It.Rest.Iterate)
with others => <>);
It.Rest_It.Ptr := It.Rest_It.First;
end if;
-- Initialize the iterator (Compute first value) -- Initialize the iterator (Compute first value)
-- Item.Ptr.FirstIt := P.Iterator(Item.Ptr.FirstVal.Iterate); -- Item.Ptr.FirstIt := P.Iterator(Item.Ptr.FirstVal.Iterate);
-- Ptr_elem := P.First(Item.Ptr.FirstIt); -- Ptr_elem := P.First(Item.Ptr.FirstIt);
-- Item.Ptr.Value.Data(1) := P.Elem_Value(Item.Ptr.FirstVal, -- Item.Ptr.Value.Data(1) := P.Elem_Value(Item.Ptr.FirstVal,
-- Ptr_elem); -- Ptr_elem);
-- if Item.Ptr.Length > 1 then -- if Item.Ptr.Length > 1 then
...@@ -35,7 +43,7 @@ package body ASN1_Iterators.Iterable_SeqOF is ...@@ -35,7 +43,7 @@ package body ASN1_Iterators.Iterable_SeqOF is
-- Item.Ptr.Rest_It.Ptr.Value.Data(1..Item.Ptr.Rest_It.Ptr.Length); -- Item.Ptr.Rest_It.Ptr.Value.Data(1..Item.Ptr.Rest_It.Ptr.Length);
-- end if; -- end if;
-- return Item.Ptr; -- return Item.Ptr;
return Temp; return Ret;
end; end;
function Next (It: ASN1_SequenceOf) return Generic_SeqOf is function Next (It: ASN1_SequenceOf) return Generic_SeqOf is
......
...@@ -39,7 +39,7 @@ package ASN1_Iterators.Iterable_SeqOF is ...@@ -39,7 +39,7 @@ package ASN1_Iterators.Iterable_SeqOF is
function Has_Element (It: ASN1_SequenceOf) return Boolean; function Has_Element (It: ASN1_SequenceOf) return Boolean;
overriding overriding
function First (It: ASN1_SequenceOf) return Generic_SeqOf; function First (It: in out ASN1_SequenceOf) return Generic_SeqOf;
overriding overriding
function Next (It: ASN1_SequenceOf) return Generic_SeqOf; function Next (It: ASN1_SequenceOf) return Generic_SeqOf;
......
...@@ -19,7 +19,7 @@ package ASN1_Iterators.Iterable_Type is ...@@ -19,7 +19,7 @@ package ASN1_Iterators.Iterable_Type is
function Has_Element (It: Iterable) return boolean is abstract; function Has_Element (It: Iterable) return boolean is abstract;
function First (It: Iterable) return Element_Type is abstract; function First (It: in out Iterable) return Element_Type is abstract;
function Next (It: Iterable) return Element_Type is abstract; function Next (It: Iterable) return Element_Type is abstract;
end; end;
...@@ -12,8 +12,10 @@ procedure test_new_iterators is ...@@ -12,8 +12,10 @@ procedure test_new_iterators is
C: MyInt.Cursor; C: MyInt.Cursor;
-- SEQUENCE OF -- SEQUENCE OF
package MySeqOf is new ASN1_Iterators.Iterable_SeqOF (1, 3, MyInt.Custom_Iterator); package MySeqOf is new ASN1_Iterators.Iterable_SeqOF (3, 5, MyInt.Custom_Iterator);
ItSO : MySeqOf.Instance; ItSO : MySeqOf.Instance;
IterSO : MySeqOf.Iterator := ItSO.Iterate;
CSO : MySeqOf.Cursor;
begin begin
Put_Line ("Hello"); Put_Line ("Hello");
Put_Line ("With 'for each of':"); Put_Line ("With 'for each of':");
...@@ -31,4 +33,10 @@ begin ...@@ -31,4 +33,10 @@ begin
Put_Line (It.Element (C)'Img); Put_Line (It.Element (C)'Img);
C := Iter.Next (C); C := Iter.Next (C);
end loop; end loop;
Put_Line ("SeqOF manual iterators:");
CSO := IterSO.First;
while MySeqOf.Has_Element (CSO) loop
Put_Line ("Element"); -- ItSO.Element (CSO)'Img);
CSO := IterSO.Next (CSO);
end loop;
end; end;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment