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)
5  2017, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
66 #ifndef STAGE_CTRL_UI_DISPATCHER_H
67 #define STAGE_CTRL_UI_DISPATCHER_H
68 
69 #include "stage/gtk-base.hpp"
70 #include "lib/call-queue.hpp"
71 #include "lib/format-string.hpp"
72 #include "lib/nocopy.hpp"
73 
74 #include <utility>
75 
76 
77 namespace stage {
78 namespace ctrl {
79 
80  using std::move;
81  using ::util::_Fmt;
82 
83  namespace {
85  inline string
86  generateErrorResponse (const char* problem = "unexpected problem")
87  {
88  static _Fmt messageTemplate{"asynchronous UI response failed: %s (error flag was: %s)"};
89  string response{messageTemplate % problem % lumiera_error()};
90  WARN (stage, "%s", response.c_str());
91  return response;
92  }
93  }
94 
95 
104  {
105  lib::CallQueue queue_;
106  Glib::Dispatcher dispatcher_;
107 
108  using Operation = lib::CallQueue::Operation;
109 
110  public:
111  template<class FUN>
112  UiDispatcher(FUN notification)
113  : queue_{}
114  , dispatcher_{}
115  {
116  dispatcher_.connect(
117  [=]() {try {
118  queue_.invoke();
119  }
120  catch (std::exception& problem)
121  {
122  notification (generateErrorResponse(problem.what()));
123  }
124  catch (...)
125  {
126  notification (generateErrorResponse());
127  }
128  });
129  }
130 
138  void
139  event (Operation&& op)
140  {
141  queue_.feed (move(op));
142  dispatcher_.emit();
143  }
144  };
145 
146 
147 
148 }}// namespace stage::ctrl
149 #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:49
Any copy and copy construction prohibited.
Definition: nocopy.hpp:37
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:37
lumiera_err lumiera_error(void)
Get and clear current error state.
Definition: error-state.c:115
A set of basic GTK includes for the UI.