Singleton: Difference between revisions

Content added Content deleted
m ((D) header reorder & double checked lock)
Line 5: Line 5:
<ada>
<ada>
package Global_Singleton is
package Global_Singleton is
procedure Set_Data(Value : Integer);
type Singleton is limited private;
procedure Set_Data(Item : out Singleton; Value : Integer);
function Get_Data return Integer;
function Get_Data(Item : Singleton) return Integer;
function Create return Singleton;
private
private
type Instance_Type is record
type Instance_Type is record
Line 14: Line 12:
Data : Integer := 0;
Data : Integer := 0;
end record;
end record;
type Singleton is access all Instance_Type;
Instance : Instance_Type;
Instance : aliased Instance_Type;
end Global_Singleton;</ada>
end Global_Singleton;</ada>


Line 24: Line 21:
--------------
--------------


procedure Set_Data (Item : out Singleton; Value : Integer) is
procedure Set_Data (Value : Integer) is
begin
begin
if Item = null then
Instance.Data := Value;
Item := Create;
end if;
Item.Data := Value;
end Set_Data;
end Set_Data;


Line 36: Line 30:
--------------
--------------


function Get_Data (Item : Singleton) return Integer is
function Get_Data return Integer is
begin
begin
return Item.Data;
return Instance.Data;
end Get_Data;
end Get_Data;


end Global_Singleton;</ada>
------------
-- Create --
------------


function Create return Singleton is
begin
return Instance'access;
end Create;

end Global_Singleton;</ada>
===Thread Safe===
===Thread Safe===
<ada>package Protected_Singleton is
<ada>package Protected_Singleton is