Commit d20683a4 authored by Maxime Perrotin's avatar Maxime Perrotin

Work on iterators

parent 40c02b44
......@@ -16,10 +16,10 @@ package body ASN1_Iterators.Generic_Basic is
Self.Is_Valid := True;
end;
function Has_Element (Ptr : Iterator_Ptr) return Boolean is
function Has_Element (Ptr : Cursor) return Boolean is
(Ptr.Value.Has_Element and Ptr.Is_Valid);
function First (Item : Iterator) return Iterator_Ptr is
function First (Item : Iterator) return Cursor is
begin
Item.Ptr.Value.First;
-- Reset validity flag
......@@ -28,7 +28,7 @@ package body ASN1_Iterators.Generic_Basic is
end;
function Next (Item : Iterator;
Ptr : Iterator_Ptr) return Iterator_Ptr is
Ptr : Cursor) return Cursor is
pragma Unreferenced (Item);
begin
Ptr.Value.Next;
......
......@@ -4,44 +4,44 @@ use Ada.Finalization;
with ASN1_Iterators.SimpleTypes;
generic
with package P is new SimpleTypes (<>);
with package P is new SimpleTypes (<>);
package ASN1_Iterators.Generic_Basic is
-- Provides an iterator for a basic type
subtype Sort is P.Sort;
-- Provides an iterator for a basic type
subtype Sort is P.Sort;
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,
Constant_Indexing => Elem_Value;
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,
Constant_Indexing => Elem_Value;
procedure Initialize (self: in out Basic_ASN1_Iterator);
procedure Initialize (self: in out Basic_ASN1_Iterator);
type Iterator_Ptr is access all Basic_ASN1_Iterator;
type Cursor is access all Basic_ASN1_Iterator;
function Has_Element (Ptr : Iterator_Ptr) return Boolean;
function Has_Element (Ptr : Cursor) return Boolean;
package Iterators is
new Ada.Iterator_Interfaces (Iterator_Ptr, Has_Element);
package Iterators is
new Ada.Iterator_Interfaces (Cursor, Has_Element);
type Iterator is new Iterators.Forward_Iterator with record
Ptr : Iterator_Ptr;
end record;
type Iterator is new Iterators.Forward_Iterator with record
Ptr : Cursor;
end record;
function Ptr (Item: Iterator) return Iterator_Ptr is (Item.Ptr);
function Ptr (Item : Iterator) return Cursor is (Item.Ptr);
overriding function First (Item : Iterator) return Iterator_Ptr;
overriding function Next (Item : Iterator;
Ptr : Iterator_Ptr) return Iterator_Ptr;
overriding function First (Item : Iterator) return Cursor;
overriding function Next (Item : Iterator;
Ptr : Cursor) return Cursor;
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;
Ptr : Iterator_Ptr)
return P.Sort is (Ptr.Value.Element_Value);
end;
function Elem_Value (Self : Basic_ASN1_Iterator;
Ptr : Cursor)
return P.Sort is (Ptr.Value.Element_Value);
end ASN1_Iterators.Generic_Basic;
......@@ -5,22 +5,22 @@ with Interfaces;
use Interfaces;
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
function Elem_Init return Sort;
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;
function Has_Elem (Value : Sort) return Boolean;
function Elem_First return Sort is (Sort'First);
function Elem_Next (Value : Sort) return Sort;
package Discrete_Type is new SimpleTypes (Sort => Sort,
Elem_Init => Elem_Init,
Has_Elem => Has_Elem,
Elem_First => Elem_First,
Elem_Next => Elem_Next);
package Discrete_Type is new SimpleTypes (Sort => Sort,
Elem_Init => Elem_Init,
Has_Elem => Has_Elem,
Elem_First => Elem_First,
Elem_Next => Elem_Next);
package It is new Generic_Basic (P => Discrete_Type);
package It is new Generic_Basic (P => Discrete_Type);
subtype Instance is It.Basic_ASN1_Iterator;
end;
subtype Instance is It.Basic_ASN1_Iterator;
end ASN1_Iterators.Generic_Enumerated;
......@@ -25,7 +25,7 @@ package body ASN1_Iterators.Generic_Fixed_SeqOf is
function First (Item : ASN1_SeqOf_It) return ASN1_SeqOf_Ptr is
Ptr_Elem : P.Iterator_Ptr := P.Ptr (Item.Ptr.FirstIt);
Ptr_Elem : P.Cursor := P.Ptr (Item.Ptr.FirstIt);
begin
-- Initialize the iterator (Compute first value)
Item.Ptr.FirstIt := P.Iterator (Item.Ptr.FirstVal.Iterate);
......@@ -42,7 +42,7 @@ package body ASN1_Iterators.Generic_Fixed_SeqOf is
function Next (Item : ASN1_SeqOf_It;
Ptr : ASN1_SeqOf_Ptr) return ASN1_SeqOf_Ptr is
pragma Unreferenced (Item);
Ptr_elem : P.Iterator_Ptr := P.Ptr (Ptr.FirstIt);
Ptr_elem : P.Cursor := P.Ptr (Ptr.FirstIt);
begin
if Ptr.Length > 1 then
Ptr.RestIt.Ptr := Ptr.RestIt.Next (Ptr.RestIt.Ptr);
......
......@@ -7,56 +7,56 @@ with ASN1_Iterators.Generic_Basic;
with ASN1_Iterators.SimpleTypes;
generic
with package P is new Generic_Basic (<>);
with package P is new Generic_Basic (<>);
package ASN1_Iterators.Generic_Fixed_SeqOf is
type DataArray is array (natural range <>) of P.Sort;
type SeqOf (Max : Natural) is record
Length : Integer;
Data : DataArray (1 .. Max);
end record;
type ASN1_SeqOf_Ptr;
type ASN1_SeqOf_It;
type ASN1_SeqOf (Size : Natural) is new Controlled
with record
Length : Natural := Size;
Value : SeqOf (Size);
-- First value
FirstVal : P.Basic_ASN1_Iterator;
FirstIt : P.Iterator;
-- The rest (recursive)
Rest : access ASN1_SeqOf;
RestIt : access ASN1_SeqOf_It;
end record
type DataArray is array (natural range <>) of P.Sort;
type SeqOf (Max : Natural) is record
Length : Integer;
Data : DataArray (1 .. Max);
end record;
type ASN1_SeqOf_Ptr;
type ASN1_SeqOf_It;
type ASN1_SeqOf (Size : Natural) is new Controlled
with record
Length : Natural := Size;
Value : SeqOf (Size);
-- First value
FirstVal : P.Basic_ASN1_Iterator;
FirstIt : P.Iterator;
-- The rest (recursive)
Rest : access ASN1_SeqOf;
RestIt : access ASN1_SeqOf_It;
end record
with Default_Iterator => Iterate,
Iterator_Element => SeqOf,
Constant_Indexing => Element_SeqOf_Value;
type ASN1_SeqOf_Ptr is access all ASN1_SeqOf;
type ASN1_SeqOf_Ptr is access all ASN1_SeqOf;
-- Constructor (called automatically)
procedure Initialize (Self : in out ASN1_SeqOf);
-- Constructor (called automatically)
procedure Initialize (Self : in out ASN1_SeqOf);
function Has_Element_SeqOf (Ptr : ASN1_SeqOf_Ptr) return Boolean;
function Has_Element_SeqOf (Ptr : ASN1_SeqOf_Ptr) return Boolean;
package Iterators_SeqOf is
new Ada.Iterator_Interfaces (ASN1_SeqOf_Ptr, Has_Element_SeqOf);
package Iterators_SeqOf is
new Ada.Iterator_Interfaces (ASN1_SeqOf_Ptr, Has_Element_SeqOf);
type ASN1_SeqOf_It is new Iterators_SeqOf.Forward_Iterator with record
Ptr : ASN1_SeqOf_Ptr;
end record;
type ASN1_SeqOf_It is new Iterators_SeqOf.Forward_Iterator with record
Ptr : ASN1_SeqOf_Ptr;
end record;
overriding function First (Item : ASN1_SeqOf_It) return ASN1_SeqOf_Ptr;
overriding function Next (Item : ASN1_SeqOf_It;
Ptr : ASN1_SeqOf_Ptr) return ASN1_SeqOf_Ptr;
overriding function First (Item : ASN1_SeqOf_It) return ASN1_SeqOf_Ptr;
overriding function Next (Item : ASN1_SeqOf_It;
Ptr : ASN1_SeqOf_Ptr) return ASN1_SeqOf_Ptr;
function Iterate (self : ASN1_SeqOf)
return ASN1_SeqOf_It'Class; -- Iterators_SeqOf.Forward_Iterator'Class;
function Iterate (self : ASN1_SeqOf)
return ASN1_SeqOf_It'Class;
function Element_SeqOf_Value (Self : ASN1_SeqOf;
Ptr : ASN1_SeqOf_Ptr)
return SeqOf is (self.Value);
end;
function Element_SeqOf_Value (Self : ASN1_SeqOf;
Ptr : ASN1_SeqOf_Ptr)
return SeqOf is (self.Value);
end ASN1_Iterators.Generic_Fixed_SeqOf;
......@@ -5,27 +5,27 @@ with Interfaces,
use Interfaces;
generic
Min: Interfaces.Integer_64;
Max: Interfaces.Integer_64;
Min : Interfaces.Integer_64;
Max : Interfaces.Integer_64;
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
(Sort => Interfaces.Integer_64,
Elem_Init => Elem_Init,
Has_Elem => Has_Elem,
Elem_First => Elem_First,
Elem_Next => Elem_Next);
package Signed_Integer is new SimpleTypes
(Sort => Interfaces.Integer_64,
Elem_Init => Elem_Init,
Has_Elem => Has_Elem,
Elem_First => Elem_First,
Elem_Next => Elem_Next);
package It is new Generic_Basic (P => Signed_Integer);
package It is new Generic_Basic (P => Signed_Integer);
subtype Instance is It.Basic_ASN1_Iterator;
end;
subtype Instance is It.Basic_ASN1_Iterator;
end ASN1_Iterators.Generic_Integer;
......@@ -39,7 +39,7 @@ package body ASN1_Iterators.Generic_SeqOf is
return ASN1_Variable_SeqOf_Ptr
is
pragma Unreferenced (Item);
Ptr_elem : Length_Pkg.Iterator_Ptr := Length_Pkg.Ptr(Ptr.LenIt);
Ptr_elem : Length_Pkg.Cursor := Length_Pkg.Ptr(Ptr.LenIt);
begin
Ptr.RestIt.Ptr := Ptr.RestIt.Next (Ptr.RestIt.Ptr);
if P.Has_Element_SeqOf (Ptr.RestIt.Ptr) then
......
......@@ -10,73 +10,71 @@ with ASN1_Iterators.Generic_Fixed_SeqOf,
with Interfaces;
use Interfaces;
-- Iterator for a variable-size array of basic type
-- Iterator for a variable-size array of basic type
generic
Min : Natural;
Max : Natural;
with package Basic is new Generic_Basic (<>);
Min : Natural;
Max : Natural;
with package Basic is new Generic_Basic (<>);
package ASN1_Iterators.Generic_SeqOf is
Package P is new Generic_Fixed_SeqOF (P => Basic);
-- Create an integer type with a range constraint to iterate on
package Length_ty is new Generic_Integer (Integer_64(Min),
Integer_64(Max));
-- Instantiate the package to iterate on the integer for the length
package Length_Pkg renames Length_ty.It;
type ASN1_Variable_SeqOf_Ptr is private;
subtype MySeqOf is P.SeqOf(Max);
type ASN1_Variable_SeqOf is new Controlled
with record
Length : Natural := Min;
Value : MySeqOf; -- P.SeqOf(Max);
-- Iterate on the length
LenVal : Length_ty.Instance;
LenIt : Length_Pkg.Iterator;
-- And on the value
RestVal : access P.ASN1_SeqOf;
RestIt : access P.ASN1_SeqOf_It;
end record
with Default_Iterator => Iterate,
Iterator_Element => MySeqOf, --P.SeqOf,
Constant_Indexing => Element_Variable_SeqOf_Value;
-- Constructor (called automatically)
procedure Initialize(self: in out ASN1_Variable_SeqOf);
function Has_Element_Variable_SeqOf (Ptr : ASN1_Variable_SeqOf_Ptr)
return Boolean;
package Iterators_Variable_SeqOf is
new Ada.Iterator_Interfaces (ASN1_Variable_SeqOf_Ptr,
Has_Element_Variable_SeqOf);
type ASN1_Variable_SeqOf_It
is new Iterators_Variable_SeqOf.Forward_Iterator with record
Ptr : ASN1_Variable_SeqOf_Ptr;
end record;
overriding
function First (Item : ASN1_Variable_SeqOf_It)
package P is new Generic_Fixed_SeqOF (P => Basic);
-- Create an integer type with a range constraint to iterate on
package Length_ty is new Generic_Integer (Integer_64 (Min),
Integer_64 (Max));
-- Instantiate the package to iterate on the integer for the length
package Length_Pkg renames Length_ty.It;
type ASN1_Variable_SeqOf_Ptr is private;
subtype MySeqOf is P.SeqOf (Max);
type ASN1_Variable_SeqOf is new Controlled
with record
Length : Natural := Min;
Value : MySeqOf;
-- Iterate on the length
LenVal : Length_ty.Instance;
LenIt : Length_Pkg.Iterator;
-- And on the value
RestVal : access P.ASN1_SeqOf;
RestIt : access P.ASN1_SeqOf_It;
end record
with Default_Iterator => Iterate,
Iterator_Element => MySeqOf,
Constant_Indexing => Element_Variable_SeqOf_Value;
-- Constructor (called automatically)
procedure Initialize (Self : in out ASN1_Variable_SeqOf);
function Has_Element_Variable_SeqOf (Ptr : ASN1_Variable_SeqOf_Ptr)
return Boolean;
package Iterators_Variable_SeqOf is
new Ada.Iterator_Interfaces (ASN1_Variable_SeqOf_Ptr,
Has_Element_Variable_SeqOf);
type ASN1_Variable_SeqOf_It
is new Iterators_Variable_SeqOf.Forward_Iterator with record
Ptr : ASN1_Variable_SeqOf_Ptr;
end record;
overriding
function First (Item : ASN1_Variable_SeqOf_It)
return ASN1_Variable_SeqOf_Ptr;
overriding
function Next (Item : ASN1_Variable_SeqOf_It;
Ptr : ASN1_Variable_SeqOf_Ptr)
return ASN1_Variable_SeqOf_Ptr;
overriding
function Next (Item : ASN1_Variable_SeqOf_It;
Ptr : ASN1_Variable_SeqOf_Ptr)
return ASN1_Variable_SeqOf_Ptr;
function Iterate (self : ASN1_Variable_SeqOf)
return Iterators_Variable_SeqOf.Forward_Iterator'Class;
function Iterate (self : ASN1_Variable_SeqOf)
return Iterators_Variable_SeqOf.Forward_Iterator'Class;
function Element_Variable_SeqOf_Value (Self : ASN1_Variable_SeqOf;
Ptr : ASN1_Variable_SeqOf_Ptr)
--return P.SeqOf is (Self.Value);
function Element_Variable_SeqOf_Value (Self : ASN1_Variable_SeqOf;
Ptr : ASN1_Variable_SeqOf_Ptr)
return MySeqOf is (Self.Value);
subtype Instance is ASN1_Variable_SeqOf;
private
type ASN1_Variable_SeqOf_Ptr is access all ASN1_Variable_SeqOf;
subtype Instance is ASN1_Variable_SeqOf;
end;
private
type ASN1_Variable_SeqOf_Ptr is access all ASN1_Variable_SeqOf;
end ASN1_Iterators.Generic_SeqOF;
......@@ -5,24 +5,24 @@ with Interfaces;
use Interfaces;
generic
Min: Interfaces.Unsigned_64;
Max: Interfaces.Unsigned_64;
Min : Interfaces.Unsigned_64;
Max : Interfaces.Unsigned_64;
package ASN1_Iterators.Generic_Unsigned_Integer is
function Elem_Init return Interfaces.Unsigned_64 is (Min);
function Has_Elem(Value: Interfaces.Unsigned_64) return Boolean is
(Value <= Max);
function Elem_First return Interfaces.Unsigned_64 is (Min);
function Elem_Next(Value: Interfaces.Unsigned_64)
return Interfaces.Unsigned_64 is (Value + 1);
function Elem_Init return Interfaces.Unsigned_64 is (Min);
function Has_Elem (Value : Interfaces.Unsigned_64) return Boolean is
(Value <= Max);
function Elem_First return Interfaces.Unsigned_64 is (Min);
function Elem_Next (Value : Interfaces.Unsigned_64)
return Interfaces.Unsigned_64 is (Value + 1);
package Unsigned_Type is new SimpleTypes
(Sort => Interfaces.Unsigned_64,
Elem_Init => Elem_Init,
Has_Elem => Has_Elem,
Elem_First => Elem_First,
Elem_Next => Elem_Next);
package Unsigned_Type is new SimpleTypes
(Sort => Interfaces.Unsigned_64,
Elem_Init => Elem_Init,
Has_Elem => Has_Elem,
Elem_First => Elem_First,
Elem_Next => Elem_Next);
package It is new Generic_Basic (P => Unsigned_Type);
package It is new Generic_Basic (P => Unsigned_Type);
subtype Instance is It.Basic_ASN1_Iterator;
end;
subtype Instance is It.Basic_ASN1_Iterator;
end ASN1_Iterators.Generic_Unsigned_Integer;
......@@ -17,7 +17,7 @@ package body ASN1_Iterators.Iterable_SeqOF is
end;
function First (It: in out ASN1_SequenceOf) return Generic_SeqOf is
-- Ptr_elem : P.Iterator_Ptr := P.Ptr(Item.Ptr.FirstIt);
-- Ptr_elem : P.Cursor := P.Ptr(Item.Ptr.FirstIt);
Ret : constant Generic_SeqOf := It.Value;
begin
It.Value.Length := It.Length;
......@@ -47,7 +47,7 @@ package body ASN1_Iterators.Iterable_SeqOF is
end;
function Next (It: ASN1_SequenceOf) return Generic_SeqOf is
--Ptr_elem : P.Iterator_Ptr := P.Ptr(Ptr.FirstIt);
--Ptr_elem : P.Cursor := P.Ptr(Ptr.FirstIt);
Temp : constant Generic_SeqOf := It.Value;
begin
-- if Ptr.Length > 1 then
......
generic
type Sort is private;
with function Elem_Init return Sort;
with function Has_Elem (Value : Sort) return Boolean;
with function Elem_First return Sort;
with function Elem_Next (Value : Sort) return Sort;
type Sort is private;
with function Elem_Init return Sort;
with function Has_Elem (Value : Sort) return Boolean;
with function Elem_First return Sort;
with function Elem_Next (Value : Sort) return Sort;
package ASN1_Iterators.SimpleTypes is
type SimpleType is tagged record
Value : Sort;
end record;
type SimpleType is tagged record
Value : Sort;
end record;
procedure Initialize (Self : in out SimpleType);
procedure Initialize (Self : in out SimpleType);
function Has_Element (Self : in out SimpleType) return Boolean is
(Has_Elem (Self.Value));
function Has_Element (Self : in out SimpleType) return Boolean is
(Has_Elem (Self.Value));
procedure First (Self : in out SimpleType);
procedure First (Self : in out SimpleType);
procedure Next (Self : in out SimpleType);
procedure Next (Self : in out SimpleType);
function Element_Value (Self : in out SimpleType) return Sort is
(Self.Value);
end;
function Element_Value (Self : in out SimpleType) return Sort is
(Self.Value);
end ASN1_Iterators.SimpleTypes;
package ASN1_Iterators is
Stop_Iteration : exception;
end;
end ASN1_Iterators;
......@@ -12,17 +12,17 @@ package body ASN1_Iterators.MySeq is
(P_a.Has_Element(P_a.Ptr(Ptr.a_it)));
function Iterate (Self : ASN1_MySeq)
return ASN1_MySeq'Class is --Iterators_MySeq.Forward_Iterator'Class is
return ASN1_MySeq_It'Class is --Iterators_MySeq.Forward_Iterator'Class is
begin
return I: ASN1_MySeq do
return I: ASN1_MySeq_It do
I.Ptr := Self'Unrestricted_Access;
end return;
end;
function First (Item : ASN1_MySeq_It) return Cursor is
Cursor_a : P_a.Iterator_Ptr := P_a.Ptr (Item.Ptr.a_it);
Cursor_b : P_b.Iterator_Ptr := P_b.Ptr (Item.Ptr.b_it);
Cursor_a : P_a.Cursor := P_a.Ptr (Item.Ptr.a_it);
Cursor_b : P_b.Cursor := P_b.Ptr (Item.Ptr.b_it);
begin
-- Initialize the iterator (Compute first value)
Item.Ptr.a_it := P_a.Iterator (Item.Ptr.a_val.Iterate);
......@@ -34,26 +34,26 @@ package body ASN1_Iterators.MySeq is
return Item.Ptr;
end;
function Next (Item : ASN1_MySeq_It;
Ptr : Cursor) return Cursor is
pragma Unreferenced (Item);
Ptr_elem_a : P_a.Iterator_Ptr := P_a.Ptr (Ptr.a_it); -- ? (checkme)
Ptr_elem_b : P_b.Iterator_Ptr := P_b.Ptr (Ptr.b_it); -- ?
begin
-- Exhaust b first, then a...
Ptr.b_it.Ptr := Ptr.b_it.Next (Ptr.b_it.Ptr);
if Has_Element_MyEnum (Ptr.b_it.Ptr) then
Ptr.Value.b := Ptr.b_it.Ptr.Value.b;
else
-- Exhausted b: reset it, and iterate a by one element
Ptr.b_it.Ptr := Ptr.b_it.First;
Ptr_elem_a := P_a.Next(Ptr.a_it, Ptr_elem_a);
if P_a.Has_Element (Ptr_elem_a) then
Ptr.Value.a := P_a.Elem_Value (Ptr.a_val, Ptr_elem_a);
function Next (Item : ASN1_MySeq_It;
Ptr : Cursor) return Cursor is
pragma Unreferenced (Item);
Ptr_elem_a : P_a.Cursor := P_a.Ptr (Ptr.a_it);
Ptr_elem_b : P_b.Cursor := P_b.Ptr (Ptr.b_it);
begin
-- Exhaust b first, then a...
Ptr.b_it.Ptr := Ptr.b_it.Next (Ptr.b_it.Ptr);
if P_b.Has_Element (Ptr_elem_b) then
Ptr.Value.b := P_b.Elem_Value (Ptr.b_val, Ptr_elem_b);
else
-- Exhausted b: reset it, and iterate a by one element
Ptr.b_it.Ptr := Ptr.b_it.First;
Ptr_elem_a := P_a.Next(Ptr.a_it, Ptr_elem_a);