safeapplication.hh
Go to the documentation of this file.
1 // Copyright (c) 2015-2018, LAAS-CNRS
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4 // This file is part of gepetto-viewer.
5 // gepetto-viewer is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // gepetto-viewer is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // gepetto-viewer. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef GEPETTO_GUI_SAFEAPPLICATION_HH
18 #define GEPETTO_GUI_SAFEAPPLICATION_HH
19 
20 #include <assert.h>
21 
22 #include <gepetto/gui/fwd.hh>
23 
24 #include <QApplication>
25 
26 namespace gepetto {
27  namespace gui {
29  {
30  public:
31  SlotExceptionCatch () : child_ (NULL) {}
32 
34  virtual bool safeNotify (QApplication* app, QObject* receiver, QEvent* e) = 0;
35 
37  {
38  if (child_ == NULL) child_ = child;
39  else child_->addAsLeaf (child);
40  }
41 
42  protected:
43  bool impl_notify(QApplication* app, QObject* receiver, QEvent* e)
44  {
45  if (child_ == NULL)
46  return app->QApplication::notify(receiver, e);
47  else
48  return child_->safeNotify(app, receiver, e);
49  }
50 
52  };
53 
54  class SafeApplication : public QApplication, public SlotExceptionCatch
55  {
56  public:
57  explicit SafeApplication (int& argc, char ** argv)
58  : QApplication(argc, argv) {}
59 
60  bool notify(QObject* receiver, QEvent* e)
61  {
62  return impl_notify (this, receiver, e);
63  }
64 
65  private:
66  bool safeNotify (QApplication*, QObject*, QEvent*) { assert(false); return false; };
67  };
68  } // namespace gui
69 } // namespace gepetto
70 
71 #endif // GEPETTO_GUI_SAFEAPPLICATION_HH
Definition: safeapplication.hh:54
Definition: safeapplication.hh:28
Definition: action-search-bar.hh:27
virtual bool safeNotify(QApplication *app, QObject *receiver, QEvent *e)=0
Inherited classes must call impl_notify method surronded by a try/catch.
bool impl_notify(QApplication *app, QObject *receiver, QEvent *e)
Definition: safeapplication.hh:43
bool notify(QObject *receiver, QEvent *e)
Definition: safeapplication.hh:60
SafeApplication(int &argc, char **argv)
Definition: safeapplication.hh:57
void addAsLeaf(SlotExceptionCatch *child)
Definition: safeapplication.hh:36
SlotExceptionCatch()
Definition: safeapplication.hh:31
SlotExceptionCatch * child_
Definition: safeapplication.hh:51