Kể từ ngày 27 tháng 3 năm 2025, bạn nên sử dụng android-latest-release
thay vì aosp-main
để xây dựng và đóng góp cho AOSP. Để biết thêm thông tin, hãy xem phần Thay đổi đối với AOSP.
Liên kết an toàn
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
safe_union
trong HIDL đại diện cho một loại liên kết được gắn thẻ rõ ràng.
Phương thức này tương tự như union
, ngoại trừ safe_union
theo dõi loại cơ bản và tương thích với Java. Loại safe_union
có trong Android 10 trở lên cho các thiết bị mới và được nâng cấp.
Cú pháp
safe_union
được biểu thị trong HIDL giống hệt như union
hoặc struct
.
safe_union MySafeUnion {
TypeA a;
TypeB b;
...
};
Cách sử dụng
Trong thời gian chạy, safe_union
chỉ có một loại. Theo mặc định, đây là loại đầu tiên trong liên kết. Ví dụ: ở trên, MySafeUnion
theo mặc định là TypeA
.
hidl-gen
tạo một lớp hoặc cấu trúc tuỳ chỉnh cho safe_union
trong cả C++ và Java. Lớp này bao gồm một giá trị phân biệt cho mỗi thành viên (trong hidl_discriminator
), một phương thức để lấy giá trị phân biệt hiện tại (getDiscriminator
) cũng như các phương thức setter và getter cho mỗi thành viên. Mỗi phương thức setter và getter được đặt tên chính xác như thành viên của phương thức đó.
Ví dụ: phương thức getter cho TypeA a
được gọi là "a" và phương thức này sẽ trả về một giá trị của TypeA
. Phương thức setter tương ứng cũng được gọi là "a" và lấy tham số là TypeA
. Việc đặt giá trị trong safe_union
sẽ cập nhật giá trị của giá trị phân biệt do getDiscriminator
trả về. Việc truy cập vào một giá trị từ giá trị phân biệt không phải là giá trị phân biệt hiện tại sẽ huỷ bỏ chương trình. Ví dụ: nếu lệnh gọi getDiscriminator
trên một thực thể của MySafeUnion
trả về hidl_discriminator::b
, thì việc cố gắng truy xuất a
sẽ huỷ bỏ chương trình.
Trạng thái đơn
safe_union
luôn có giá trị, nhưng nếu bạn không muốn giá trị, hãy sử dụng android.hidl.safe_union@1.0::Monostate
làm phần giữ chỗ. Ví dụ: tổ hợp sau đây có thể là noinit
(trống) hoặc foo
:
import android.hidl.safe_union@1.0::Monostate;
safe_union OptionalFoo {
Monostate noinit;
Foo foo;
};
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],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```"]]