我試圖用受保護的對象包裝散列映射,以便可以通過多個任務來訪問它。我希望受保護類型的過程可用,但將散列映射和元素記錄定義移動到軟件包專用部分會很好。從Ada的受保護對象訪問私有類型
示例代碼在這裏:
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;
我編輯了自己的冠軍。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –
我想現在它不像ada相關那麼明顯。尤其是在排名前15的投票標題爲「ada」的問題中,只有一個在標題中沒有Ada。 –
我會照顧的。 –