Index: src/natools-references.adb ================================================================== --- src/natools-references.adb +++ src/natools-references.adb @@ -20,19 +20,19 @@ --------------------------------- -- Low-level memory management -- --------------------------------- - overriding procedure Adjust (Object : in out Reference) is + overriding procedure Adjust (Object : in out Immutable_Reference) is begin if Object.Count /= null then Object.Count.all := Object.Count.all + 1; end if; end Adjust; - overriding procedure Finalize (Object : in out Reference) is + overriding procedure Finalize (Object : in out Immutable_Reference) is procedure Free is new Ada.Unchecked_Deallocation (Held_Data, Data_Access); procedure Free is new Ada.Unchecked_Deallocation (Counter, Counter_Access); begin @@ -55,41 +55,41 @@ -- Object construction and destruction -- ----------------------------------------- function Create (Constructor : not null access function return Held_Data) - return Reference is + return Immutable_Reference is begin return (Ada.Finalization.Controlled with Data => new Held_Data'(Constructor.all), Count => new Counter'(1)); end Create; procedure Replace - (Ref : in out Reference; + (Ref : in out Immutable_Reference; Constructor : not null access function return Held_Data) is begin Finalize (Ref); Ref.Data := new Held_Data'(Constructor.all); Ref.Count := new Counter'(1); end Replace; - procedure Reset (Ref : in out Reference) is + procedure Reset (Ref : in out Immutable_Reference) is begin Finalize (Ref); end Reset; - function Is_Empty (Ref : Reference) return Boolean is + function Is_Empty (Ref : Immutable_Reference) return Boolean is begin return Ref.Count = null; end Is_Empty; - function "=" (Left, Right : Reference) return Boolean is + function "=" (Left, Right : Immutable_Reference) return Boolean is begin return Left.Data = Right.Data; end "="; @@ -96,11 +96,11 @@ ---------------------- -- Dereferenciation -- ---------------------- - function Query (Ref : in Reference) return Accessor is + function Query (Ref : in Immutable_Reference) return Accessor is begin return Accessor'(Data => Ref.Data, Parent => Ref); end Query; @@ -109,11 +109,11 @@ return Mutator'(Data => Ref.Data, Parent => Ref); end Update; procedure Query - (Ref : in Reference; + (Ref : in Immutable_Reference; Process : not null access procedure (Object : in Held_Data)) is begin Process.all (Ref.Data.all); end Query; Index: src/natools-references.ads ================================================================== --- src/natools-references.ads +++ src/natools-references.ads @@ -31,51 +31,55 @@ Data_Pool : in out System.Storage_Pools.Root_Storage_Pool'Class; package Natools.References is pragma Preelaborate (References); - type Reference is new Ada.Finalization.Controlled with private; - type Accessor (Data : not null access constant Held_Data) is limited private; type Mutator (Data : not null access Held_Data) is limited private; + type Immutable_Reference is new Ada.Finalization.Controlled with private; + function Create (Constructor : not null access function return Held_Data) - return Reference; + return Immutable_Reference; -- Create a new held object and return a reference to it procedure Replace - (Ref : in out Reference; + (Ref : in out Immutable_Reference; Constructor : not null access function return Held_Data); -- Replace the object held in Ref with a newly created object - procedure Reset (Ref : in out Reference); + procedure Reset (Ref : in out Immutable_Reference); -- Empty Ref - function Is_Empty (Ref : Reference) return Boolean; + function Is_Empty (Ref : Immutable_Reference) return Boolean; -- Check whether Ref refers to an actual object - function "=" (Left, Right : Reference) return Boolean; + function "=" (Left, Right : Immutable_Reference) return Boolean; -- Check whether Left and Right refer to the same object - - function Query (Ref : in Reference) return Accessor; + function Query (Ref : in Immutable_Reference) return Accessor; pragma Inline (Query); -- Return a derefenciable constant access to the held object - function Update (Ref : in Reference) return Mutator; - pragma Inline (Update); - -- Return a derefenciable mutable access to the held object - procedure Query - (Ref : in Reference; + (Ref : in Immutable_Reference; Process : not null access procedure (Object : in Held_Data)); -- Call Process with the held object + Null_Immutable_Reference : constant Immutable_Reference; + + + type Reference is new Immutable_Reference with private; + + function Update (Ref : in Reference) return Mutator; + pragma Inline (Update); + -- Return a ereferenciable mutable access to the held object + procedure Update (Ref : in Reference; Process : not null access procedure (Object : in out Held_Data)); -- Call Process with the held object @@ -89,28 +93,33 @@ for Counter_Access'Storage_Pool use Counter_Pool; type Data_Access is access Held_Data; for Data_Access'Storage_Pool use Data_Pool; - type Reference is new Ada.Finalization.Controlled with record + type Immutable_Reference is new Ada.Finalization.Controlled with record Count : Counter_Access := null; Data : Data_Access := null; end record; - overriding procedure Adjust (Object : in out Reference); + overriding procedure Adjust (Object : in out Immutable_Reference); -- Increate reference counter - overriding procedure Finalize (Object : in out Reference); + overriding procedure Finalize (Object : in out Immutable_Reference); -- Decrease reference counter and release memory if needed + + type Reference is new Immutable_Reference with null record; type Accessor (Data : not null access constant Held_Data) is limited record - Parent : Reference; + Parent : Immutable_Reference; end record; type Mutator (Data : not null access Held_Data) is limited record Parent : Reference; end record; - Null_Reference : constant Reference + Null_Immutable_Reference : constant Immutable_Reference := (Ada.Finalization.Controlled with Count => null, Data => null); + Null_Reference : constant Reference + := (Null_Immutable_Reference with null record); + end Natools.References; Index: src/natools-s_expressions-atom_buffers.adb ================================================================== --- src/natools-s_expressions-atom_buffers.adb +++ src/natools-s_expressions-atom_buffers.adb @@ -141,11 +141,16 @@ begin return Null_Atom; end Create; begin if Buffer.Ref.Is_Empty then - return Atom_Refs.Create (Create'Access).Query; + declare + Tmp_Ref : constant Atom_Refs.Reference + := Atom_Refs.Create (Create'Access); + begin + return Tmp_Ref.Query; + end; else return Buffer.Ref.Query; end if; end Raw_Query;