Choreonoid  1.8
IoConnectionMap.h
Go to the documentation of this file.
1 #ifndef CNOID_BODY_IO_CONNECTION_MAP_H
2 #define CNOID_BODY_IO_CONNECTION_MAP_H
3 
4 #include <cnoid/ClonableReferenced>
5 #include <cnoid/Signal>
6 #include <string>
7 #include <vector>
8 #include "exportdecl.h"
9 
10 namespace cnoid {
11 
12 class DigitalIoDevice;
13 typedef ref_ptr<DigitalIoDevice> DigitalIoDevicePtr;
14 
15 class Body;
16 class Mapping;
17 
18 class CNOID_EXPORT DigitalIoConnection : public ClonableReferenced
19 {
20 public:
22  DigitalIoConnection(DigitalIoDevice* outDevice, int outIndex, DigitalIoDevice* inDevice, int inIndex);
23 
25  return static_cast<DigitalIoConnection*>(doClone(nullptr));
26  }
27  DigitalIoConnection* clone(CloneMap& cloneMap) const {
28  return static_cast<DigitalIoConnection*>(doClone(&cloneMap));
29  }
30 
31  enum IoType { In = 0, Out = 1 };
32 
33  DigitalIoDevice* device(int which) const { return device_[which]; }
34  int signalIndex(int which) const { return signalIndex_[which]; }
35  const std::string& bodyName(int which) const;
36  const std::string& deviceName(int which) const;
37 
38  bool hasDeviceInstances() const {
39  return (device_[In] != nullptr) && (device_[Out] != nullptr);
40  }
41 
42  void setDevice(int which, DigitalIoDevice* device);
43  void setNames(int which, const std::string& bodyName, const std::string& deviceName);
44  void setSignalIndex(int which, int index){ signalIndex_[which] = index; }
45 
46  DigitalIoDevice* outDevice(){ return device(Out); }
47  int outSignalIndex() const { return signalIndex(Out); }
48  const std::string& outBodyName(int which) const { return bodyName(Out); }
49  const std::string& outDeviceName(int which) const { return deviceName(Out); }
50 
51  DigitalIoDevice* inDevice(){ return device(In); }
52  int inSignalIndex() const { return signalIndex(In); }
53  const std::string& inBodyName(int which) const { return bodyName(In); }
54  const std::string& inDeviceName(int which) const { return deviceName(In); }
55 
56  void setInDevice(DigitalIoDevice* device){ setDevice(In, device); }
57  void setInSignalIndex(int index){ setSignalIndex(In, index); }
58  void setOutDevice(DigitalIoDevice* device){ setDevice(Out, device); }
59  void setOutSignalIndex(int index){ setSignalIndex(Out, index); }
60 
61  const std::string& note(){ return note_; }
62  void setNote(const std::string& note){ note_ = note; }
63 
64  bool establishConnection();
65  void releaseConnection();
66 
67  bool read(const Mapping& archive);
68  bool write(Mapping& archive) const;
69 
70 protected:
71  DigitalIoConnection(const DigitalIoConnection& org, CloneMap* cloneMap);
72  virtual Referenced* doClone(CloneMap* cloneMap) const override;
73 
74 private:
75  DigitalIoDevicePtr device_[2];
76  int signalIndex_[2];
77  std::string bodyName_[2];
78  std::string deviceName_[2];
79  std::string note_;
80  ScopedConnection connection;
81 };
83 
84 
85 class CNOID_EXPORT IoConnectionMap : public ClonableReferenced
86 {
87 public:
89 
91  return static_cast<IoConnectionMap*>(doClone(nullptr));
92  }
93  IoConnectionMap* clone(CloneMap& cloneMap) const {
94  return static_cast<IoConnectionMap*>(doClone(&cloneMap));
95  }
96 
97  typedef std::vector<DigitalIoConnectionPtr> container;
98  typedef container::iterator iterator;
99  typedef container::const_iterator const_iterator;
100 
101  iterator begin(){ return connections_.begin(); }
102  const_iterator begin() const { return connections_.begin(); }
103  iterator end(){ return connections_.end(); }
104  const_iterator end() const { return connections_.end(); }
105 
106  int numConnections() const { return connections_.size(); }
107  DigitalIoConnection* connection(int index){ return connections_[index]; }
108  const DigitalIoConnection* connection(int index) const { return connections_[index]; }
109  void insert(int index, DigitalIoConnection* connection);
110  void append(DigitalIoConnection* connection);
111  void remove(DigitalIoConnection* connection);
112 
113  void establishConnections();
114  void releaseConnections();
115 
116  bool read(const Mapping& archive);
117  bool write(Mapping& archive) const;
118 
119 protected:
120  IoConnectionMap(const IoConnectionMap& org, CloneMap* cloneMap);
121  virtual Referenced* doClone(CloneMap* cloneMap) const override;
122 
123 private:
124  std::vector<DigitalIoConnectionPtr> connections_;
125 };
126 
128 
129 }
130 
131 #endif
cnoid::DigitalIoConnection::outDeviceName
const std::string & outDeviceName(int which) const
Definition: IoConnectionMap.h:49
cnoid::DigitalIoConnection::inBodyName
const std::string & inBodyName(int which) const
Definition: IoConnectionMap.h:53
cnoid::DigitalIoDevice
Definition: DigitalIoDevice.h:12
cnoid::Mapping
Definition: ValueTree.h:253
cnoid::IoConnectionMap::iterator
container::iterator iterator
Definition: IoConnectionMap.h:98
cnoid::DigitalIoConnection::setSignalIndex
void setSignalIndex(int which, int index)
Definition: IoConnectionMap.h:44
cnoid::DigitalIoConnection::clone
DigitalIoConnection * clone(CloneMap &cloneMap) const
Definition: IoConnectionMap.h:27
cnoid::IoConnectionMap::end
const_iterator end() const
Definition: IoConnectionMap.h:104
cnoid::write
Listing * write(Mapping *mapping, const std::string &key, const Eigen::MatrixBase< Derived > &x)
Definition: EigenArchive.h:145
cnoid::DigitalIoConnection::outBodyName
const std::string & outBodyName(int which) const
Definition: IoConnectionMap.h:48
cnoid::IoConnectionMap::numConnections
int numConnections() const
Definition: IoConnectionMap.h:106
cnoid::IoConnectionMap::clone
IoConnectionMap * clone() const
Definition: IoConnectionMap.h:90
cnoid::DigitalIoConnection::inDevice
DigitalIoDevice * inDevice()
Definition: IoConnectionMap.h:51
cnoid::IoConnectionMap::clone
IoConnectionMap * clone(CloneMap &cloneMap) const
Definition: IoConnectionMap.h:93
cnoid::ScopedConnection
Definition: Signal.h:279
cnoid::DigitalIoConnection::note
const std::string & note()
Definition: IoConnectionMap.h:61
cnoid::DigitalIoConnection::outDevice
DigitalIoDevice * outDevice()
Definition: IoConnectionMap.h:46
cnoid::DigitalIoConnection::outSignalIndex
int outSignalIndex() const
Definition: IoConnectionMap.h:47
cnoid::DigitalIoConnection::IoType
IoType
Definition: IoConnectionMap.h:31
cnoid::read
bool read(const Mapping *mapping, const std::string &key, Eigen::MatrixBase< Derived > &x)
Definition: EigenArchive.h:43
cnoid::ref_ptr< DigitalIoDevice >
cnoid::IoConnectionMap::end
iterator end()
Definition: IoConnectionMap.h:103
cnoid::DigitalIoConnection::hasDeviceInstances
bool hasDeviceInstances() const
Definition: IoConnectionMap.h:38
cnoid::DigitalIoConnection::setInSignalIndex
void setInSignalIndex(int index)
Definition: IoConnectionMap.h:57
cnoid::DigitalIoConnection
Definition: IoConnectionMap.h:18
cnoid::IoConnectionMap::begin
const_iterator begin() const
Definition: IoConnectionMap.h:102
cnoid::DigitalIoConnection::setOutDevice
void setOutDevice(DigitalIoDevice *device)
Definition: IoConnectionMap.h:58
cnoid::DigitalIoDevicePtr
ref_ptr< DigitalIoDevice > DigitalIoDevicePtr
Definition: DigitalIoDevice.h:75
cnoid::IoConnectionMap
Definition: IoConnectionMap.h:85
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::ClonableReferenced
Definition: ClonableReferenced.h:10
cnoid::DigitalIoConnection::clone
DigitalIoConnection * clone() const
Definition: IoConnectionMap.h:24
cnoid::CloneMap
Definition: CloneMap.h:13
cnoid::IoConnectionMap::connection
const DigitalIoConnection * connection(int index) const
Definition: IoConnectionMap.h:108
cnoid::DigitalIoConnection::setOutSignalIndex
void setOutSignalIndex(int index)
Definition: IoConnectionMap.h:59
cnoid::IoConnectionMap::begin
iterator begin()
Definition: IoConnectionMap.h:101
cnoid::DigitalIoConnectionPtr
ref_ptr< DigitalIoConnection > DigitalIoConnectionPtr
Definition: IoConnectionMap.h:82
cnoid::DigitalIoConnection::signalIndex
int signalIndex(int which) const
Definition: IoConnectionMap.h:34
cnoid::DigitalIoConnection::inSignalIndex
int inSignalIndex() const
Definition: IoConnectionMap.h:52
cnoid::DigitalIoConnection::inDeviceName
const std::string & inDeviceName(int which) const
Definition: IoConnectionMap.h:54
cnoid::IoConnectionMapPtr
ref_ptr< IoConnectionMap > IoConnectionMapPtr
Definition: IoConnectionMap.h:127
cnoid::IoConnectionMap::connection
DigitalIoConnection * connection(int index)
Definition: IoConnectionMap.h:107
cnoid::IoConnectionMap::const_iterator
container::const_iterator const_iterator
Definition: IoConnectionMap.h:99
cnoid::Referenced
Definition: Referenced.h:54
cnoid::DigitalIoConnection::device
DigitalIoDevice * device(int which) const
Definition: IoConnectionMap.h:33
cnoid::DigitalIoConnection::setInDevice
void setInDevice(DigitalIoDevice *device)
Definition: IoConnectionMap.h:56
cnoid::DigitalIoConnection::setNote
void setNote(const std::string &note)
Definition: IoConnectionMap.h:62
cnoid::IoConnectionMap::container
std::vector< DigitalIoConnectionPtr > container
Definition: IoConnectionMap.h:97