Singleton: Difference between revisions

Content added Content deleted
Line 38: Line 38:
===Thread Safe===
===Thread Safe===
<ada>package Protected_Singleton is
<ada>package Protected_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
protected type Instance_Type is
protected Instance is
procedure Set(Value : Integer);
procedure Set(Value : Integer);
function Get return Integer;
function Get return Integer;
Line 49: Line 47:
Data : Integer := 0;
Data : Integer := 0;
end Instance_Type;
end Instance_Type;
type Singleton is access all Instance_Type;
Instance : aliased Instance_Type;
end Protected_Singleton;</ada>
end Protected_Singleton;</ada>


Line 59: Line 55:
--------------
--------------


procedure Set_Data (Item : out Singleton; Value : Integer) is
procedure Set_Data (Value : Integer) is
begin
begin
if Item = null then
Item := Create;
end if;
Instance.Set(Value);
Instance.Set(Value);
end Set_Data;
end Set_Data;
Line 71: Line 64:
--------------
--------------


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


------------
--------------
-- Create --
-- Instance --
------------
--------------


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

-------------------
-- Instance_Type --
-------------------

protected body Instance_Type is


---------
---------
Line 109: Line 93:
end Get;
end Get;


end Instance_Type;
end Instance;


end Protected_Singleton;</ada>
end Protected_Singleton;</ada>