BuiltIns.swift 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // SwiftyUserDefaults
  3. //
  4. // Copyright (c) 2015-present Radosław Pietruszewski, Łukasz Mróz
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in all
  14. // copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. //
  24. import Foundation
  25. extension DefaultsSerializable {
  26. public static var _defaultsArray: DefaultsArrayBridge<[T]> { return DefaultsArrayBridge() }
  27. }
  28. extension Date: DefaultsSerializable {
  29. public static var _defaults: DefaultsObjectBridge<Date> { return DefaultsObjectBridge() }
  30. }
  31. extension String: DefaultsSerializable {
  32. public static var _defaults: DefaultsStringBridge { return DefaultsStringBridge() }
  33. }
  34. extension Int: DefaultsSerializable {
  35. public static var _defaults: DefaultsIntBridge { return DefaultsIntBridge() }
  36. }
  37. extension Double: DefaultsSerializable {
  38. public static var _defaults: DefaultsDoubleBridge { return DefaultsDoubleBridge() }
  39. }
  40. extension Bool: DefaultsSerializable {
  41. public static var _defaults: DefaultsBoolBridge { return DefaultsBoolBridge() }
  42. }
  43. extension Data: DefaultsSerializable {
  44. public static var _defaults: DefaultsDataBridge { return DefaultsDataBridge() }
  45. }
  46. extension URL: DefaultsSerializable {
  47. #if os(Linux)
  48. public static var _defaults: DefaultsKeyedArchiverBridge<URL> { return DefaultsKeyedArchiverBridge() }
  49. #else
  50. public static var _defaults: DefaultsUrlBridge { return DefaultsUrlBridge() }
  51. #endif
  52. public static var _defaultsArray: DefaultsKeyedArchiverBridge<[URL]> { return DefaultsKeyedArchiverBridge() }
  53. }
  54. extension DefaultsSerializable where Self: Codable {
  55. public static var _defaults: DefaultsCodableBridge<Self> { return DefaultsCodableBridge() }
  56. public static var _defaultsArray: DefaultsCodableBridge<[Self]> { return DefaultsCodableBridge() }
  57. }
  58. extension DefaultsSerializable where Self: RawRepresentable {
  59. public static var _defaults: DefaultsRawRepresentableBridge<Self> { return DefaultsRawRepresentableBridge() }
  60. public static var _defaultsArray: DefaultsRawRepresentableArrayBridge<[Self]> { return DefaultsRawRepresentableArrayBridge() }
  61. }
  62. extension DefaultsSerializable where Self: NSCoding {
  63. public static var _defaults: DefaultsKeyedArchiverBridge<Self> { return DefaultsKeyedArchiverBridge() }
  64. public static var _defaultsArray: DefaultsKeyedArchiverBridge<[Self]> { return DefaultsKeyedArchiverBridge() }
  65. }
  66. extension Dictionary: DefaultsSerializable where Key == String {
  67. public typealias T = [Key: Value]
  68. public typealias Bridge = DefaultsObjectBridge<T>
  69. public typealias ArrayBridge = DefaultsArrayBridge<[T]>
  70. public static var _defaults: Bridge { return Bridge() }
  71. public static var _defaultsArray: ArrayBridge { return ArrayBridge() }
  72. }
  73. extension Array: DefaultsSerializable where Element: DefaultsSerializable {
  74. public typealias T = [Element.T]
  75. public typealias Bridge = Element.ArrayBridge
  76. public typealias ArrayBridge = DefaultsObjectBridge<[T]>
  77. public static var _defaults: Bridge {
  78. return Element._defaultsArray
  79. }
  80. public static var _defaultsArray: ArrayBridge {
  81. fatalError("Multidimensional arrays are not supported yet")
  82. }
  83. }
  84. extension Optional: DefaultsSerializable where Wrapped: DefaultsSerializable {
  85. public typealias Bridge = DefaultsOptionalBridge<Wrapped.Bridge>
  86. public typealias ArrayBridge = DefaultsOptionalBridge<Wrapped.ArrayBridge>
  87. public static var _defaults: DefaultsOptionalBridge<Wrapped.Bridge> { return DefaultsOptionalBridge(bridge: Wrapped._defaults) }
  88. public static var _defaultsArray: DefaultsOptionalBridge<Wrapped.ArrayBridge> { return DefaultsOptionalBridge(bridge: Wrapped._defaultsArray) }
  89. }