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
procedure Initialize (self: in out Basic_ASN1_Iterator) is
-- When iterating, the sequence of call is:
-- First : Set the first value, set validity flag to true
-- Has_Element : Check if the element is valid, e.g. for an integer, <= Max
-- Elem_Value : Return the value that was set
-- 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;
......@@ -16,13 +33,16 @@ package body ASN1_Iterators.Generic_Basic is
begin
Ptr.Value.Next;
return Ptr;
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
begin
return I: Iterator do
I.Ptr := self'Unrestricted_Access;
I.Ptr := Self'Unrestricted_Access;
end return;
end;
end;
......@@ -12,6 +12,7 @@ package ASN1_Iterators.Generic_Basic is
type Basic_ASN1_Iterator is new Controlled
with record
Value : P.SimpleType;
Is_Valid : Boolean := True;
end record
with Default_Iterator => Iterate,
Iterator_Element => P.Sort,
......@@ -21,8 +22,7 @@ package ASN1_Iterators.Generic_Basic is
type Iterator_Ptr is access all Basic_ASN1_Iterator;
function Has_Element (Ptr : Iterator_Ptr) return Boolean is
(Ptr.Value.Has_Element);
function Has_Element (Ptr : Iterator_Ptr) return Boolean;
package Iterators is
new Ada.Iterator_Interfaces (Iterator_Ptr, Has_Element);
......@@ -39,9 +39,9 @@ package ASN1_Iterators.Generic_Basic is
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)
return P.Sort is (Ptr.Value.Element_Value);
end;
......@@ -2,20 +2,16 @@ with Text_IO; use Text_IO;
package body ASN1_Iterators.Generic_Enumerated is
function Elem_Init return Sort is
begin
Put_Line ("Elem_Init ");
Is_Last := (Sort'First = Sort'Last);
return Sort'First;
end Elem_Init;
function Elem_Init return Sort is (Sort'First);
function Has_Elem (Value: Sort) return Boolean is (True);
function Has_Elem (Value: Sort) return Boolean is
Result : constant Boolean := (not Is_Last and Value <= Sort'Last);
function Elem_Next (Value : Sort) return Sort is
begin
Put_Line ("Value: " & Value'Img & " Has_Elem = " & Result'Img);
if Value = Sort'Last then
Is_Last := True;
if Value < Sort'Last then
return Sort'Succ (Value);
else
raise Stop_Iteration;
end if;
return Result;
end Has_Elem;
end Elem_Next;
end;
......@@ -7,16 +7,12 @@ use Interfaces;
generic
type Sort is (<>); -- a discrete type, such as enumerated
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 Has_Elem (Value : Sort) return Boolean;
function Elem_First return Sort is (Sort'First);
function Elem_Next (Value : Sort) return Sort is
(if Is_Last then Sort'Last else Sort'Succ (Value));
function Elem_Next (Value : Sort) return Sort;
package Discrete_Type is new SimpleTypes (Sort => Sort,
Elem_Init => Elem_Init,
......
......@@ -59,6 +59,4 @@ package ASN1_Iterators.Generic_Fixed_SeqOf is
function Element_SeqOf_Value (Self : ASN1_SeqOf;
Ptr : ASN1_SeqOf_Ptr)
return SeqOf is (self.Value);
end;
with ASN1_Iterators.SimpleTypes,
with Interfaces,
ASN1_Iterators.SimpleTypes,
ASN1_Iterators.Generic_Basic;
with Interfaces;
use Interfaces;
generic
......@@ -9,11 +9,14 @@ generic
Max: Interfaces.Integer_64;
package ASN1_Iterators.Generic_Integer is
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_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
(Sort => Interfaces.Integer_64,
......
......@@ -5,7 +5,8 @@ package body ASN1_Iterators.Generic_SeqOf is
self.value.length := self.length;
if self.length >= 1 then
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;
end if;
end;
......@@ -21,42 +22,45 @@ package body ASN1_Iterators.Generic_SeqOf is
end return;
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
-- Initialize the iterator (Compute first value)
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
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;
end;
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);
Ptr_elem : Length_Pkg.Iterator_Ptr := Length_Pkg.Ptr(Ptr.LenIt);
begin
Ptr.RestIt.Ptr := Ptr.RestIt.Next (Ptr.RestIt.Ptr);
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.RestIt.Ptr.Value.Data;
Ptr.Value.Data (1 .. Ptr.RestIt.Ptr.Value.Data'Length) :=
Ptr.RestIt.Ptr.Value.Data;
else
Ptr.RestIt.Ptr := Ptr.RestIt.First;
-- 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
Ptr.Value.Length := Integer(Ptr_elem.Value.Value);
Ptr.Value.Length := Integer (Ptr_Elem.Value.Value);
Ptr.Length := 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 :=
new P.ASN1_SeqOf_It'(P.ASN1_SeqOf_It (Ptr.RestVal.Iterate));
Ptr.RestIt.Ptr := Ptr.RestIt.First;
--Ptr.Value := Ptr.RestIt.Ptr.Value;
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;
return Ptr;
end;
end;
package ASN1_Iterators is
Stop_Iteration : exception;
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