Vuo  2.3.2
VuoComposition.cc
Go to the documentation of this file.
1 
10 #include "VuoComposition.hh"
11 #include "VuoCable.hh"
13 #include "VuoProtocol.hh"
14 #include "VuoPublishedPort.hh"
15 #include "VuoStringUtilities.hh"
16 
22 {
23  metadata = new VuoCompositionMetadata();
24  ownsMetadata = true;
25  directory = "";
26 }
27 
28 VuoComposition::~VuoComposition(void)
29 {
30  if (ownsMetadata)
31  delete metadata;
32 }
33 
40 void VuoComposition::setMetadata(VuoCompositionMetadata *metadata, bool takeOwnership)
41 {
42  if (ownsMetadata)
43  delete this->metadata;
44 
45  this->metadata = metadata;
46  ownsMetadata = takeOwnership;
47 }
48 
53 {
54  return metadata;
55 }
56 
60 void VuoComposition::setDirectory(string directory)
61 {
62  this->directory = directory;
63 }
64 
69 {
70  return directory;
71 }
72 
77 {
78  nodes.insert(node);
79 }
80 
87 {
88  nodes.erase(node);
89 }
90 
94 set<VuoNode *> VuoComposition::getNodes(void)
95 {
96  return nodes;
97 }
98 
105 {
106  cables.insert(cable);
107 }
108 
113 {
114  cable->setFrom(NULL, NULL);
115  cable->setTo(NULL, NULL);
116 
117  cables.erase(cable);
118 }
119 
123 set<VuoCable *> VuoComposition::getCables(void)
124 {
125  return cables;
126 }
127 
132 {
133  comments.insert(comment);
134 }
135 
140 {
141  comments.erase(comment);
142 }
143 
147 set<VuoComment *> VuoComposition::getComments(void)
148 {
149  return comments;
150 }
151 
156 {
157  publishedInputPorts.insert(publishedInputPorts.begin() + index, port);
158 }
159 
164 {
165  publishedOutputPorts.insert(publishedOutputPorts.begin() + index, port);
166 }
167 
172 {
173  publishedInputPorts.erase(publishedInputPorts.begin() + index);
174 }
175 
180 {
181  publishedOutputPorts.erase(publishedOutputPorts.begin() + index);
182 }
183 
187 vector<VuoPublishedPort *> VuoComposition::getPublishedInputPorts(void)
188 {
189  return publishedInputPorts;
190 }
191 
195 vector<VuoPublishedPort *> VuoComposition::getPublishedOutputPorts(void)
196 {
197  return publishedOutputPorts;
198 }
199 
204 {
205  return getPublishedPortWithName(name, true);
206 }
207 
212 {
213  return getPublishedPortWithName(name, false);
214 }
215 
219 VuoPublishedPort * VuoComposition::getPublishedPortWithName(string name, bool isInput)
220 {
221  vector<VuoPublishedPort *> publishedPorts = (isInput ? publishedInputPorts : publishedOutputPorts);
222  for (vector<VuoPublishedPort *>::iterator i = publishedPorts.begin(); i != publishedPorts.end(); ++i)
223  {
224  VuoPublishedPort *port = *i;
225  if (port->getClass()->getName() == name)
226  return port;
227  }
228  return NULL;
229 }
230 
235 {
236  vector<VuoPublishedPort *> publishedPorts = (isInput ? getPublishedInputPorts() : getPublishedOutputPorts());
237  vector<VuoPublishedPort *>::iterator foundPort = find(publishedPorts.begin(), publishedPorts.end(), port);
238  if (foundPort != publishedPorts.end())
239  return std::distance(publishedPorts.begin(), foundPort);
240  else
241  return -1;
242 }
243 
249 vector<VuoPublishedPort *> VuoComposition::getProtocolAwarePublishedPortOrder(VuoProtocol *protocol, bool publishedInputs)
250 {
251  vector<VuoPublishedPort *> publishedPorts = (publishedInputs? publishedInputPorts : publishedOutputPorts);
252  vector<VuoPublishedPort *> sortedPublishedPorts;
253 
254  // First list published ports that are part of the target protocol.
255  if (protocol)
256  {
257  vector<pair<string, string> > protocolPorts = (publishedInputs?
258  protocol->getInputPortNamesAndTypes() :
259  protocol->getOutputPortNamesAndTypes());
260 
261  for (vector<pair<string, string> >::const_iterator i = protocolPorts.begin(); i != protocolPorts.end(); ++i)
262  {
263  string protocolPortName = i->first;
264  VuoPublishedPort *compliantPublishedPort = (publishedInputs?
265  getPublishedInputPortWithName(protocolPortName) :
266  getPublishedOutputPortWithName(protocolPortName));
267  if (compliantPublishedPort)
268  sortedPublishedPorts.push_back(compliantPublishedPort);
269  }
270  }
271 
272  // Next list published ports that are not part of the target protocol.
273  for (vector<VuoPublishedPort *>::const_iterator port = publishedPorts.begin(); port != publishedPorts.end(); ++port)
274  {
275  if (std::find(sortedPublishedPorts.begin(), sortedPublishedPorts.end(), *port) == sortedPublishedPorts.end())
276  sortedPublishedPorts.push_back(*port);
277  }
278 
279  return sortedPublishedPorts;
280 }
281