Music Hub  ..
A session-wide music playback service
qtbridge.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2014 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: Jussi Pakkanen <jussi.pakkanen@canonical.com>
17  * Thomas Voß <thomas.voss@canonical.com>
18  */
19 
20 #ifndef QT_CORE_WORLD_BRIDGE_H_
21 #define QT_CORE_WORLD_BRIDGE_H_
22 
23 #include <QObject>
24 
25 #include <functional>
26 #include <future>
27 #include <iostream>
28 
29 namespace qt
30 {
31 namespace core
32 {
33 namespace world
34 {
42 void build_and_run(int argc, char** argv, const std::function<void()>& ready);
43 
47 void destroy();
48 
54 std::future<void> enter_with_task(const std::function<void()>& task);
55 
56 
62 template<typename T>
63 inline std::future<T> enter_with_task_and_expect_result(const std::function<T()>& task)
64 {
65  std::shared_ptr<std::promise<T>> promise = std::make_shared<std::promise<T>>();
66  std::future<T> future = promise->get_future();
67 
68  auto t = [promise, task]()
69  {
70  try
71  {
72  promise->set_value(task());
73  } catch(...)
74  {
75  promise->set_exception(std::current_exception());
76  }
77  };
78 
79  enter_with_task(t);
80 
81  return future;
82 }
83 }
84 }
85 }
86 
87 #endif // QT_CORE_WORLD_BRIDGE_H_
void destroy()
Destroys the Qt core world and quits its event loop.
Definition: qtbridge.cpp:154
Definition: player.h:33
void build_and_run(int argc, char **argv, const std::function< void()> &ready)
Sets up the Qt core world and executes its event loop. Blocks until destroy() is called.
Definition: qtbridge.cpp:132
Definition: qtbridge.cpp:36
std::future< T > enter_with_task_and_expect_result(const std::function< T()> &task)
Enters the Qt core world and schedules the given task for execution.
Definition: qtbridge.h:63
std::future< void > enter_with_task(const std::function< void()> &task)
Enters the Qt core world and schedules the given task for execution.
Definition: qtbridge.cpp:163