primarydriverwiththread.h
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13 ***************************************************************************/
14 #ifndef PRIMARYDRIVERWITHTHREAD_H_
15 #define PRIMARYDRIVERWITHTHREAD_H_
16 
17 #include "primarydriver.h"
18 
20 public:
21  XPrimaryDriverWithThread(const char *name, bool runtime, Transaction &tr_meas, const shared_ptr<XMeasure> &meas) :
22  XPrimaryDriver(name, runtime, ref(tr_meas), meas) {}
23  virtual ~XPrimaryDriverWithThread() = default;
24 
25  //! Shuts down your threads, unconnects GUI, and deactivates signals.\n
26  //! This function may be called even if driver has already stopped.
27  //! This should not cause an exception.
28  virtual void stop();
29 private:
30 protected:
31  //! Starts up your threads, connects GUI, and activates signals.
32  //! This function should not cause an exception.
33  virtual void start();
34  //! This function should not cause an exception.
35  virtual void closeInterface() = 0;
36 
37  virtual void *execute(const atomic<bool> &terminated) = 0;
38 private:
39  shared_ptr<XThread<XPrimaryDriverWithThread> > m_thread;
40  void *execute_internal(const atomic<bool> &terminated) {
41  void *ret = NULL;
42  try {
43  ret = execute(terminated);
44  }
45  catch(XKameError &e) {
46  e.print(getLabel() + i18n(" Error: "));
47  }
48  closeInterface(); //closes interface if any.
49  return ret;
50  }
51 };
52 
53 inline void
55  auto th = std::make_shared<XThread<XPrimaryDriverWithThread> >(
56  shared_from_this(),
57  &XPrimaryDriverWithThread::execute_internal);
58  th->resume();
59  m_thread = th;
60 }
61 
62 inline void
64  if(auto th = m_thread)
65  th->terminate();
66  else
67  closeInterface(); //closes interface if any.
68 }
69 
70 #endif /*PRIMARYDRIVERWITHTHREAD_H_*/

Generated for KAME4 by  doxygen 1.8.3