~thomas-voss/location-service/add-snapcraft-setup-next

« back to all changes in this revision

Viewing changes to src/location/providers/state_tracking_provider.h

Merge lp:~thomas-voss/location-service/simplify-provider-interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2016 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
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 Lesser 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
 *
 
16
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
#ifndef LOCATION_PROVIDERS_STATE_TRACKING_PROVIDER_H_
 
19
#define LOCATION_PROVIDERS_STATE_TRACKING_PROVIDER_H_
 
20
 
 
21
#include <location/provider.h>
 
22
 
 
23
#include <functional>
 
24
#include <mutex>
 
25
 
 
26
namespace location
 
27
{
 
28
namespace providers
 
29
{
 
30
/// @brief StateTrackingProvider implements a simple state-machine making sure
 
31
/// that only supported and valid state transitions are triggered. In addition,
 
32
/// an activation count is tracked that ensures that providers are only ever started/stopped once.
 
33
class StateTrackingProvider : public Provider
 
34
{
 
35
public:
 
36
    typedef std::shared_ptr<StateTrackingProvider> Ptr;
 
37
 
 
38
    /// @brief State models the different states of a provider.
 
39
    enum class State
 
40
    {
 
41
        disabled,   ///< The provider is disabled. Implementations are free to minimize resource usage in this state.
 
42
        enabled,    ///< The provider is actively delivering updates.
 
43
        active      ///< The provider is actively delivering updates.
 
44
    };
 
45
 
 
46
    struct InvalidStateTransition : public std::runtime_error
 
47
    {
 
48
        InvalidStateTransition(State from, State to);
 
49
        State from;
 
50
        State to;
 
51
    };
 
52
 
 
53
    StateTrackingProvider(const Provider::Ptr& impl);
 
54
 
 
55
    const core::Property<State>& state() const;
 
56
 
 
57
    // From Provider
 
58
    void on_new_event(const Event& event) override;
 
59
 
 
60
    void enable() override;
 
61
    void disable() override;
 
62
    void activate() override;
 
63
    void deactivate() override;
 
64
 
 
65
    Requirements requirements() const override;
 
66
    bool satisfies(const Criteria& criteria) override;
 
67
    const core::Signal<Update<Position>>& position_updates() const override;
 
68
    const core::Signal<Update<Heading>>& heading_updates() const override;
 
69
    const core::Signal<Update<Velocity>>& velocity_updates() const override;
 
70
 
 
71
private:
 
72
    class ReferenceCountedOnce
 
73
    {
 
74
    public:
 
75
        // increment increments the internal counter, executing task if the new value is 1.
 
76
        void increment(const std::function<void()>& task);
 
77
 
 
78
        // decrement decrements the internal counter and executes task if 0 is reached.
 
79
        void decrement(const std::function<void()>& task);
 
80
 
 
81
    private:
 
82
        std::size_t counter{0};
 
83
    };
 
84
 
 
85
    Provider::Ptr impl_;
 
86
    std::recursive_mutex guard_;
 
87
    ReferenceCountedOnce activation_counter_;
 
88
    core::Property<State> state_;
 
89
};
 
90
}
 
91
}
 
92
 
 
93
#endif // LOCATION_PROVIDERS_STATE_TRACKING_PROVIDER_H_