interface.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 INTERFACE_H_
15 #define INTERFACE_H_
16 
17 #include "xnode.h"
18 #include "xlistnode.h"
19 #include "xitemnode.h"
20 #include <vector>
21 
22 class XDriver;
23 //! virtual class for communication devices.
24 //! \sa XCharInterface
25 class DECLSPEC_KAME XInterface : public XNode {
26 public:
27  XInterface(const char *name, bool runtime, const shared_ptr<XDriver> &driver);
28  virtual ~XInterface() {}
29 
30  struct DECLSPEC_KAME XInterfaceError : public XKameError {
31  XInterfaceError(const XString &msg, const char *file, int line);
32  virtual ~XInterfaceError() throw() {}
33  };
34  struct DECLSPEC_KAME XConvError : public XInterfaceError {
35  XConvError(const char *file, int line);
36  virtual ~XConvError() throw() {}
37  };
38  struct DECLSPEC_KAME XCommError : public XInterfaceError {
39  XCommError(const XString &, const char *file, int line);
40  virtual ~XCommError() throw() {}
41  };
42  struct DECLSPEC_KAME XOpenInterfaceError : public XInterfaceError {
43  XOpenInterfaceError(const char *file, int line);
44  virtual ~XOpenInterfaceError() throw() {}
45  };
46 
47  void setLabel(const XString& str) {m_label = str;}
48  virtual XString getLabel() const;
49 
50  shared_ptr<XDriver> driver() const {return m_driver.lock();}
51  //! type of interface or driver.
52  const shared_ptr<XComboNode> &device() const {return m_device;}
53  //! port number or device name.
54  const shared_ptr<XStringNode> &port() const {return m_port;}
55  //! e.g. GPIB address.
56  const shared_ptr<XUIntNode> &address() const {return m_address;}
57  //! True if interface is opened. Start/stop interface.
58  const shared_ptr<XBoolNode> &control() const {return m_control;}
59 
60  void lock() {m_mutex.lock();}
61  void unlock() {m_mutex.unlock();}
62  bool isLocked() const {return m_mutex.isLockedByCurrentThread();}
63 
64  XRecursiveMutex &mutex() {return m_mutex;}
65 
66  virtual bool isOpened() const = 0;
67 
68  void start();
69  void stop();
70 
71  struct DECLSPEC_KAME Payload : public XNode::Payload {
72  Talker<XInterface*> &onOpen() {return m_tlkOnOpen;}
73  const Talker<XInterface*> &onOpen() const {return m_tlkOnOpen;}
74  Talker<XInterface*> &onClose() {return m_tlkOnClose;}
75  const Talker<XInterface*> &onClose() const {return m_tlkOnClose;}
76  protected:
77  Talker<XInterface*> m_tlkOnOpen;
78  Talker<XInterface*> m_tlkOnClose;
79  };
80 protected:
81  virtual void open() throw (XInterfaceError &) = 0;
82  //! This can be called even if has already closed.
83  virtual void close() throw (XInterfaceError &) = 0;
84 private:
85  void onControlChanged(const Snapshot &shot, XValueNodeBase *);
86 
87  const weak_ptr<XDriver> m_driver;
88  const shared_ptr<XComboNode> m_device;
89  const shared_ptr<XStringNode> m_port;
90  const shared_ptr<XUIntNode> m_address;
91  const shared_ptr<XBoolNode> m_control;
92 
93  shared_ptr<XListener> lsnOnControlChanged;
94 
95  XRecursiveMutex m_mutex;
96 
97  XString m_label;
98 };
99 
100 class DECLSPEC_KAME XInterfaceList : public XAliasListNode<XInterface> {
101 public:
102  XInterfaceList(const char *name, bool runtime) : XAliasListNode<XInterface>(name, runtime) {}
103 };
104 
105 #endif /*INTERFACE_H_*/

Generated for KAME4 by  doxygen 1.8.3