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

Code reformat and refactoring

parent dc4c6e87
......@@ -7,17 +7,17 @@ generic
with package P is new SimpleTypes (<>);
package ASN1_Iterators.Generic_Basic is
-- Provides an iterator for a basic type
subtype Element is P.Element;
subtype Sort is P.Sort;
type Basic_ASN1_Iterator is new Controlled
with record
Value : P.SimpleType;
end record
with Default_Iterator => Iterate,
Iterator_Element => P.Element,
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;
......@@ -43,5 +43,5 @@ package ASN1_Iterators.Generic_Basic is
function Elem_Value (self : Basic_ASN1_Iterator;
Ptr : Iterator_Ptr)
return P.Element is (Ptr.Value.Element_Value);
return P.Sort is (Ptr.Value.Element_Value);
end;
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;
......@@ -9,6 +12,7 @@ package body ASN1_Iterators.Generic_Enumerated is
function Has_Elem (Value: Sort) return Boolean is
Result : constant Boolean := (not Is_Last and Value <= Sort'Last);
begin
Put_Line ("Value: " & Value'Img & " Has_Elem = " & Result'Img);
if Value = Sort'Last then
Is_Last := True;
end if;
......
......@@ -11,16 +11,14 @@ package ASN1_Iterators.Generic_Enumerated is
-- keep a flag set to one when the iteration is finished
Is_Last : Boolean := False;
-- function Elem_Init return Sort is (Sort'First);
function Elem_Init return Sort;
function Has_Elem (Value: Sort) return Boolean;
-- function Has_Elem (Value: Sort) return Boolean is (Value <= Sort'Last);
function Elem_First return Sort is (Elem_Init); -- Sort'First);
function Elem_Next (Value: Sort) return Sort is
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));
package Discrete_Type is new SimpleTypes (Element => Sort,
package Discrete_Type is new SimpleTypes (Sort => Sort,
Elem_Init => Elem_Init,
Has_Elem => Has_Elem,
Elem_First => Elem_First,
......
......@@ -2,71 +2,71 @@ with Ada.Unchecked_Conversion;
package body ASN1_Iterators.Generic_Fixed_SeqOf is
procedure Initialize (self: in out ASN1_SeqOf) is
begin
self.value.length := self.length;
if self.length > 1 then
self.Rest := new ASN1_SeqOf(self.Length - 1);
self.RestIt := new ASN1_SeqOf_It'(ASN1_SeqOf_It(self.Rest.Iterate));
self.RestIt.Ptr := self.RestIt.First;
procedure Initialize (Self: in out ASN1_SeqOf) is
begin
Self.Value.Length := Self.Length;
if Self.Length > 1 then
Self.Rest := new ASN1_SeqOf (self.Length - 1);
Self.RestIt := new ASN1_SeqOf_It'(ASN1_SeqOf_It (Self.Rest.Iterate));
Self.RestIt.Ptr := Self.RestIt.First;
end if;
end;
end;
function Has_Element_SeqOf (Ptr: ASN1_SeqOf_Ptr) return Boolean is
(P.Has_Element(P.Ptr(Ptr.FirstIt)));
function Has_Element_SeqOf (Ptr: ASN1_SeqOf_Ptr) return Boolean is
(P.Has_Element(P.Ptr(Ptr.FirstIt)));
function Iterate (self : ASN1_SeqOf)
function Iterate (Self : ASN1_SeqOf)
return Iterators_SeqOf.Forward_Iterator'Class is
begin
return I: ASN1_SeqOf_It do
I.Ptr := self'Unrestricted_Access;
end return;
end;
begin
return I: ASN1_SeqOf_It do
I.Ptr := Self'Unrestricted_Access;
end return;
end;
function First (Item : ASN1_SeqOf_It) return ASN1_SeqOf_Ptr is
Ptr_elem : P.Iterator_Ptr := P.Ptr(Item.Ptr.FirstIt);
begin
-- Initialize the iterator (Compute first value)
Item.Ptr.FirstIt := P.Iterator (Item.Ptr.FirstVal.Iterate);
Ptr_elem := P.First (Item.Ptr.FirstIt);
Item.Ptr.Value.Data(1) := P.Elem_Value (Item.Ptr.FirstVal,
function First (Item : ASN1_SeqOf_It) return ASN1_SeqOf_Ptr is
Ptr_Elem : P.Iterator_Ptr := P.Ptr (Item.Ptr.FirstIt);
begin
-- Initialize the iterator (Compute first value)
Item.Ptr.FirstIt := P.Iterator (Item.Ptr.FirstVal.Iterate);
Ptr_Elem := P.First (Item.Ptr.FirstIt);
Item.Ptr.Value.Data (1) := P.Elem_Value (Item.Ptr.FirstVal,
Ptr_elem);
if Item.Ptr.Length > 1 then
Item.Ptr.Value.Data (2 .. Item.Ptr.Length) :=
Item.Ptr.RestIt.Ptr.Value.Data (1 .. Item.Ptr.RestIt.Ptr.Length);
end if;
return Item.Ptr;
end;
if Item.Ptr.Length > 1 then
Item.Ptr.Value.Data (2 .. Item.Ptr.Length) :=
Item.Ptr.RestIt.Ptr.Value.Data (1 .. Item.Ptr.RestIt.Ptr.Length);
end if;
return Item.Ptr;
end;
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);
begin
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);
begin
if Ptr.Length > 1 then
Ptr.RestIt.Ptr := Ptr.RestIt.Next (Ptr.RestIt.Ptr);
if Has_Element_SeqOf (Ptr.RestIt.Ptr) then
Ptr.Value.Data (2..Ptr.Length) :=
Ptr.RestIt.Ptr.Value.Data (1..Ptr.RestIt.Ptr.Value.Length);
else
Ptr.RestIt.Ptr := Ptr.RestIt.First;
-- Exhausted "rest": iterate on the first item
Ptr_elem := P.Next(Ptr.FirstIt, Ptr_elem);
if P.Has_Element (Ptr_elem) then
Ptr.Value.Data(1) := P.Elem_Value(Ptr.FirstVal, Ptr_elem);
Ptr.Value.Data(2..Ptr.Length) := Ptr.RestIt.Ptr.Value.Data (1..Ptr.RestIt.Ptr.Length);
end if;
end if;
else
Ptr.RestIt.Ptr := Ptr.RestIt.Next (Ptr.RestIt.Ptr);
if Has_Element_SeqOf (Ptr.RestIt.Ptr) then
Ptr.Value.Data (2 .. Ptr.Length) :=
Ptr.RestIt.Ptr.Value.Data (1 .. Ptr.RestIt.Ptr.Value.Length);
else
Ptr.RestIt.Ptr := Ptr.RestIt.First;
-- Exhausted "rest": iterate on the first item
Ptr_elem := P.Next(Ptr.FirstIt, Ptr_elem);
if P.Has_Element (Ptr_elem) then
Ptr.Value.Data (1) := P.Elem_Value (Ptr.FirstVal, Ptr_elem);
Ptr.Value.Data (2 .. Ptr.Length) :=
Ptr.RestIt.Ptr.Value.Data (1 .. Ptr.RestIt.Ptr.Length);
end if;
end if;
else
-- Size is 0 or 1
Ptr_elem := P.Next(Ptr.FirstIt, Ptr_elem);
Ptr_elem := P.Next (Ptr.FirstIt, Ptr_elem);
if P.Has_Element (Ptr_elem) then
Ptr.Value.Data(1) := P.Elem_Value(Ptr.FirstVal, Ptr_elem);
Ptr.Value.Data (1) := P.Elem_Value (Ptr.FirstVal, Ptr_elem);
end if;
end if;
return Ptr;
end;
return Ptr;
end;
end;
......@@ -10,16 +10,17 @@ generic
with package P is new Generic_Basic (<>);
package ASN1_Iterators.Generic_Fixed_SeqOf is
type DataArray is array(natural range <>) of P.Element;
type SeqOf (Max: Natural) is record
Length : Integer;
Data : DataArray(1..Max);
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
type ASN1_SeqOf (Size : Natural) is new Controlled
with record
Length : Natural := Size;
Value : SeqOf (Size);
......@@ -37,15 +38,15 @@ package ASN1_Iterators.Generic_Fixed_SeqOf is
type ASN1_SeqOf_Ptr is access all ASN1_SeqOf;
-- Constructor (called automatically)
procedure Initialize(self: in out ASN1_SeqOf);
procedure Initialize (Self : in out ASN1_SeqOf);
function Has_Element_SeqOf (Ptr : ASN1_SeqOf_Ptr) return Boolean;
package Iterators_SeqOf is
new Ada.Iterator_Interfaces (ASN1_SeqOf_Ptr, Has_Element_SeqOf);
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;
Ptr : ASN1_SeqOf_Ptr;
end record;
overriding function First (Item : ASN1_SeqOf_It) return ASN1_SeqOf_Ptr;
......@@ -53,11 +54,11 @@ package ASN1_Iterators.Generic_Fixed_SeqOf is
Ptr : ASN1_SeqOf_Ptr) return ASN1_SeqOf_Ptr;
function Iterate (self : ASN1_SeqOf)
return Iterators_SeqOf.Forward_Iterator'Class;
return Iterators_SeqOf.Forward_Iterator'Class;
function Element_SeqOf_Value (self : ASN1_SeqOf;
function Element_SeqOf_Value (Self : ASN1_SeqOf;
Ptr : ASN1_SeqOf_Ptr)
return SeqOf is (self.Value);
return SeqOf is (self.Value);
end;
......@@ -15,13 +15,14 @@ package ASN1_Iterators.Generic_Integer is
function Elem_Next(Value: Interfaces.Integer_64) return Interfaces.Integer_64 is
(Value + 1);
package Integer_type is new SimpleTypes(Element => 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 => Integer_type);
package It is new Generic_Basic (P => Signed_Integer);
subtype Instance is It.Basic_ASN1_Iterator;
end;
......@@ -12,16 +12,17 @@ package ASN1_Iterators.Generic_Unsigned_Integer is
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_Next(Value: Interfaces.Unsigned_64)
return Interfaces.Unsigned_64 is (Value + 1);
package Integer_type is new SimpleTypes(Element => 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 => Integer_type);
package It is new Generic_Basic (P => Unsigned_Type);
subtype Instance is It.Basic_ASN1_Iterator;
end;
generic
type Element is private;
with function Elem_Init return Element;
with function Has_Elem (Value: Element) return Boolean;
with function Elem_First return Element;
with function Elem_Next (Value: Element) return Element;
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: Element;
type SimpleType is tagged record
Value : Sort;
end record;
procedure Initialize (Self : in out SimpleType);
......@@ -19,6 +18,6 @@ package ASN1_Iterators.SimpleTypes is
procedure Next (Self : in out SimpleType);
function Element_Value (Self : in out SimpleType) return Element is
function Element_Value (Self : in out SimpleType) return Sort is
(Self.Value);
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