Commit 1f8180b4 authored by Maxime Perrotin's avatar Maxime Perrotin

Create a Generic_Simple iterator for SeqOf

parent 28e95302
......@@ -4,7 +4,6 @@ with Ada.Finalization;
use Ada.Finalization;
with ASN1_Iterators.Generic_Basic;
with ASN1_Iterators.SimpleTypes;
generic
with package P is new Generic_Basic (<>);
......
package body ASN1_Iterators.Generic_SeqOf is
procedure Initialize (self: in out ASN1_Variable_SeqOf) is
procedure Initialize (Self: in out ASN1_Variable_SeqOf) is
begin
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.Ptr := self.RestIt.First;
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.Ptr := Self.RestIt.First;
end if;
end;
function Has_Element_Variable_SeqOf (Ptr: ASN1_Variable_SeqOf_Ptr)
return Boolean is (Length_Pkg.Has_Element(Length_Pkg.Ptr(Ptr.LenIt)));
function Has_Element_Variable_SeqOf (Ptr : ASN1_Variable_SeqOf_Ptr)
return Boolean is (Length_Pkg.Has_Element (Length_Pkg.Ptr (Ptr.LenIt)));
function Iterate (self : ASN1_Variable_SeqOf)
function Iterate (Self : ASN1_Variable_SeqOf)
return Iterators_Variable_SeqOf.Forward_Iterator'Class is
begin
return I: ASN1_Variable_SeqOf_It do
......@@ -26,7 +26,7 @@ package body ASN1_Iterators.Generic_SeqOf is
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.LenIt := Length_Pkg.Iterator (Item.Ptr.LenVal.Iterate);
-- 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) :=
......@@ -62,5 +62,34 @@ package body ASN1_Iterators.Generic_SeqOf is
end if;
return Ptr;
end;
-- Interface functions for the generic Seqof to act as a SimpleType
function Simple_Init return Instance is
Initial_Value : Instance;
begin
Initialize (Initial_Value);
return Initial_Value;
end Simple_Init;
function Simple_Has_Elem (Value : Instance) return Boolean is
Val : aliased Instance := Value;
begin
return Has_Element_Variable_SeqOf (Val'Unchecked_Access);
end Simple_Has_Elem;
function Simple_First return Instance is
It : ASN1_Variable_SeqOf_It;
Res : aliased Instance;
begin
It.Ptr := Res'Unchecked_Access;
return It.First.all;
end Simple_First;
function Simple_Next (Value : Instance) return Instance is
Val : aliased Instance := Value;
It : ASN1_Variable_SeqOf_It := (Ptr => Val'Unchecked_Access);
begin
return It.Next (Val'Unchecked_Access).all;
end Simple_Next;
end;
......@@ -5,7 +5,8 @@ use Ada.Finalization;
with ASN1_Iterators.Generic_Fixed_SeqOf,
ASN1_Iterators.Generic_Integer,
ASN1_Iterators.Generic_Basic;
ASN1_Iterators.Generic_Basic,
ASN1_Iterators.SimpleTypes;
with Interfaces;
use Interfaces;
......@@ -26,8 +27,7 @@ package ASN1_Iterators.Generic_SeqOf is
type ASN1_Variable_SeqOf_Ptr is private;
subtype MySeqOf is P.SeqOf(Max);
type ASN1_Variable_SeqOf is new Controlled
with record
type ASN1_Variable_SeqOf is new Controlled with record
Length : Natural := Min;
Value : MySeqOf; -- P.SeqOf(Max);
-- Iterate on the length
......@@ -42,7 +42,7 @@ package ASN1_Iterators.Generic_SeqOf is
Constant_Indexing => Element_Variable_SeqOf_Value;
-- Constructor (called automatically)
procedure Initialize(self: in out ASN1_Variable_SeqOf);
procedure Initialize (Self: in out ASN1_Variable_SeqOf);
function Has_Element_Variable_SeqOf (Ptr : ASN1_Variable_SeqOf_Ptr)
return Boolean;
......@@ -51,8 +51,8 @@ package ASN1_Iterators.Generic_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
type ASN1_Variable_SeqOf_It is
new Iterators_Variable_SeqOf.Forward_Iterator with record
Ptr : ASN1_Variable_SeqOf_Ptr;
end record;
......@@ -65,16 +65,31 @@ package ASN1_Iterators.Generic_SeqOf is
Ptr : ASN1_Variable_SeqOf_Ptr)
return ASN1_Variable_SeqOf_Ptr;
function Iterate (self : ASN1_Variable_SeqOf)
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);
return MySeqOf is (Self.Value);
subtype Instance is ASN1_Variable_SeqOf;
-- Export as a simple type, to enable iteration such as Seq of SeqOf
-- (since Generic_SeqOf takes a Generic_Basic as parameter)
-- NOTE : this compiles but I have not tested it yet...
function Simple_Init return Instance;
function Simple_Has_Elem (Value : Instance) return Boolean;
function Simple_First return Instance;
function Simple_Next (Value : Instance) return Instance;
package Simple_Generic_SeqOf is new SimpleTypes
(Sort => Instance,
Elem_Init => Simple_Init,
Has_Elem => Simple_Has_Elem,
Elem_First => Simple_First,
Elem_Next => Simple_Next);
package It is new Generic_Basic (P => Simple_Generic_SeqOf);
subtype Generic_Instance is It.Basic_ASN1_Iterator;
private
type ASN1_Variable_SeqOf_Ptr is access all ASN1_Variable_SeqOf;
......
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