dummydriver.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 DUMMYDRIVER_H_
15 #define DUMMYDRIVER_H_
16 
17 #include "driver.h"
18 #include "interface.h"
19 
20 class XDummyInterface : public XInterface {
21 public:
22  XDummyInterface(const char *name, bool runtime, const shared_ptr<XDriver> &driver)
23  : XInterface(name, runtime, driver), m_bOpened(false)
24  {}
25  virtual ~XDummyInterface() {}
26 
27  virtual void open() throw (XInterfaceError &) {
28  m_bOpened = true;
29  }
30  //! This can be called even if has already closed.
31  virtual void close() throw (XInterfaceError &) {
32  m_bOpened = false;
33  }
34 
35  virtual bool isOpened() const {return m_bOpened;}
36 private:
37  bool m_bOpened;
38 };
39 template<class tDriver>
40 class XDummyDriver : public tDriver {
41 public:
42  XDummyDriver(const char *name, bool runtime, Transaction &tr_meas, const shared_ptr<XMeasure> &meas);
43  virtual ~XDummyDriver() {}
44 protected:
45  virtual void closeInterface() {interface()->stop();}
46  const shared_ptr<XDummyInterface> &interface() const {return m_interface;}
47 private:
48  shared_ptr<XListener> m_lsnOnOpen, m_lsnOnClose;
49  void onOpen(const Snapshot &shot, XInterface *);
50  void onClose(const Snapshot &shot, XInterface *);
51  const shared_ptr<XDummyInterface> m_interface;
52 };
53 
54 template<class tDriver>
55 XDummyDriver<tDriver>::XDummyDriver(const char *name, bool runtime,
56  Transaction &tr_meas, const shared_ptr<XMeasure> &meas) :
57  tDriver(name, runtime, ref(tr_meas), meas),
58  m_interface(XNode::create<XDummyInterface>("Interface", false,
59  dynamic_pointer_cast<XDriver>(this->shared_from_this()))) {
60  meas->interfaces()->insert(tr_meas, m_interface);
61  this->iterate_commit([=](Transaction &tr){
62  m_lsnOnOpen = tr[ *interface()].onOpen().connectWeakly(
63  this->shared_from_this(), &XDummyDriver<tDriver>::onOpen);
64  m_lsnOnClose = tr[ *interface()].onClose().connectWeakly(
65  this->shared_from_this(), &XDummyDriver<tDriver>::onClose);
66  });
67 }
68 template<class tDriver>
69 void
71  try {
72  this->start();
73  }
74  catch (XKameError& e) {
75  e.print(this->getLabel() + i18n(": Starting driver failed, because "));
76  interface()->stop();
77  }
78 }
79 template<class tDriver>
80 void
82  try {
83  this->stop();
84  }
85  catch (XKameError& e) {
86  e.print(this->getLabel() + i18n(": Stopping driver failed, because "));
87  }
88 }
89 
90 #endif /*DUMMYDRIVER_H_*/

Generated for KAME4 by  doxygen 1.8.3