ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
สหภาพแบบปลอดภัย
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
safe_union
ใน HIDL แสดงถึงประเภทยูเนียนที่ติดแท็กไว้อย่างชัดเจน
ซึ่งคล้ายกับ union
ยกเว้น safe_union
จะติดตามประเภทพื้นฐานและเข้ากันได้กับ Java ประเภท safe_union
พร้อมใช้งานใน Android 10 ขึ้นไปสำหรับอุปกรณ์ใหม่และอุปกรณ์ที่อัปเกรด
วากยสัมพันธ์
safe_union
จะแสดงใน HIDL เหมือนกับ union
หรือ struct
ทุกประการ
safe_union MySafeUnion {
TypeA a;
TypeB b;
...
};
การใช้งาน
safe_union
จะมีเพียง 1 ประเภทเท่านั้นที่รันไทม์ โดยค่าเริ่มต้น จะเป็นประเภทแรกในยูเนียน ตัวอย่างเช่น ด้านบน MySafeUnion
คือ TypeA
โดยค่าเริ่มต้น
hidl-gen
สร้างคลาสหรือโครงสร้างที่กำหนดเองสำหรับ
safe_union
ทั้งใน C++ และ Java คลาสนี้มีตัวแบ่งสำหรับสมาชิกแต่ละคน (ใน hidl_discriminator
) วิธีการรับตัวแบ่งปัจจุบัน (getDiscriminator
) รวมถึงตัวตั้งค่าและตัวรับสำหรับสมาชิกแต่ละคน โดยชื่อของ setter และ getter แต่ละรายการจะเหมือนกับชื่อของสมาชิก
เช่น Getter สําหรับ TypeA a
จะเรียกว่า "a" และแสดงผล TypeA
ตัวตั้งค่าที่เกี่ยวข้องจะเรียกว่า "a" และรับพารามิเตอร์ TypeA
ด้วย การตั้งค่าใน safe_union
จะอัปเดตค่าของตัวระบุตามข้อมูลที่ getDiscriminator
แสดง การเข้าถึงค่าจากตัวระบุที่ไม่ใช่ตัวระบุปัจจุบันจะหยุดโปรแกรม ตัวอย่างเช่น หากการเรียก getDiscriminator
ในอินสแตนซ์ของ MySafeUnion
แสดงผลเป็น hidl_discriminator::b
การพยายามดึงข้อมูล a
จะยกเลิกโปรแกรม
สถานะเดียว
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 UTC
[[["เข้าใจง่าย","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 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```"]]