OSDN Git Service

無駄な行や改行を削除
[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     init(_ rawValue: String, alternative value: T? = nil, regulator: @escaping (T) -> T = { $0 }) {
77         
78         self.rawAlternativeValue = value
79         self.regulator = regulator
80         
81         super.init(rawValue: rawValue)
82         
83     }
84     
85     required convenience init(rawValue: String) {
86         
87         self.init(rawValue)
88     }
89     
90     
91     var alternative: T {
92         
93         switch rawAlternativeValue {
94         case .none: fatalError("DefaultKey (\(self)) has no alternative value.")
95         case let .some(value): return value
96         }
97     }
98     
99 }
100
101
102 extension UserDefaults {
103     
104     subscript(key: DefaultKey<Bool>) -> Bool {
105         
106         get { return self.bool(forKey: key.rawValue) }
107         set { self.set(newValue, forKey: key.rawValue) }
108     }
109     
110     
111     subscript(key: DefaultKey<Int>) -> Int {
112         
113         get { return key.regulator(self.integer(forKey: key.rawValue)) }
114         set { self.set(key.regulator(newValue), forKey: key.rawValue) }
115     }
116     
117     
118     subscript(key: DefaultKey<Double>) -> Double {
119         
120         get { return key.regulator(self.double(forKey: key.rawValue)) }
121         set { self.set(key.regulator(newValue), forKey: key.rawValue) }
122     }
123     
124     
125     subscript(key: DefaultKey<CGFloat>) -> CGFloat {
126         
127         get { return key.regulator(CGFloat(self.double(forKey: key.rawValue))) }
128         set { self.set(key.regulator(newValue), forKey: key.rawValue) }
129     }
130     
131     
132     subscript(key: DefaultKey<String?>) -> String? {
133         
134         get { return self.string(forKey: key.rawValue) }
135         set { self.set(newValue, forKey: key.rawValue) }
136     }
137     
138 }