~vanvugt/mir/vbox

« back to all changes in this revision

Viewing changes to include/server/mir/observer_registrar.h

  • Committer: Tarmac
  • Author(s): Christopher James Halse Rogers
  • Date: 2016-10-31 04:44:12 UTC
  • mfrom: (3745.4.20 observer-multiplexer)
  • Revision ID: tarmac-20161031044412-bywv9t1xufjir3a1
Add ObserverRegistrar interface and an ObserverMultiplexer helper implementation.

An ObserverRegistrar manages registering and unregistering Observers of some Mir state.

The ObserverMultiplexer is an implementation of ObserverRegistrar which itself is an Observer, so can be handed out to internal Mir components as a singular Observer and to shells using Mir as an ObserverRegistrar.

At the moment ObserverMultiplexer requires code to implement Observer be provided manually. In The Glorious Future, C++ will provide compile-time reflection which will make it possible to automatically implement.

Approved by mir-ci-bot, Andreas Pokorny, Cemil Azizoglu.

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 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 General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Christopher James Halse Rogers <christopher.halse.rogers@canonical.com>
 
17
 */
 
18
 
 
19
#ifndef MIR_OBSERVER_REGISTRAR_H_
 
20
#define MIR_OBSERVER_REGISTRAR_H_
 
21
 
 
22
#include <memory>
 
23
 
 
24
namespace mir
 
25
{
 
26
class Executor;
 
27
 
 
28
/**
 
29
 * Register observers for a subsystem.
 
30
 *
 
31
 * \tparam Observer The Observer type to register
 
32
 */
 
33
template<class Observer>
 
34
class ObserverRegistrar
 
35
{
 
36
public:
 
37
    /**
 
38
     * Add an observer to the set notified of all observations
 
39
     *
 
40
     * The ObserverRegistrar does not take any ownership of \p observer, and will
 
41
     * automatically remove it when \p observer expires.
 
42
     *
 
43
     * \param [in] observer The observer to register
 
44
     */
 
45
    virtual void register_interest(std::weak_ptr<Observer> const& observer) = 0;
 
46
 
 
47
    /**
 
48
     * Add an observer with specified execution environment
 
49
     *
 
50
     * This is threadsafe and can be called in any context.
 
51
     *
 
52
     * The ObserverRegistrar does not take any ownership of \p observer, and will
 
53
     * automatically remove it when \p observer expires.
 
54
     *
 
55
     * All calls to \p observer methods are performed in the context of
 
56
     * \p executor.
 
57
     *
 
58
     * The \p executor should process work in a delayed fashion. Particularly,
 
59
     * executor::spawn(work) is expected to \b not run \p work in the current
 
60
     * stack. Eager execution of work may result in deadlocks if calls to the
 
61
     * observer result in calls into the ObserverRegistrar.
 
62
     *
 
63
     * \param [in] observer The observer to register
 
64
     * \param [in] executor Execution environment for calls to \p observer methods.
 
65
     *                          The caller is responsible for ensuring \p executor outlives
 
66
     *                          \p observer.
 
67
     */
 
68
    virtual void register_interest(
 
69
        std::weak_ptr<Observer> const& observer,
 
70
        Executor& executor) = 0;
 
71
 
 
72
    /**
 
73
     * Remove an observer from the set notified of all observations.
 
74
     *
 
75
     * This is threadsafe and can be called in any context.
 
76
     * It is \b not guaranteed that methods of \p observer will not be called after
 
77
     * this returns.
 
78
     *
 
79
     * \param observer [in] The observer to unregister
 
80
     */
 
81
    virtual void unregister_interest(Observer const& observer) = 0;
 
82
 
 
83
protected:
 
84
    ObserverRegistrar() = default;
 
85
    virtual ~ObserverRegistrar() = default;
 
86
    ObserverRegistrar(ObserverRegistrar const&) = delete;
 
87
    ObserverRegistrar& operator=(ObserverRegistrar const&) = delete;
 
88
};
 
89
 
 
90
}
 
91
 
 
92
#endif //MIR_OBSERVER_REGISTRAR_H_