1
// Copyright (C) 2016 Canonical Ltd.
3
// This library is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU Lesser General Public License as published
5
// by the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
8
// This program is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
// GNU General Public License for more details.
13
// You should have received a copy of the GNU Lesser General Public License
14
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
#ifndef ICHNAEA_RESPONSE_H_
16
#define ICHNAEA_RESPONSE_H_
18
#include <ichnaea/error.h>
20
#include <boost/variant.hpp>
22
#include <type_traits>
26
/// @brief Repsonse models a typed variant that either contains the Result of
27
/// an operation xor an error if the operation fails.
28
template<typename Result>
32
/// @brief NotAnError is thrown if error() is invoked on a Response<T>
33
/// instance that contains a result.
34
struct NotAnError : public std::runtime_error
36
NotAnError() : std::runtime_error{"Not an error"}
41
/// @brief Response initializes a new instance with result such
42
/// that subsequent invocations of is_error() return false.
43
explicit Response(const Result& result)
44
: result_or_error{result}
48
/// @brief Response initializes a new instance with error such
49
/// that subsequent invocations of is_error() return true.
50
explicit Response(const Error& error)
51
: result_or_error{error}
55
/// @brief is_error returns true if the instance contains an error.
58
return 1 == result_or_error.which();
61
/// @brief error returns the error response or throws NotAnError
62
/// if no error is contained in the instance.
63
const Error& error() const
68
return boost::get<Error>(result_or_error);
71
/// @brief result returns the Result instance. Throws the contained
72
/// error if this is an error response.
73
const Result& result() const
78
return boost::get<Result>(result_or_error);
82
boost::variant<Result, Error> result_or_error;
86
#endif // ICHNAEA_RESPONSE_H_