自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
安全聯合
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
HIDL 中的 safe_union
代表明確標記的聯集類型。這與 union
類似,但 safe_union
會追蹤基礎類型,且與 Java 相容。safe_union
類型適用於 Android 10 以上版本的新裝置和升級裝置。
語法
safe_union
在 HIDL 中的表示方式與 union
或 struct
完全相同。
safe_union MySafeUnion {
TypeA a;
TypeB b;
...
};
用量
在執行階段,safe_union
只有一種型別。根據預設,它是組合中的第一個類型。例如,上述 MySafeUnion
的預設值為 TypeA
。
hidl-gen
會為 C++ 和 Java 中的 safe_union
產生自訂類別或結構體。這個類別包含每個成員 (在 hidl_discriminator
中) 的判別器、用於取得目前判別器 (getDiscriminator
) 的方法,以及每個成員的 setter 和 getter。每個 setter 和 getter 都會以成員的名稱命名。舉例來說,TypeA a
的 getter 稱為「a」,且會傳回 TypeA
的某些內容。對應的 setter 也稱為「a」,並採用 TypeA
參數。在 safe_union
中設定值時,會更新 getDiscriminator
傳回的判別符值。如果從非目前的判別子存取值,程式會中止。舉例來說,如果在 MySafeUnion
例項上呼叫 getDiscriminator
會傳回 hidl_discriminator::b
,那麼嘗試擷取 a
就會中止程式。
Monostate
safe_union
一律會有值,但如果不希望有值,請使用 android.hidl.safe_union@1.0::Monostate
做為預留位置。舉例來說,下列聯集可以是 noinit
(空白) 或 foo
:
import android.hidl.safe_union@1.0::Monostate;
safe_union OptionalFoo {
Monostate noinit;
Foo foo;
};
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Safe union\n\n`safe_union` in HIDL represents an explicitly tagged union type.\nThis is similar to a `union` except `safe_union` keeps\ntrack of the underlying type and is compatible with Java. The\n`safe_union` type is available in Android 10\nand higher for new and upgraded devices.\n\nSyntax\n------\n\nA `safe_union` is expressed in HIDL exactly like a\n`union` or `struct`. \n\n```scdoc\nsafe_union MySafeUnion {\n TypeA a;\n TypeB b;\n ...\n};\n```\n\nUsage\n-----\n\nAt runtime, a `safe_union` is only ever one type. By default, it's\nthe first type in the union. For instance, above,\n`MySafeUnion` is by default `TypeA`.\n\n`hidl-gen` generates a custom class or struct for a\n`safe_union` in both C++ and Java. This class includes a\ndiscriminator for each member (in `hidl_discriminator`), a method to\nget the current discriminator (`getDiscriminator`), and setters and\ngetters for each member. Each setter and getter is named exactly as its member.\nFor instance, the getter for `TypeA a` is called \"a\", and it\nreturns something of `TypeA`. The corresponding setter is also\nbe called \"a\" and takes a parameter of `TypeA`. Setting the value in\na `safe_union` updates the value of the discriminator as\nreturned by `getDiscriminator`. Accessing a value from a\ndiscriminator that isn't the current discriminator aborts the program. For\ninstance, if calling `getDiscriminator` on an instance of\n`MySafeUnion` returns `hidl_discriminator::b`, then\ntrying to retrieve `a` aborts the program.\n\nMonostate\n---------\n\nA `safe_union` always has a value, but if it is desired to not\nhave a value, use `android.hidl.safe_union@1.0::Monostate` as a\nplaceholder. For instance, the following union can either be\n`noinit` (empty) or `foo`: \n\n```python\nimport android.hidl.safe_union@1.0::Monostate;\n\nsafe_union OptionalFoo {\n Monostate noinit;\n Foo foo;\n};\n```"]]