AnyExtensions.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 1999-2101 Alibaba Group.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. //
  17. // AnyExtension.swift
  18. // HandyJSON
  19. //
  20. // Created by zhouzhuo on 08/01/2017.
  21. //
  22. protocol AnyExtensions {}
  23. extension AnyExtensions {
  24. public static func isValueTypeOrSubtype(_ value: Any) -> Bool {
  25. return value is Self
  26. }
  27. public static func value(from storage: UnsafeRawPointer) -> Any {
  28. return storage.assumingMemoryBound(to: self).pointee
  29. }
  30. public static func write(_ value: Any, to storage: UnsafeMutableRawPointer) {
  31. guard let this = value as? Self else {
  32. return
  33. }
  34. storage.assumingMemoryBound(to: self).pointee = this
  35. }
  36. public static func takeValue(from anyValue: Any) -> Self? {
  37. return anyValue as? Self
  38. }
  39. }
  40. func extensions(of type: Any.Type) -> AnyExtensions.Type {
  41. struct Extensions : AnyExtensions {}
  42. var extensions: AnyExtensions.Type = Extensions.self
  43. withUnsafePointer(to: &extensions) { pointer in
  44. UnsafeMutableRawPointer(mutating: pointer).assumingMemoryBound(to: Any.Type.self).pointee = type
  45. }
  46. return extensions
  47. }
  48. func extensions(of value: Any) -> AnyExtensions {
  49. struct Extensions : AnyExtensions {}
  50. var extensions: AnyExtensions = Extensions()
  51. withUnsafePointer(to: &extensions) { pointer in
  52. UnsafeMutableRawPointer(mutating: pointer).assumingMemoryBound(to: Any.self).pointee = value
  53. }
  54. return extensions
  55. }
  56. /// Tests if `value` is `type` or a subclass of `type`
  57. func value(_ value: Any, is type: Any.Type) -> Bool {
  58. return extensions(of: type).isValueTypeOrSubtype(value)
  59. }
  60. /// Tests equality of any two existential types
  61. func == (lhs: Any.Type, rhs: Any.Type) -> Bool {
  62. return Metadata(type: lhs) == Metadata(type: rhs)
  63. }
  64. // MARK: AnyExtension + Storage
  65. extension AnyExtensions {
  66. mutating func storage() -> UnsafeRawPointer {
  67. if type(of: self) is AnyClass {
  68. let opaquePointer = Unmanaged.passUnretained(self as AnyObject).toOpaque()
  69. return UnsafeRawPointer(opaquePointer)
  70. } else {
  71. return withUnsafePointer(to: &self) { pointer in
  72. return UnsafeRawPointer(pointer)
  73. }
  74. }
  75. }
  76. }