Singleton: Difference between revisions

410 bytes removed ,  16 years ago
m ((D) header reorder & double checked lock)
Line 5:
<ada>
package Global_Singleton is
procedure Set_Data(Value : Integer);
type Singleton is limited private;
procedurefunction Set_Data(ItemGet_Data : out Singleton; Value :return Integer);
function Get_Data(Item : Singleton) return Integer;
function Create return Singleton;
private
type Instance_Type is record
Line 14 ⟶ 12:
Data : Integer := 0;
end record;
typeInstance Singleton is access all: Instance_Type;
Instance : aliased Instance_Type;
end Global_Singleton;</ada>
 
Line 24 ⟶ 21:
--------------
 
procedure Set_Data (Item : out Singleton; Value : Integer) is
begin
if ItemInstance.Data := null thenValue;
Item := Create;
end if;
Item.Data := Value;
end Set_Data;
 
Line 36 ⟶ 30:
--------------
 
function Get_Data (Item : Singleton) return Integer is
begin
return ItemInstance.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===
<ada>package Protected_Singleton is
Anonymous user