Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Enum Case Name
- ```swift
- SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
- const char *swift_EnumCaseName(OpaqueValue *value, const Metadata *type) {
- // Build a magic mirror. Unconditionally destroy the value at the end.
- const Metadata *mirrorType;
- const OpaqueValue *cMirrorValue;
- std::tie(mirrorType, cMirrorValue) = unwrapExistential(type, value);
- OpaqueValue *mirrorValue = const_cast<OpaqueValue*>(cMirrorValue);
- Mirror mirror;
- bool take = mirrorValue == value;
- ::new (&mirror) MagicMirror(mirrorValue, mirrorType, take);
- MagicMirror *theMirror = reinterpret_cast<MagicMirror *>(&mirror);
- MagicMirrorData data = theMirror->Data;
- const char *result = swift_EnumMirror_caseName(data.Owner, data.Value, data.Type);
- return result;
- }
- ```
- ```swift
- SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
- const char *swift_EnumMirror_caseName(HeapObject *owner,
- const OpaqueValue *value,
- const Metadata *type) {
- if (!isEnumReflectable(type)) {
- swift_release(owner);
- return nullptr;
- }
- const auto Enum = static_cast<const EnumMetadata *>(type);
- const auto &Description = Enum->Description->Enum;
- unsigned tag;
- getEnumMirrorInfo(value, type, &tag, nullptr, nullptr);
- swift_release(owner);
- return getFieldName(Description.CaseNames, tag);
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement