X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fresult.hpp;h=e14f92b253b726b6a14fa3727de4855aaeff9a6b;hb=6fdda786876d9640a6a26f05ef2c5aa7e2175421;hp=e22fde9872260e05700d0b880277bacad54f6fad;hpb=5463a34f0136555cee0bfad9a1cb85e93aeb635c;p=staging%2Fwindowmanager.git diff --git a/src/result.hpp b/src/result.hpp index e22fde9..e14f92b 100644 --- a/src/result.hpp +++ b/src/result.hpp @@ -1,11 +1,24 @@ -// -// Created by mfritzsc on 7/12/17. -// +/* + * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #ifndef TMCAGLWM_RESULT_HPP #define TMCAGLWM_RESULT_HPP #include +#include namespace wm { @@ -29,11 +42,16 @@ struct result { return this->t.value(); } - operator T() { - return this->unwrap(); - } + operator T() { return this->unwrap(); } char const *unwrap_err() { return this->e; } + + optional const &ok() const { return this->t; } + optional err() const { + return this->e ? optional(this->e) : nullopt; + } + + result map_err(std::function f); }; template @@ -46,6 +64,14 @@ struct result Ok(T t) { return result{nullptr, t}; } +template +result result::map_err(std::function f) { + if (this->is_err()) { + return Err(f(this->e)); + } + return *this; +} + } // namespace wm #endif // TMCAGLWM_RESULT_HPP