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; |
|||
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; |
||
Instance : Instance_Type; |
|||
Instance : aliased Instance_Type; |
|||
end Global_Singleton;</ada> |
end Global_Singleton;</ada> |
||
Line 24: | Line 21: | ||
-------------- |
-------------- |
||
procedure Set_Data ( |
procedure Set_Data (Value : Integer) is |
||
begin |
begin |
||
Instance.Data := Value; |
|||
Item := Create; |
|||
end if; |
|||
Item.Data := Value; |
|||
end Set_Data; |
end Set_Data; |
||
Line 36: | Line 30: | ||
-------------- |
-------------- |
||
function Get_Data |
function Get_Data return Integer is |
||
begin |
begin |
||
return |
return Instance.Data; |
||
end Get_Data; |
end Get_Data; |
||
⚫ | |||
------------ |
|||
-- Create -- |
|||
------------ |
|||
function Create return Singleton is |
|||
begin |
|||
return Instance'access; |
|||
end Create; |
|||
⚫ | |||
===Thread Safe=== |
===Thread Safe=== |
||
<ada>package Protected_Singleton is |
<ada>package Protected_Singleton is |