chardevicedriver.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 CHARDEVICEDRIVER_H_
15 #define CHARDEVICEDRIVER_H_
16 
17 #include "driver.h"
18 #include "interface.h"
19 class XCharInterface;
20 
21 template<class tDriver, class tInterface = XCharInterface>
22 class XCharDeviceDriver : public tDriver {
23 public:
24  XCharDeviceDriver(const char *name, bool runtime,
25  Transaction &tr_meas, const shared_ptr<XMeasure> &meas);
26  virtual ~XCharDeviceDriver() = default;
27 protected:
28  const shared_ptr<tInterface> &interface() const {return m_interface;}
29  //! Be called just after opening interface. Call start() inside this routine appropriately.
30  virtual void open() throw (XKameError &) {this->start();}
31  //! Be called during stopping driver. Call interface()->stop() inside this routine.
32  virtual void close() throw (XKameError &) {interface()->stop();}
33  void onOpen(const Snapshot &shot, XInterface *);
34  void onClose(const Snapshot &shot, XInterface *);
35  //! This should not cause an exception.
36  //! This function should be called before leaving a measurement thread to terminate the interface.
37  virtual void closeInterface() {close();}
38 private:
39  shared_ptr<XListener> m_lsnOnOpen, m_lsnOnClose;
40 
41  const shared_ptr<tInterface> m_interface;
42 };
43 
44 template<class tDriver, class tInterface>
45 XCharDeviceDriver<tDriver, tInterface>::XCharDeviceDriver(const char *name, bool runtime,
46  Transaction &tr_meas, const shared_ptr<XMeasure> &meas) :
47  tDriver(name, runtime, ref(tr_meas), meas),
48  m_interface(XNode::create<tInterface>("Interface", false,
49  dynamic_pointer_cast<XDriver>(this->shared_from_this()))) {
50 
51  meas->interfaces()->insert(tr_meas, m_interface);
52  this->iterate_commit([=](Transaction &tr){
53  m_lsnOnOpen = tr[ *interface()].onOpen().connectWeakly(
54  this->shared_from_this(), &XCharDeviceDriver<tDriver, tInterface>::onOpen);
55  m_lsnOnClose = tr[ *interface()].onClose().connectWeakly(
56  this->shared_from_this(), &XCharDeviceDriver<tDriver, tInterface>::onClose);
57  });
58 }
59 template<class tDriver, class tInterface>
60 void
62  try {
63  open();
64  }
65  catch (XKameError& e) {
66  e.print(this->getLabel() + i18n(": Opening driver failed, because "));
67  onClose(shot, NULL);
68  }
69 }
70 template<class tDriver, class tInterface>
71 void
73  try {
74  this->stop();
75  }
76  catch (XKameError& e) {
77  e.print(this->getLabel() + i18n(": Stopping driver failed, because "));
78  close();
79  }
80 }
81 #endif /*CHARDEVICEDRIVER_H_*/

Generated for KAME4 by  doxygen 1.8.3