Lumiera  0.pre.03
»edit your freedom«
ui-dispatcher.hpp
Go to the documentation of this file.
1 /*
2  UI-DISPATCHER.hpp - dispatch invocations into the UI event thread
3 
4  Copyright (C) Lumiera.org
5  2017, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
75 #ifndef STAGE_CTRL_UI_DISPATCHER_H
76 #define STAGE_CTRL_UI_DISPATCHER_H
77 
78 #include "stage/gtk-base.hpp"
79 #include "lib/call-queue.hpp"
80 #include "lib/format-string.hpp"
81 #include "lib/nocopy.hpp"
82 
83 #include <utility>
84 
85 
86 namespace stage {
87 namespace ctrl {
88 
89  using std::move;
90  using ::util::_Fmt;
91 
92  namespace {
94  inline string
95  generateErrorResponse (const char* problem = "unexpected problem")
96  {
97  static _Fmt messageTemplate{"asynchronous UI response failed: %s (error flag was: %s)"};
98  string response{messageTemplate % problem % lumiera_error()};
99  WARN (stage, "%s", response.c_str());
100  return response;
101  }
102  }
103 
104 
113  {
114  lib::CallQueue queue_;
115  Glib::Dispatcher dispatcher_;
116 
117  using Operation = lib::CallQueue::Operation;
118 
119  public:
120  template<class FUN>
121  UiDispatcher(FUN notification)
122  : queue_{}
123  , dispatcher_{}
124  {
125  dispatcher_.connect(
126  [=]() {try {
127  queue_.invoke();
128  }
129  catch (std::exception& problem)
130  {
131  notification (generateErrorResponse(problem.what()));
132  }
133  catch (...)
134  {
135  notification (generateErrorResponse());
136  }
137  });
138  }
139 
147  void
148  event (Operation&& op)
149  {
150  queue_.feed (move(op));
151  dispatcher_.emit();
152  }
153  };
154 
155 
156 
157 }}// namespace stage::ctrl
158 #endif /*STAGE_CTRL_UI_DISPATCHER_H*/
string generateErrorResponse(const char *problem="unexpected problem")
void event(Operation &&op)
move the given operation into our private dispatcher queue and then schedule dequeuing and invocation...
A threadsafe queue for bound void(void) functors.
Definition: call-queue.hpp:58
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
Front-end for printf-style string template interpolation.
Helper to dispatch code blocks into the UI event thread for execution.
A front-end for using printf-style formatting.
Mix-Ins to allow or prohibit various degrees of copying and cloning.
A Queue for function invocations, allowing them to be dispatched on demand.
Lumiera GTK UI implementation root.
Definition: guifacade.cpp:46
lumiera_err lumiera_error(void)
Get and clear current error state.
Definition: error-state.c:124
A set of basic GTK includes for the UI.