OSDN Git Service

guard の書き方を統一した
[kcd/KCD.git] / KCD / DefaultKey.swift
1 /*
2   DefaultKey.swift
3
4  Copyright (c) 2017, Hori, Masaki.
5  All rights reserved.
6  
7  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8  
9  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10  
11  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
12  
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  
15  */
16
17 /*
18  
19  DefaultKey.swift
20  
21  CotEditor
22  https://coteditor.com
23  
24  Created by 1024jp on 2016-01-03.
25  
26  ------------------------------------------------------------------------------
27  
28  © 2016-2017 1024jp
29  
30  Licensed under the Apache License, Version 2.0 (the "License");
31  you may not use this file except in compliance with the License.
32  You may obtain a copy of the License at
33  
34  https://www.apache.org/licenses/LICENSE-2.0
35  
36  Unless required by applicable law or agreed to in writing, software
37  distributed under the License is distributed on an "AS IS" BASIS,
38  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39  See the License for the specific language governing permissions and
40  limitations under the License.
41  
42  */
43
44 import Foundation
45
46 class DefaultKeys: RawRepresentable, Hashable, CustomStringConvertible {
47     
48     let rawValue: String
49     
50     
51     required init(rawValue: String) {
52         
53         self.rawValue = rawValue
54     }
55     
56     
57     var hashValue: Int {
58         
59         return self.rawValue.hashValue
60     }
61     
62     
63     var description: String {
64         
65         return self.rawValue
66     }
67     
68 }
69
70 final class DefaultKey<T>: DefaultKeys {
71     
72     private let rawAlternativeValue: T?
73     
74     let regulator: (T) -> T
75     
76     
77     init(_ rawValue: String, alternative value: T? = nil, regulator: @escaping (T) -> T = { $0 }) {
78         
79         self.rawAlternativeValue = value
80         self.regulator = regulator
81         
82         super.init(rawValue: rawValue)
83         
84     }
85     
86     required convenience init(rawValue: String) {
87         
88         self.init(rawValue)
89     }
90     
91     
92     var alternative: T {
93         
94         switch rawAlternativeValue {
95         case .none: fatalError("DefaultKey (\(self)) has no alternative value.")
96         case let .some(value): return value
97         }
98     }
99     
100 }
101
102
103 extension UserDefaults {
104     
105     subscript(key: DefaultKey<Bool>) -> Bool {
106         
107         get { return self.bool(forKey: key.rawValue) }
108         set { self.set(newValue, forKey: key.rawValue) }
109     }
110     
111     
112     subscript(key: DefaultKey<Int>) -> Int {
113         
114         get { return key.regulator(self.integer(forKey: key.rawValue)) }
115         set { self.set(key.regulator(newValue), forKey: key.rawValue) }
116     }
117     
118     
119     subscript(key: DefaultKey<Double>) -> Double {
120         
121         get { return key.regulator(self.double(forKey: key.rawValue)) }
122         set { self.set(key.regulator(newValue), forKey: key.rawValue) }
123     }
124     
125     
126     subscript(key: DefaultKey<CGFloat>) -> CGFloat {
127         
128         get { return key.regulator(CGFloat(self.double(forKey: key.rawValue))) }
129         set { self.set(key.regulator(newValue), forKey: key.rawValue) }
130     }
131     
132     
133     subscript(key: DefaultKey<String?>) -> String? {
134         
135         get { return self.string(forKey: key.rawValue) }
136         set { self.set(newValue, forKey: key.rawValue) }
137     }
138     
139 }