Program Listing for File multiplexing_dispatchable.h

↰ Return to documentation for file (/home/docs/checkouts/readthedocs.org/user_builds/canonical-mir-documentation/checkouts/2.26/include/common/mir/dispatch/multiplexing_dispatchable.h)

/*
 * Copyright © 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 2 or 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/>.
 */

#ifndef MIR_DISPATCH_MULTIPLEXING_DISPATCHABLE_H_
#define MIR_DISPATCH_MULTIPLEXING_DISPATCHABLE_H_

#include <mir/dispatch/dispatchable.h>
#include <mir/posix_rw_mutex.h>

#include <functional>
#include <initializer_list>
#include <list>
#include <mutex>

#include <pthread.h>

namespace mir
{
namespace dispatch
{
enum class DispatchReentrancy
{
    sequential,
    reentrant
};

class MultiplexingDispatchable final : public Dispatchable
{
public:
    MultiplexingDispatchable();
    MultiplexingDispatchable(std::initializer_list<std::shared_ptr<Dispatchable>> dispatchees);
    virtual ~MultiplexingDispatchable() noexcept;

    MultiplexingDispatchable& operator=(MultiplexingDispatchable const&) = delete;
    MultiplexingDispatchable(MultiplexingDispatchable const&) = delete;

    Fd watch_fd() const override;
    bool dispatch(FdEvents events) override;
    FdEvents relevant_events() const override;

    void add_watch(std::shared_ptr<Dispatchable> const& dispatchee);
    void add_watch(std::shared_ptr<Dispatchable> const& dispatchee, DispatchReentrancy reentrancy);

    void add_watch(Fd const& fd, std::function<void()> const& callback);

    void remove_watch(std::shared_ptr<Dispatchable> const& dispatchee);

    void remove_watch(Fd const& fd);
private:
    PosixRWMutex lifetime_mutex;
    std::list<std::pair<std::shared_ptr<Dispatchable>, bool>> dispatchee_holder;

    Fd epoll_fd;
};
}
}
#endif // MIR_DISPATCH_MULTIPLEXING_DISPATCHABLE_H_