TPIE

11a2c2d
internal_buffer.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2012, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
23 
24 #ifndef __TPIE_PIPELINING_INTERNAL_BUFFER_H__
25 #define __TPIE_PIPELINING_INTERNAL_BUFFER_H__
26 
27 #include <tpie/pipelining/node.h>
28 #include <tpie/pipelining/factory_helpers.h>
29 #include <tpie/internal_queue.h>
30 #include <tpie/tpie_assert.h>
31 
32 namespace tpie::pipelining {
33 namespace bits {
34 
35 template <typename T>
37  internal_queue<T> * m_queue;
38 public:
39  typedef T item_type;
40 
41  internal_buffer_pull_output_t(const node_token & input_token) {
42  add_pull_source(input_token);
43  set_name("Fetching items", PRIORITY_SIGNIFICANT);
44  // Memory is accouted for by the input node
45  }
46 
47  void propagate() override {
48  m_queue = fetch<internal_queue<T> *>("queue");
49  }
50 
51  bool can_pull() const {
52  return !m_queue->empty();
53  }
54 
55  T pull() {
56  T item=m_queue->front();
57  m_queue->pop();
58  return item;
59  }
60 
61  void end() override {
62  tpie_delete(m_queue);
63  m_queue=NULL;
64  }
65 };
66 
67 template <typename T>
69 public:
70  typedef T item_type;
71 
72  internal_buffer_input_t(const node_token & token, size_t size, size_t additional_item_size=0)
73  : node(token), size(size) {
74  set_name("Storing items", PRIORITY_INSIGNIFICANT);
75  set_minimum_memory(internal_queue<T>::memory_usage(size) + size*additional_item_size);
76  set_plot_options(PLOT_SIMPLIFIED_HIDE);
77  }
78 
79  void propagate() override {
80  m_queue = tpie::tpie_new<internal_queue<item_type> >(size);
81  forward("queue", m_queue, 1);
82  }
83 
84  void push(const T & item) {
85  tp_assert(!m_queue->full(), "Tried to push into full queue");
86  m_queue->push(item);
87  }
88 
89 private:
90  internal_queue<item_type> * m_queue;
91  size_t size;
92 };
93 
94 } //namespace bits
95 
101 template <typename T>
103 public:
104  typedef T item_type;
107 private:
112 
113 public:
125 
126  internal_passive_buffer(size_t size, size_t additional_item_size=0)
127  : size(size)
128  , additional_item_size(additional_item_size) {}
129 
130  input_t raw_input() {
131  return input_t(input_token, size);
132  }
133 
134  output_t raw_output() {
135  return output_t(input_token, size);
136  }
137 
143  return inputfact_t(input_token, size, additional_item_size);
144  }
145 
151  return outputfact_t(input_token);
152  }
153 
154 private:
155  node_token input_token;
156  size_t size, additional_item_size;
157 
160 };
161 
162 } // namespace tpie::pipelining
163 
164 #endif // __TPIE_PIPELINING_INTERNAL_BUFFER_H__
tpie::pipelining
pipelining/factory_base.h Base class of pipelining factories
Definition: ami_glue.h:23
tpie::pipelining::internal_passive_buffer::output
outputpipe_t output()
Return pipe node where items in the buffer can be pulled from Pulling from the empty buffer causes un...
Definition: internal_buffer.h:150
tpie_assert.h
tpie::pipelining::node::add_pull_source
void add_pull_source(const node_token &dest)
Called by implementers to declare a pull source.
tpie::internal_queue::front
const T & front() const
Return the item that has been in the queue for the longest time.
Definition: internal_queue.h:76
tpie::pipelining::pullpipe_begin
Definition: pipe_base.h:442
tpie::pipelining::pipe_end
Definition: pipe_base.h:212
tpie::internal_queue::full
bool full() const
Check if the queue is empty.
Definition: internal_queue.h:105
tpie::internal_queue
A generic internal circular queue.
Definition: internal_queue.h:42
tpie::internal_queue::push
void push(T val)
Add an element to the front of the queue.
Definition: internal_queue.h:88
tpie::pipelining::node::set_minimum_memory
void set_minimum_memory(memory_size_type minimumMemory)
Called by implementers to declare minimum memory requirements.
Definition: node.h:206
tpie::internal_queue::pop
void pop()
Remove an element from the back of the queue.
Definition: internal_queue.h:93
tpie::pipelining::internal_passive_buffer
Internal fifo buffer.
Definition: internal_buffer.h:102
tpie::pipelining::node::set_plot_options
void set_plot_options(flags< PLOT > options)
Set options specified for plot(), as a combination of node::PLOT values.
Definition: node.h:458
tpie::pipelining::node::forward
void forward(std::string key, T value, memory_size_type k=std::numeric_limits< memory_size_type >::max())
Called by implementers to forward auxiliary data to successors.
Definition: node.h:563
tp_assert
#define tp_assert(condition, message)
Definition: tpie_assert.h:65
tpie::pipelining::node::set_name
void set_name(const std::string &name, priority_type priority=PRIORITY_USER)
Set this node's name.
tpie::pipelining::internal_passive_buffer::input
inputpipe_t input()
Return pipe node where items are pushed into the buffer Pushing items into a full buffer causes undef...
Definition: internal_buffer.h:142
tpie::pipelining::bits::internal_buffer_input_t
Definition: internal_buffer.h:68
tpie::pipelining::bits::internal_buffer_pull_output_t::propagate
void propagate() override
Propagate stream metadata.
Definition: internal_buffer.h:47
tpie::internal_queue::empty
bool empty() const
Check if the queue is empty.
Definition: internal_queue.h:99
tpie::pipelining::termfactory
Definition: factory_helpers.h:78
tpie::pipelining::bits::internal_buffer_pull_output_t::end
void end() override
End pipeline processing phase.
Definition: internal_buffer.h:61
tpie::tpie_delete
void tpie_delete(T *p)
Delete an object allocated with tpie_new.
Definition: memory.h:271
tpie::pipelining::bits::internal_buffer_input_t::propagate
void propagate() override
Propagate stream metadata.
Definition: internal_buffer.h:79
tpie::pipelining::node
Base class of all nodes.
Definition: node.h:77
tpie::pipelining::internal_passive_buffer::internal_passive_buffer
internal_passive_buffer(size_t size, size_t additional_item_size=0)
Construct a factory for the buffer.
Definition: internal_buffer.h:126
internal_queue.h
tpie::pipelining::node_token
Definition: tokens.h:292
tpie::pipelining::node::node
node()
Default constructor, using a new node_token.
tpie::pipelining::bits::internal_buffer_pull_output_t
Definition: internal_buffer.h:36