Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. ## Enum Case Name
  2.  
  3. ```swift
  4. SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
  5. const char *swift_EnumCaseName(OpaqueValue *value, const Metadata *type) {
  6. // Build a magic mirror. Unconditionally destroy the value at the end.
  7. const Metadata *mirrorType;
  8. const OpaqueValue *cMirrorValue;
  9. std::tie(mirrorType, cMirrorValue) = unwrapExistential(type, value);
  10.  
  11. OpaqueValue *mirrorValue = const_cast<OpaqueValue*>(cMirrorValue);
  12. Mirror mirror;
  13.  
  14. bool take = mirrorValue == value;
  15. ::new (&mirror) MagicMirror(mirrorValue, mirrorType, take);
  16.  
  17. MagicMirror *theMirror = reinterpret_cast<MagicMirror *>(&mirror);
  18. MagicMirrorData data = theMirror->Data;
  19. const char *result = swift_EnumMirror_caseName(data.Owner, data.Value, data.Type);
  20. return result;
  21. }
  22. ```
  23.  
  24. ```swift
  25. SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
  26. const char *swift_EnumMirror_caseName(HeapObject *owner,
  27. const OpaqueValue *value,
  28. const Metadata *type) {
  29. if (!isEnumReflectable(type)) {
  30. swift_release(owner);
  31. return nullptr;
  32. }
  33.  
  34. const auto Enum = static_cast<const EnumMetadata *>(type);
  35. const auto &Description = Enum->Description->Enum;
  36.  
  37. unsigned tag;
  38. getEnumMirrorInfo(value, type, &tag, nullptr, nullptr);
  39.  
  40. swift_release(owner);
  41.  
  42. return getFieldName(Description.CaseNames, tag);
  43. }
  44. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement