我正在尝试用受保护的对象包装散列映射,以便可以通过多个任务访问它。我希望受保护类型的过程可用,但将哈希映射和元素记录定义移动到包私有部分会很好。
这里的示例代码:
package Thing_Protected is
type Thing_Info is record
Key : Ada.Strings.Unbounded.Unbounded_String;
Counter_Value : Natural := 0;
end record;
package Thing_Info_Maps is new Ada.Containers.Hashed_Maps
(Key_Type => Ada.Strings.Unbounded.Unbounded_String,
Element_Type => Thing_Info,
Hash => Ada.Strings.Unbounded.Hash,
Equivalent_Keys => Ada.Strings.Unbounded."=");
protected type Thing is
procedure Increment (Key : String);
procedure Another_Thing (Key : String);
private
Thing_Map : Thing_Info_Maps.Map;
end Thing;
private
-- move Thing_Info, Thing_info_maps into here.
end Thing_Protected;
我尝试将 Thing_Info 定义为私有类型。但我不确定如何将 Thing_Info_Maps 包定义为私有但仍从受保护的对象类型访问它。
所以我真的找不到试图找到一种方法来获得这样的东西:
package Thing_Protected is
type Thing_Info is private;
package Thing_Info_Maps is private;
protected type Thing is
procedure Increment (Key : String);
procedure Another_Thing (Key : String);
private
Thing_Map : Thing_Info_Maps.Map; -- <<- how would we know about .Map??
end Thing;
private
type Thing_Info is record
Key : Ada.Strings.Unbounded.Unbounded_String;
Counter_Value : Natural := 0;
end record;
package Thing_Info_Maps is new Ada.Containers.Hashed_Maps
(Key_Type => Ada.Strings.Unbounded.Unbounded_String,
Element_Type => Thing_Info,
Hash => Ada.Strings.Unbounded.Hash,
Equivalent_Keys => Ada.Strings.Unbounded."=");
end Thing_Protected;