Commit 28e95302 authored by Maxime Perrotin's avatar Maxime Perrotin

Complete iteration of enumerated

parent 4bc8737a
with Text_IO; use Text_IO;
package body ASN1_Iterators.Generic_Basic is package body ASN1_Iterators.Generic_Basic is
procedure Initialize (self: in out Basic_ASN1_Iterator) is
begin
Self.Value.Initialize;
end;
function First (Item : Iterator) return Iterator_Ptr is -- When iterating, the sequence of call is:
begin -- First : Set the first value, set validity flag to true
Item.Ptr.Value.First; -- Has_Element : Check if the element is valid, e.g. for an integer, <= Max
return Item.Ptr; -- Elem_Value : Return the value that was set
end; -- Next : Compute and set the next value
-- Next can raise an exception, after the last value, to set a validity
-- flag used by Has_Element.
procedure Initialize (Self : in out Basic_ASN1_Iterator) is
begin
Self.Value.Initialize;
Self.Is_Valid := True;
end;
function Has_Element (Ptr : Iterator_Ptr) return Boolean is
(Ptr.Value.Has_Element and Ptr.Is_Valid);
function First (Item : Iterator) return Iterator_Ptr is
begin
Item.Ptr.Value.First;
-- Reset validity flag
Item.Ptr.Is_Valid := True;
return Item.Ptr;
end;
function Next (Item : Iterator; function Next (Item : Iterator;
Ptr : Iterator_Ptr) return Iterator_Ptr is Ptr : Iterator_Ptr) return Iterator_Ptr is
pragma Unreferenced (Item); pragma Unreferenced (Item);
begin begin
Ptr.Value.Next; Ptr.Value.Next;
return Ptr; return Ptr;
end; exception
when Stop_Iteration => Ptr.Is_Valid := False;
return Ptr;
end;
function Iterate (self : Basic_ASN1_Iterator) function Iterate (Self : Basic_ASN1_Iterator)
return Forward_Iterator is return Forward_Iterator is
begin begin
return I: Iterator do return I: Iterator do
I.Ptr := self'Unrestricted_Access; I.Ptr := Self'Unrestricted_Access;
end return; end return;
end; end;
end; end;
...@@ -11,7 +11,8 @@ package ASN1_Iterators.Generic_Basic is ...@@ -11,7 +11,8 @@ package ASN1_Iterators.Generic_Basic is
type Basic_ASN1_Iterator is new Controlled type Basic_ASN1_Iterator is new Controlled
with record with record
Value : P.SimpleType; Value : P.SimpleType;
Is_Valid : Boolean := True;
end record end record
with Default_Iterator => Iterate, with Default_Iterator => Iterate,
Iterator_Element => P.Sort, Iterator_Element => P.Sort,
...@@ -21,8 +22,7 @@ package ASN1_Iterators.Generic_Basic is ...@@ -21,8 +22,7 @@ package ASN1_Iterators.Generic_Basic is
type Iterator_Ptr is access all Basic_ASN1_Iterator; type Iterator_Ptr is access all Basic_ASN1_Iterator;
function Has_Element (Ptr : Iterator_Ptr) return Boolean is function Has_Element (Ptr : Iterator_Ptr) return Boolean;
(Ptr.Value.Has_Element);
package Iterators is package Iterators is
new Ada.Iterator_Interfaces (Iterator_Ptr, Has_Element); new Ada.Iterator_Interfaces (Iterator_Ptr, Has_Element);
...@@ -39,9 +39,9 @@ package ASN1_Iterators.Generic_Basic is ...@@ -39,9 +39,9 @@ package ASN1_Iterators.Generic_Basic is
subtype Forward_Iterator is Iterators.Forward_Iterator'Class; subtype Forward_Iterator is Iterators.Forward_Iterator'Class;
function Iterate (self : Basic_ASN1_Iterator) return Forward_Iterator; function Iterate (Self : Basic_ASN1_Iterator) return Forward_Iterator;
function Elem_Value (self : Basic_ASN1_Iterator; function Elem_Value (Self : Basic_ASN1_Iterator;
Ptr : Iterator_Ptr) Ptr : Iterator_Ptr)
return P.Sort is (Ptr.Value.Element_Value); return P.Sort is (Ptr.Value.Element_Value);
end; end;
...@@ -2,20 +2,16 @@ with Text_IO; use Text_IO; ...@@ -2,20 +2,16 @@ with Text_IO; use Text_IO;
package body ASN1_Iterators.Generic_Enumerated is package body ASN1_Iterators.Generic_Enumerated is
function Elem_Init return Sort is function Elem_Init return Sort is (Sort'First);
begin
Put_Line ("Elem_Init "); function Has_Elem (Value: Sort) return Boolean is (True);
Is_Last := (Sort'First = Sort'Last);
return Sort'First;
end Elem_Init;
function Has_Elem (Value: Sort) return Boolean is function Elem_Next (Value : Sort) return Sort is
Result : constant Boolean := (not Is_Last and Value <= Sort'Last);
begin begin
Put_Line ("Value: " & Value'Img & " Has_Elem = " & Result'Img); if Value < Sort'Last then
if Value = Sort'Last then return Sort'Succ (Value);
Is_Last := True; else
raise Stop_Iteration;
end if; end if;
return Result; end Elem_Next;
end Has_Elem;
end; end;
...@@ -7,16 +7,12 @@ use Interfaces; ...@@ -7,16 +7,12 @@ use Interfaces;
generic generic
type Sort is (<>); -- a discrete type, such as enumerated type Sort is (<>); -- a discrete type, such as enumerated
package ASN1_Iterators.Generic_Enumerated is package ASN1_Iterators.Generic_Enumerated is
-- To avoid exception with 'Succ attribute when last item is reached
-- keep a flag set to one when the iteration is finished
Is_Last : Boolean := False;
function Elem_Init return Sort; function Elem_Init return Sort;
function Has_Elem (Value : Sort) return Boolean; function Has_Elem (Value : Sort) return Boolean;
function Elem_First return Sort is (Sort'First); function Elem_First return Sort is (Sort'First);
function Elem_Next (Value : Sort) return Sort is function Elem_Next (Value : Sort) return Sort;
(if Is_Last then Sort'Last else Sort'Succ (Value));
package Discrete_Type is new SimpleTypes (Sort => Sort, package Discrete_Type is new SimpleTypes (Sort => Sort,
Elem_Init => Elem_Init, Elem_Init => Elem_Init,
......
...@@ -59,6 +59,4 @@ package ASN1_Iterators.Generic_Fixed_SeqOf is ...@@ -59,6 +59,4 @@ package ASN1_Iterators.Generic_Fixed_SeqOf is
function Element_SeqOf_Value (Self : ASN1_SeqOf; function Element_SeqOf_Value (Self : ASN1_SeqOf;
Ptr : ASN1_SeqOf_Ptr) Ptr : ASN1_SeqOf_Ptr)
return SeqOf is (self.Value); return SeqOf is (self.Value);
end; end;
with ASN1_Iterators.SimpleTypes, with Interfaces,
ASN1_Iterators.SimpleTypes,
ASN1_Iterators.Generic_Basic; ASN1_Iterators.Generic_Basic;
with Interfaces;
use Interfaces; use Interfaces;
generic generic
...@@ -9,11 +9,14 @@ generic ...@@ -9,11 +9,14 @@ generic
Max: Interfaces.Integer_64; Max: Interfaces.Integer_64;
package ASN1_Iterators.Generic_Integer is package ASN1_Iterators.Generic_Integer is
function Elem_Init return Interfaces.Integer_64 is (Min); function Elem_Init return Interfaces.Integer_64 is (Min);
function Has_Elem(Value: Interfaces.Integer_64) return Boolean is
(Value <= Max); function Has_Elem (Value : Interfaces.Integer_64)
return Boolean is (Value <= Max);
function Elem_First return Interfaces.Integer_64 is (Min); function Elem_First return Interfaces.Integer_64 is (Min);
function Elem_Next(Value: Interfaces.Integer_64) return Interfaces.Integer_64 is
(Value + 1); function Elem_Next (Value : Interfaces.Integer_64)
return Interfaces.Integer_64 is (Value + 1);
package Signed_Integer is new SimpleTypes package Signed_Integer is new SimpleTypes
(Sort => Interfaces.Integer_64, (Sort => Interfaces.Integer_64,
......
...@@ -5,7 +5,8 @@ package body ASN1_Iterators.Generic_SeqOf is ...@@ -5,7 +5,8 @@ package body ASN1_Iterators.Generic_SeqOf is
self.value.length := self.length; self.value.length := self.length;
if self.length >= 1 then if self.length >= 1 then
self.RestVal := new P.ASN1_SeqOf(Self.Length); self.RestVal := new P.ASN1_SeqOf(Self.Length);
self.RestIt := new P.ASN1_SeqOf_It'(P.ASN1_SeqOf_It(Self.RestVal.Iterate)); self.RestIt :=
new P.ASN1_SeqOf_It'(P.ASN1_SeqOf_It(Self.RestVal.Iterate));
self.RestIt.Ptr := self.RestIt.First; self.RestIt.Ptr := self.RestIt.First;
end if; end if;
end; end;
...@@ -21,42 +22,45 @@ package body ASN1_Iterators.Generic_SeqOf is ...@@ -21,42 +22,45 @@ package body ASN1_Iterators.Generic_SeqOf is
end return; end return;
end; end;
function First (Item : ASN1_Variable_SeqOf_It) return ASN1_Variable_SeqOf_Ptr is function First (Item : ASN1_Variable_SeqOf_It)
return ASN1_Variable_SeqOf_Ptr is
begin begin
-- Initialize the iterator (Compute first value) -- Initialize the iterator (Compute first value)
Item.Ptr.LenIt := Length_Pkg.Iterator(Item.Ptr.LenVal.Iterate); Item.Ptr.LenIt := Length_Pkg.Iterator(Item.Ptr.LenVal.Iterate);
--Item.Ptr.Value := Item.Ptr.RestIt.Ptr.Value;
-- Copy only the actual size, since the arrays may be different in size -- Copy only the actual size, since the arrays may be different in size
Item.Ptr.Value.Data(1..Item.Ptr.RestIt.Ptr.Value.Data'Length) := Item.Ptr.RestIt.Ptr.Value.Data; Item.Ptr.Value.Data (1 .. Item.Ptr.RestIt.Ptr.Value.Data'Length) :=
Item.Ptr.RestIt.Ptr.Value.Data;
return Item.Ptr; return Item.Ptr;
end; end;
function Next (Item : ASN1_Variable_SeqOf_It; function Next (Item : ASN1_Variable_SeqOf_It;
Ptr : ASN1_Variable_SeqOf_Ptr) return ASN1_Variable_SeqOf_Ptr is Ptr : ASN1_Variable_SeqOf_Ptr)
return ASN1_Variable_SeqOf_Ptr
is
pragma Unreferenced (Item); pragma Unreferenced (Item);
Ptr_elem : Length_Pkg.Iterator_Ptr := Length_Pkg.Ptr(Ptr.LenIt); Ptr_elem : Length_Pkg.Iterator_Ptr := Length_Pkg.Ptr(Ptr.LenIt);
begin begin
Ptr.RestIt.Ptr := Ptr.RestIt.Next (Ptr.RestIt.Ptr); Ptr.RestIt.Ptr := Ptr.RestIt.Next (Ptr.RestIt.Ptr);
if P.Has_Element_SeqOf (Ptr.RestIt.Ptr) then if P.Has_Element_SeqOf (Ptr.RestIt.Ptr) then
--Ptr.Value := Ptr.RestIt.Ptr.Value; Ptr.Value.Data (1 .. Ptr.RestIt.Ptr.Value.Data'Length) :=
Ptr.Value.Data(1..Ptr.RestIt.Ptr.Value.Data'Length) := Ptr.RestIt.Ptr.Value.Data; Ptr.RestIt.Ptr.Value.Data;
else else
Ptr.RestIt.Ptr := Ptr.RestIt.First; Ptr.RestIt.Ptr := Ptr.RestIt.First;
-- Exhausted "rest": iterate on the first item -- Exhausted "rest": iterate on the first item
Ptr_elem := Ptr.LenIt.Next(Ptr_elem); Ptr_elem := Ptr.LenIt.Next (Ptr_elem);
if Length_Pkg.Has_Element (Ptr_elem) then if Length_Pkg.Has_Element (Ptr_elem) then
Ptr.Value.Length := Integer(Ptr_elem.Value.Value); Ptr.Value.Length := Integer (Ptr_Elem.Value.Value);
Ptr.Length := Ptr.Value.Length; Ptr.Length := Ptr.Value.Length;
Ptr.RestVal := new P.ASN1_SeqOf(Ptr.Value.Length); Ptr.RestVal := new P.ASN1_SeqOf(Ptr.Value.Length);
Ptr.RestIt := new P.ASN1_SeqOf_It'(P.ASN1_SeqOf_It(Ptr.RestVal.Iterate)); Ptr.RestIt :=
Ptr.RestIt.Ptr := Ptr.RestIt.First; new P.ASN1_SeqOf_It'(P.ASN1_SeqOf_It (Ptr.RestVal.Iterate));
--Ptr.Value := Ptr.RestIt.Ptr.Value; Ptr.RestIt.Ptr := Ptr.RestIt.First;
Ptr.Value.Data(1..Ptr.RestIt.Ptr.Value.Data'Length) := Ptr.RestIt.Ptr.Value.Data; Ptr.Value.Data (1 .. Ptr.RestIt.Ptr.Value.Data'Length) :=
Ptr.RestIt.Ptr.Value.Data;
end if; end if;
end if; end if;
return Ptr; return Ptr;
end; end;
end; end;
package ASN1_Iterators is package ASN1_Iterators is
Stop_Iteration : exception;
end; end;
...@@ -61,7 +61,7 @@ begin ...@@ -61,7 +61,7 @@ begin
end loop; end loop;
-- test: compute all combination of values for the variable-size array -- test: compute all combination of values for the variable-size array
New_Line; New_Line;
Put_Line ("Fixed-size array:"); Put_Line ("Fixed-size array:");
i := 1; i := 1;
for each of Fixed_SeqOf loop for each of Fixed_SeqOf loop
......
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