SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PortManager.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2012 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9  License for the specific language governing rights and limitations under
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 /// @todo Documentation Dataflow/Network/PortManager.h
30 
31 
32 #ifndef DATAFLOW_NETWORK_PORTMANAGER_H
33 #define DATAFLOW_NETWORK_PORTMANAGER_H
34 
35 #include <Dataflow/Network/Port.h>
36 #include <Core/Utils/Exception.h>
37 
38 #include <boost/foreach.hpp>
39 #include <boost/range/adaptors.hpp>
40 #include <boost/range/algorithm/copy.hpp>
41 #include <string>
42 #include <map>
43 
44 namespace SCIRun {
45 namespace Dataflow {
46 namespace Networks {
47 
48 template<class T>
49 class PortManager : boost::noncopyable
50 {
51 private:
52  typedef std::map<PortId, T> PortMap;
53  PortMap ports_;
54  ModuleInterface* module_;
55 
56 public:
57  PortManager();
58  size_t size() const;
59  size_t add(const T& item);
60  void remove(const PortId& id);
61  T operator[](const PortId& id) const;
62  std::vector<T> operator[](const std::string& name) const;
63  bool hasPort(const PortId& id) const;
64  void set_module(ModuleInterface* mod) { module_ = mod; }
65  std::vector<T> view() const;
66 };
67 
69 
70 template<class T>
72  module_(0)
73 {
74 }
75 
76 template<class T>
77 size_t
79 {
80  return ports_.size();
81 }
82 
83 template<class T>
84 size_t
85 PortManager<T>::add(const T& item)
86 {
87  ports_[item->id()] = item;
88  /// @todo: who should manage port indexes?
89  //item->setIndex(size() - 1);
90  auto index = size() - 1;
91  return index;
92 }
93 
94 template<class T>
95 void
97 {
98  auto it = ports_.find(id);
99  if (it == ports_.end())
100  {
101  std::ostringstream ostr;
102  ostr << "PortManager tried to remove a port that does not exist: " << id;
103  BOOST_THROW_EXCEPTION(PortOutOfBoundsException() << Core::ErrorMessage(ostr.str()));
104  }
105  ports_.erase(it);
106  size_t i = 0;
107  BOOST_FOREACH(typename PortMap::value_type& portPair, ports_)
108  portPair.second->setIndex(i++);
109 }
110 
111 template<class T>
112 T
114 {
115  auto it = ports_.find(id);
116  if (it == ports_.end())
117  {
118  /// @todo: need a way to detect and create arbitrary dynamic ports from serialized files.
119  //if (id.dynamic)
120  // std::cout << "DYNAMIC PORT NEEDS TO INSERT ITSELF HERE SOMEHOW" << std::endl;
121  //else
122  // std::cout << "HELLO NOT SETTING PORT FLAGS CORRECT" << std::endl;
123  std::ostringstream ostr;
124  ostr << "PortManager tried to access a port that does not exist: " << id;
125  BOOST_THROW_EXCEPTION(PortOutOfBoundsException() << Core::ErrorMessage(ostr.str()));
126  }
127  return it->second;
128 }
129 
130 template<class T>
131 std::vector<T> PortManager<T>::operator[](const std::string& name) const
132 {
133  std::vector<T> portsWithName;
134 
135  boost::copy(
136  ports_ | boost::adaptors::map_values
137  | boost::adaptors::filtered([&](const T& port) { return port->get_portname() == name; }), std::back_inserter(portsWithName));
138 
139  if (portsWithName.empty())
140  {
141  BOOST_THROW_EXCEPTION(PortOutOfBoundsException() << Core::ErrorMessage("PortManager does not contain a port by name: " + name));
142  }
143 
144  return portsWithName;
145 }
146 
147 template <class T>
148 std::vector<T> PortManager<T>::view() const
149 {
150  std::vector<T> portVector;
151  boost::copy(ports_ | boost::adaptors::map_values, std::back_inserter(portVector));
152  std::sort(portVector.begin(), portVector.end(), [](const T& lhs, const T& rhs) { return lhs->getIndex() < rhs->getIndex(); });
153  return portVector;
154 }
155 
156 template <class T>
157 bool PortManager<T>::hasPort(const PortId& id) const
158 {
159  return ports_.find(id) != ports_.end();
160 }
161 
162 }}}
163 
164 #endif
Definition: ModuleInterface.h:81
void set_module(ModuleInterface *mod)
Definition: PortManager.h:64
boost::error_info< struct tag_error_message, std::string > ErrorMessage
Definition: Exception.h:52
size_t size() const
Definition: PortManager.h:78
std::vector< T > view() const
Definition: PortManager.h:148
bool hasPort(const PortId &id) const
Definition: PortManager.h:157
Definition: Exception.h:46
const char * name[]
Definition: BoostGraphExampleTests.cc:87
size_t add(const T &item)
Definition: PortManager.h:85
void remove(const PortId &id)
Definition: PortManager.h:96
T operator[](const PortId &id) const
Definition: PortManager.h:113
Definition: ModuleDescription.h:45
PortManager()
Definition: PortManager.h:71
int size
Definition: eabLatVolData.py:2
Definition: PortManager.h:49