flow  3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
pipe.h
Go to the documentation of this file.
1 #if !defined(FLOW_PIPE_H)
2  #define FLOW_PIPE_H
3 
4 #include "named.h"
5 #include "packet.h"
6 
7 #include <memory>
8 #include <queue>
9 #include <string>
10 #include <utility>
11 
15 
16 namespace flow
17 {
18 
19 template<typename T>
20 class inpin;
21 
22 template<typename T>
23 class outpin;
24 
31 template<typename T>
32 class pipe : public named
33 {
34  std::deque<std::unique_ptr<packet<T>>> d_packets;
35 
36  outpin<T> *d_input_p;
37  inpin<T> *d_output_p;
38 
39  size_t d_max_length;
40  size_t d_max_weight;
41 
42  size_t d_weight;
43 
44 public:
52  pipe(const std::string& name_r, outpin<T> *output_p, inpin<T> *input_p, const size_t max_length = 0, const size_t max_weight = 0)
53  : named(name_r), d_input_p(output_p), d_output_p(input_p), d_max_length(max_length), d_max_weight(max_weight), d_weight(0)
54  {}
55 
57  pipe(pipe&& pipe_rr) : named(std::move(pipe_rr)), d_packets(std::move(pipe_rr.d_packets)), d_input_p(std::move(pipe_rr.d_input_p)), d_output_p(std::move(pipe_rr.d_output_p)),
58  d_max_length(std::move(pipe_rr.d_max_length)), d_max_weight(std::move(pipe_rr.d_max_weight)), d_weight(std::move(pipe_rr.d_weight))
59  {}
60 
61  virtual ~pipe() {}
62 
64  virtual outpin<T>* input() const
65  {
66  return d_input_p;
67  }
68 
70  virtual inpin<T>* output() const
71  {
72  return d_output_p;
73  }
74 
76  virtual size_t length() const
77  {
78  return d_packets.size();
79  }
80 
84  virtual size_t max_length() const
85  {
86  return d_max_length;
87  }
88 
90  virtual size_t weight() const
91  {
92  return d_weight;
93  }
94 
98  virtual size_t max_weight() const
99  {
100  return d_max_weight;
101  }
102 
104  virtual size_t cap_length(const size_t max_length)
105  {
106  size_t previous_cap = d_max_length;
107  d_max_length = max_length;
108  return previous_cap;
109  }
110 
112  virtual size_t cap_weight(const size_t max_weight)
113  {
114  size_t previous_cap = d_max_weight;
115  d_max_weight = max_weight;
116  return previous_cap;
117  }
118 
120  virtual size_t flush()
121  {
122  size_t s = d_packets.size();
123  d_packets.clear();
124 
125  return s;
126  }
127 
137  virtual bool push(std::unique_ptr<packet<T>> packet_p)
138  {
139  if(d_max_length && (d_packets.size() == d_max_length)) return false;
140  if(d_max_weight && (d_weight + packet_p->size() > d_max_weight)) return false;
141 
142  d_weight += packet_p->size();
143  d_packets.push_back(std::move(packet_p));
144 
145  return true;
146  }
147 
153  virtual std::unique_ptr<packet<T>> pop()
154  {
155  if(!d_packets.size()) return std::unique_ptr<packet<T>>();
156 
157  std::unique_ptr<packet<T>> packet_p(std::move(d_packets.front()));
158 
159  d_packets.pop_front();
160  d_weight -= packet_p->size();
161 
162  return packet_p;
163  }
164 };
165 
166 }
167 
168 #endif
169 
170 /*
171  (C) Copyright Thierry Seegers 2010-2012. Distributed under the following license:
172 
173  Boost Software License - Version 1.0 - August 17th, 2003
174 
175  Permission is hereby granted, free of charge, to any person or organization
176  obtaining a copy of the software and accompanying documentation covered by
177  this license (the "Software") to use, reproduce, display, distribute,
178  execute, and transmit the Software, and to prepare derivative works of the
179  Software, and to permit third-parties to whom the Software is furnished to
180  do so, all subject to the following:
181 
182  The copyright notices in the Software and this entire statement, including
183  the above license grant, this restriction and the following disclaimer,
184  must be included in all copies of the Software, in whole or in part, and
185  all derivative works of the Software, unless such copies or derivative
186  works are solely in the form of machine-executable object code generated by
187  a source language processor.
188 
189  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
190  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
191  FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
192  SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
193  FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
194  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
195  DEALINGS IN THE SOFTWARE.
196 */
197