2025년 3월 27일부터 AOSP를 빌드하고 기여하려면 aosp-main
대신 android-latest-release
를 사용하는 것이 좋습니다. 자세한 내용은 AOSP 변경사항을 참고하세요.
안전한 공용체
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
HIDL의 safe_union
은 태그가 명시적으로 지정된 공용체 형식을 나타냅니다.
이는 safe_union
이 기본 형식을 추적하고 자바와 호환된다는 지원한다는 점을 제외하고는 union
과 유사합니다. 새 기기 및 업그레이드된 기기의 경우 Android 10 이상에서 safe_union
형식을 사용할 수 있습니다.
구문
safe_union
은 HIDL에서 정확히 union
또는 struct
처럼 표현됩니다.
safe_union MySafeUnion {
TypeA a;
TypeB b;
...
};
사용
런타임에서 safe_union
은 하나의 형식일 뿐입니다. 기본적으로 이 형식이 공용체의 첫 번째 형식이 됩니다. 예를 들어 위의 MySafeUnion
은 기본적으로 TypeA
입니다.
hidl-gen
은 C++ 및 자바에서 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;
};
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 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```"]]