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; |
|||
function Get_Data return Integer; |
|||
function Get_Data(Item : Singleton) return Integer; |
|||
function Create return Singleton; |
|||
private |
private |
||
protected |
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 ( |
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 |
function Get_Data return Integer is |
||
begin |
begin |
||
return Instance.Get; |
return Instance.Get; |
||
end Get_Data; |
end Get_Data; |
||
------------ |
-------------- |
||
-- |
-- Instance -- |
||
------------ |
-------------- |
||
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 |
end Instance; |
||
end Protected_Singleton;</ada> |
end Protected_Singleton;</ada> |