~thomas-voss/location-service/refactor-location-position

« back to all changes in this revision

Viewing changes to 3rd-party/ichnaea/include/ichnaea/response.h

  • Committer: Thomas Voß
  • Date: 2016-08-14 19:36:05 UTC
  • Revision ID: thomas.voss@canonical.com-20160814193605-e6c0xitja9yncmw8
Add back mls provider relying on Mozilla's location service.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2016 Canonical Ltd.
 
2
// 
 
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.
 
7
// 
 
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.
 
12
// 
 
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_
 
17
 
 
18
#include <ichnaea/error.h>
 
19
 
 
20
#include <boost/variant.hpp>
 
21
 
 
22
#include <type_traits>
 
23
 
 
24
namespace ichnaea
 
25
{
 
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>
 
29
class Response
 
30
{
 
31
  public:
 
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
 
35
    {
 
36
        NotAnError() : std::runtime_error{"Not an error"}
 
37
        {
 
38
        }
 
39
    };
 
40
 
 
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}
 
45
    {
 
46
    }
 
47
 
 
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}
 
52
    {
 
53
    }
 
54
 
 
55
    /// @brief is_error returns true if the instance contains an error.
 
56
    bool is_error() const
 
57
    {
 
58
        return 1 == result_or_error.which();
 
59
    }
 
60
 
 
61
    /// @brief error returns the error response or throws NotAnError
 
62
    /// if no error is contained in the instance.
 
63
    const Error& error() const
 
64
    {
 
65
        if (not is_error())
 
66
            throw NotAnError{};
 
67
 
 
68
        return boost::get<Error>(result_or_error);
 
69
    }
 
70
 
 
71
    /// @brief result returns the Result instance. Throws the contained
 
72
    /// error if this is an error response.
 
73
    const Result& result() const
 
74
    {
 
75
        if (is_error())
 
76
            throw error();
 
77
 
 
78
        return boost::get<Result>(result_or_error);
 
79
    }
 
80
 
 
81
  private:
 
82
    boost::variant<Result, Error> result_or_error;    
 
83
};
 
84
}
 
85
 
 
86
#endif // ICHNAEA_RESPONSE_H_