@@ -118,25 +118,25 @@ class CancellableResult {
118
118
119
119
// / Return the result kind this \c CancellableResult represents: success,
120
120
// / failure or cancelled.
121
- CancellableResultKind getKind () { return Kind; }
121
+ CancellableResultKind getKind () const { return Kind; }
122
122
123
123
// / Assuming that the result represents success, return the underlying result
124
124
// / value.
125
- ResultType &getResult () {
125
+ const ResultType &getResult () const {
126
126
assert (getKind () == CancellableResultKind::Success);
127
127
return Result;
128
128
}
129
129
130
130
// / Assuming that the result represents success, retrieve members of the
131
131
// / underlying result value.
132
- ResultType *operator ->() { return &getResult (); }
132
+ const ResultType *operator ->() { return &getResult (); }
133
133
134
134
// / Assuming that the result represents success, return the underlying result
135
135
// / value.
136
- ResultType &operator *() { return getResult (); }
136
+ const ResultType &operator *() { return getResult (); }
137
137
138
138
// / Assuming that the result represents a failure, return the error message.
139
- std::string getError () {
139
+ std::string getError () const {
140
140
assert (getKind () == CancellableResultKind::Failure);
141
141
return Error;
142
142
}
@@ -152,7 +152,7 @@ class CancellableResult {
152
152
template <typename NewResultType>
153
153
void
154
154
mapAsync (llvm::function_ref<
155
- void (ResultType &,
155
+ void (const ResultType &,
156
156
llvm::function_ref<void (CancellableResult<NewResultType>)>)>
157
157
Transform,
158
158
llvm::function_ref<void(CancellableResult<NewResultType>)> Handle) {
@@ -170,6 +170,24 @@ class CancellableResult {
170
170
break ;
171
171
}
172
172
}
173
+
174
+ // / If the result represents success, invoke \p Transform to create a success
175
+ // / result containing new backing data.
176
+ // /
177
+ // / If the result represents error or cancelled, propagate that kind without
178
+ // / modification.
179
+ template <typename NewResultType>
180
+ CancellableResult<NewResultType>
181
+ map (llvm::function_ref<NewResultType(const ResultType &)> Transform) {
182
+ switch (getKind ()) {
183
+ case CancellableResultKind::Success:
184
+ return CancellableResult<NewResultType>::success (Transform (getResult ()));
185
+ case CancellableResultKind::Failure:
186
+ return CancellableResult<NewResultType>::failure (getError ());
187
+ case CancellableResultKind::Cancelled:
188
+ return CancellableResult<NewResultType>::cancelled ();
189
+ }
190
+ }
173
191
};
174
192
175
193
} // namespace ide
0 commit comments