orocos_cpp_base
ProxyPort.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <rtt/FlowStatus.hpp>
4 #include <rtt/InputPort.hpp>
5 #include <rtt/OutputPort.hpp>
6 #include "OrocosHelpers.hpp"
7 #include <rtt/TaskContext.hpp>
8 #include <rtt/extras/ReadOnlyPointer.hpp>
9 
10 #define FW_DECLARE_TYPE(type) \
11  extern template class RTT::InputPort<type>; \
12  extern template class RTT::OutputPort<type>; \
13  extern template class OutputProxyPort<type>; \
14  extern template class InputProxyPort<type>; \
15  extern template class RTT::base::ChannelElement<type>;
16 
17 #define INSTANCIATE_TYPE(type) \
18  template class OutputProxyPort<type>; \
19  template class InputProxyPort<type>;
20 
21 template<typename T>
23 
24 template<typename T>
26 
28 {
29 protected:
30  std::string getFreePortName(RTT::TaskContext *clientTask, RTT::base::PortInterface *portIf);
31 };
32 
33 template<typename T>
34 class InputProxyPort : public ProxyPortBase
35 {
36  friend class OutputProxyPort<T>;
37  friend class OutputProxyPort< RTT::extras::ReadOnlyPointer<T> >;
38  RTT::base::InputPortInterface *port;
39  RTT::OutputPort<T> *writer;
40 public:
41  InputProxyPort(RTT::base::PortInterface *iface): port(dynamic_cast<RTT::base::InputPortInterface *>(iface)), writer(NULL)
42  {
43  if(!port)
44  throw std::runtime_error("Flow interface of wrong type given");
45  };
46 
47  InputProxyPort(RTT::TaskContext *task, const std::string &portName): port(NULL), writer(NULL)
48  {
49  RTT::base::PortInterface *iface = task->getPort(portName);
50  if(!iface)
51  throw std::runtime_error("InputProxyPort: Error, task " + task->getName() + " has no port with the name " + portName);
52 
53  port = dynamic_cast<RTT::base::InputPortInterface *>(iface);
54  if(!port)
55  throw std::runtime_error("InputProxyPort: Error, port '" + portName + "' of task '" + task->getName() + "' hast an unexpected type");
56  };
57 
58  RTT::base::InputPortInterface *getPortInterface()
59  {
60  return port;
61  }
62 
63  RTT::OutputPort<T> &getWriter()
64  {
65  return getWriter(port->getDefaultPolicy());
66  }
67 
68  RTT::OutputPort<T> &getWriter(RTT::ConnPolicy const& policy)
69  {
70  if(!writer)
71  {
72  writer = dynamic_cast<RTT::OutputPort<T> * >(port->antiClone());
73  if(!writer)
74  throw std::runtime_error("Error, could not get writer for port " + port->getName());
75 
76  RTT::TaskContext *clientTask = OrocosHelpers::getClientTask();
77  writer->setName(getFreePortName(clientTask, port));
78  clientTask->addPort(*writer);
79  if(!port->connectTo(writer, policy))
80  throw std::runtime_error("InputProxyPort::getWriter(): Error could not connect writer to port " + port->getName() + " of task " + port->getInterface()->getOwner()->getName());
81  }
82  return *writer;
83  };
84 
85  void removeWriter()
86  {
87  if(writer)
88  {
89  writer->disconnect();
90  RTT::TaskContext *clientTask = OrocosHelpers::getClientTask();
91  //this call should also delete the writer
92  clientTask->ports()->removePort(writer->getName());
93  writer = NULL;
94  }
95  }
96 
97  bool disconnect()
98  {
99  port->disconnect();
100  return true;
101  }
102 
104  {
105  port->disconnect(outPut.getPortInterface());
106  return true;
107  }
108 
110  {
111  removeWriter();
112  }
113 };
114 
115 
116 template<typename T>
117 class OutputProxyPort : public ProxyPortBase
118 {
119  friend class InputProxyPort<T>;
120  friend class InputProxyPort< RTT::extras::ReadOnlyPointer<T> >;
121  RTT::base::OutputPortInterface *port;
122  RTT::InputPort<T> *reader;
123 public:
124  OutputProxyPort(RTT::base::PortInterface *iface): port(dynamic_cast<RTT::base::OutputPortInterface *>(iface)), reader(NULL)
125  {
126  if(!port)
127  throw std::runtime_error("Flow interface of wrong type given");
128  };
129 
130  OutputProxyPort(RTT::TaskContext *task, const std::string &portName): port(NULL), reader(NULL)
131  {
132  RTT::base::PortInterface *iface = task->getPort(portName);
133  if(!iface)
134  throw std::runtime_error("OutputProxyPort: Error, task " + task->getName() + " has no port with the name " + portName);
135 
136  port = dynamic_cast<RTT::base::OutputPortInterface *>(iface);
137  if(!port)
138  throw std::runtime_error("OutputProxyPort: Error, port '" + portName + "' of task '" + task->getName() + "' hast an unexpected type");
139  };
140 
141  RTT::base::OutputPortInterface *getPortInterface()
142  {
143  return port;
144  }
145 
146  RTT::InputPort<T> &getReader()
147  {
148  return getReader(RTT::ConnPolicy());
149  }
150 
151  RTT::InputPort<T> &getReader(RTT::ConnPolicy const& policy)
152  {
153  if(!reader)
154  {
155  reader = dynamic_cast<RTT::InputPort<T> *>(port->antiClone());
156  if(!reader)
157  throw std::runtime_error("Error, could not get reader for port " + port->getName());
158  RTT::TaskContext *clientTask = OrocosHelpers::getClientTask();
159  reader->setName(getFreePortName(clientTask, port));
160  clientTask->addPort(*reader);
161  if(!reader->connectTo(port, policy))
162  throw std::runtime_error("InputProxyPort::getReader(): Error could not connect reader to port " + port->getName() + " of task " + port->getInterface()->getOwner()->getName());
163  }
164  return *reader;
165  };
166 
168  {
169  if(reader)
170  {
171  reader->disconnect();
172  RTT::TaskContext *clientTask = OrocosHelpers::getClientTask();
173  //this call should also delete the reader
174  clientTask->ports()->removePort(reader->getName());
175  reader = NULL;
176  }
177  }
178 
179  bool connectTo(InputProxyPort<T> &inputPort, RTT::ConnPolicy const& policy)
180  {
181  return port->connectTo(inputPort.getPortInterface(), policy);
182  }
183 
184  bool connectTo(InputProxyPort<RTT::extras::ReadOnlyPointer<T> > &inputPort, RTT::ConnPolicy const& policy)
185  {
186  return port->connectTo(inputPort.getPortInterface(), policy);
187  }
188 
189  bool connectTo(InputProxyPort<T> &inputPort)
190  {
191  return port->connectTo(inputPort.getPortInterface());
192  }
193 
194 
195  template<typename T2>
196  bool connectTo(InputProxyPort<T2> &inputPort)
197  {
199  return port->connectTo(inputPort.getPortInterface());
200  }
201 
202  bool connectTo(InputProxyPort<RTT::extras::ReadOnlyPointer<T> > &inputPort)
203  {
204  return port->connectTo(inputPort.getPortInterface());
205  }
206 
207  bool disconnect()
208  {
209  port->disconnect();
210  return true;
211  }
212 
213  bool disconnect(InputProxyPort<T> &inputPort)
214  {
215  port->disconnect(inputPort.getPortInterface());
216  return true;
217  }
218 
220  {
221  deleteReader();
222  }
223 };
bool disconnect(InputProxyPort< T > &inputPort)
Definition: ProxyPort.hpp:213
~OutputProxyPort()
Definition: ProxyPort.hpp:219
OutputProxyPort(RTT::TaskContext *task, const std::string &portName)
Definition: ProxyPort.hpp:130
bool connectTo(InputProxyPort< T2 > &inputPort)
Definition: ProxyPort.hpp:196
bool disconnect()
Definition: ProxyPort.hpp:207
InputProxyPort(RTT::TaskContext *task, const std::string &portName)
Definition: ProxyPort.hpp:47
bool disconnect(OutputProxyPort< T > &outPut)
Definition: ProxyPort.hpp:103
bool connectTo(InputProxyPort< RTT::extras::ReadOnlyPointer< T > > &inputPort, RTT::ConnPolicy const &policy)
Definition: ProxyPort.hpp:184
RTT::OutputPort< T > & getWriter(RTT::ConnPolicy const &policy)
Definition: ProxyPort.hpp:68
RTT::OutputPort< T > & getWriter()
Definition: ProxyPort.hpp:63
static RTT::TaskContext * getClientTask()
Definition: OrocosHelpers.cpp:34
void deleteReader()
Definition: ProxyPort.hpp:167
Definition: ProxyPort.hpp:22
RTT::base::InputPortInterface * getPortInterface()
Definition: ProxyPort.hpp:58
RTT::InputPort< T > & getReader()
Definition: ProxyPort.hpp:146
bool connectTo(InputProxyPort< RTT::extras::ReadOnlyPointer< T > > &inputPort)
Definition: ProxyPort.hpp:202
RTT::InputPort< T > & getReader(RTT::ConnPolicy const &policy)
Definition: ProxyPort.hpp:151
bool connectTo(InputProxyPort< T > &inputPort, RTT::ConnPolicy const &policy)
Definition: ProxyPort.hpp:179
void removeWriter()
Definition: ProxyPort.hpp:85
bool connectTo(InputProxyPort< T > &inputPort)
Definition: ProxyPort.hpp:189
Definition: ProxyPort.hpp:25
~InputProxyPort()
Definition: ProxyPort.hpp:109
Definition: ProxyPort.hpp:27
RTT::base::OutputPortInterface * getPortInterface()
Definition: ProxyPort.hpp:141
bool disconnect()
Definition: ProxyPort.hpp:97
std::string getFreePortName(RTT::TaskContext *clientTask, RTT::base::PortInterface *portIf)
Definition: ProxyPort.cpp:4