2
* Copyright © 2016 Canonical Ltd.
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.
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.
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/>.
16
* Authored by: Thomas Voß <thomas.voss@canonical.com>
18
#ifndef LOCATION_PROVIDERS_STATE_TRACKING_PROVIDER_H_
19
#define LOCATION_PROVIDERS_STATE_TRACKING_PROVIDER_H_
21
#include <location/provider.h>
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
36
typedef std::shared_ptr<StateTrackingProvider> Ptr;
38
/// @brief State models the different states of a provider.
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.
46
struct InvalidStateTransition : public std::runtime_error
48
InvalidStateTransition(State from, State to);
53
StateTrackingProvider(const Provider::Ptr& impl);
55
const core::Property<State>& state() const;
58
void on_new_event(const Event& event) override;
60
void enable() override;
61
void disable() override;
62
void activate() override;
63
void deactivate() override;
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;
72
class ReferenceCountedOnce
75
// increment increments the internal counter, executing task if the new value is 1.
76
void increment(const std::function<void()>& task);
78
// decrement decrements the internal counter and executes task if 0 is reached.
79
void decrement(const std::function<void()>& task);
82
std::size_t counter{0};
86
std::recursive_mutex guard_;
87
ReferenceCountedOnce activation_counter_;
88
core::Property<State> state_;
93
#endif // LOCATION_PROVIDERS_STATE_TRACKING_PROVIDER_H_