~thomas-voss/location-service/flatten

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright © 2014 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */
#ifndef CORE_LOCATION_SETTINGS_H_
#define CORE_LOCATION_SETTINGS_H_

#include <cstdint>

#include <memory>
#include <sstream>
#include <string>

namespace core
{
namespace location
{
// A simple interface for reading/writing values given a string key.
struct Settings
{
    // To safe us some typing
    typedef std::shared_ptr<Settings> Ptr;

    // Collects possible exceptions reported from getters.
    struct Error
    {
        Error() = delete;

        // Thrown if no value of the specified type is available for
        // the given key.
        struct NoValueForKey : public std::runtime_error
        {
            NoValueForKey(const std::string& key);
        };
    };

    // The usual boilerplate
    Settings() = default;
    Settings(const Settings&) = delete;
    virtual ~Settings() = default;
    Settings& operator=(const Settings&) = delete;

    // Syncs the current settings to implementation-specific backends.
    virtual void sync() = 0;

    // Returns true iff a value is known for the given key.
    virtual bool has_value_for_key(const std::string& key) const = 0;

    // Convenience wrapper.
    template <typename T>
    inline T get_enum_for_key(const std::string& key, T default_value);

    template <typename T>
    inline T get_enum_for_key_or_throw(const std::string& key);

    // Tries to read the value for the given key and returns the default_value if
    // no value is known.
    std::string get_string_for_key(const std::string& key, const std::string& default_value);

    // Gets an integer value known for the given key, or throws Error::NoValueForKey.
    virtual std::string get_string_for_key_or_throw(const std::string& key) = 0;

    // Convenience wrapper.
    template <typename T>
    inline bool set_enum_for_key(const std::string& key, T value);

    // Sets values known for the given key.
    virtual bool set_string_for_key(const std::string& key, const std::string& value) = 0;
};

// A decorator that syncs whenever a value changes.
struct SyncingSettings : public Settings
{
    SyncingSettings(const Settings::Ptr& impl) : impl{impl} {}

    ~SyncingSettings()
    {
        sync();
    }

    virtual void sync()
    {
        impl->sync();
    }

    virtual bool has_value_for_key(const std::string& key) const
    {
        return impl->has_value_for_key(key);
    }

    virtual std::string get_string_for_key_or_throw(const std::string& key)
    {
        return impl->get_string_for_key_or_throw(key);
    }

    virtual bool set_string_for_key(const std::string& key, const std::string& value)
    {
        auto result = impl->set_string_for_key(key, value);
        if (result)
            impl->sync();
        return result;
    }

    Settings::Ptr impl;
};

template <typename T>
inline T Settings::get_enum_for_key(const std::string& key, T default_value)
{
    static_assert(std::is_enum<T>::value, "Only enum types are supported.");
    std::stringstream dss;
    dss << default_value;
    std::stringstream ss{get_string_for_key(key, dss.str())};
    T result;
    ss >> result;
    return result;
}

template <typename T>
inline T Settings::get_enum_for_key_or_throw(const std::string& key)
{
    static_assert(std::is_enum<T>::value, "Only enum types are supported.");
    std::stringstream ss{get_string_for_key_or_throw(key)};
    T result;
    ss >> result;
    return result;
}

template <typename T>
inline bool Settings::set_enum_for_key(const std::string& key, T value)
{
    static_assert(std::is_enum<T>::value, "Only enum types are supported.");
    std::stringstream ss;
    ss << value;
    return set_string_for_key(key, ss.str());
}
}
}

#endif // CORE_LOCATION_SETTINGS_H_