RaftLib  0.3a
C++ Stream Processing Template Library
port.hpp
1 
20 #ifndef _PORT_HPP_
21 #define _PORT_HPP_ 1
22 
23 #include <map>
24 #include <vector>
25 #include <string>
26 #include <utility>
27 #include <typeinfo>
28 #include <typeindex>
29 #include <functional>
30 #include <utility>
31 
32 #include "portbase.hpp"
33 #include "graphtools.hpp"
34 #include "ringbuffertypes.hpp"
35 #include "fifo.hpp"
36 #include "port_info.hpp"
37 #include "ringbuffer.tcc"
38 #include "port_info_types.hpp"
39 #include "portmap_t.hpp"
40 #include "portiterator.hpp"
41 
43 class MapBase;
44 
46 namespace raft{
47  class kernel;
48 }
49 
50 class Port : public PortBase
51 {
52 public:
59  Port( raft::kernel *k );
60 
69  Port( raft::kernel *k, void * const ptr, const std::size_t nbytes );
74  virtual ~Port();
75 
84  template < class T >
85  bool addPort( const std::string port_name )
86  {
93  PortInfo pi( typeid( T ) );
94  pi.my_kernel = kernel;
95  pi.my_name = port_name;
96  (this)->initializeConstMap<T>( pi );
97  const auto ret_val( portmap.map.insert( std::make_pair( port_name,
98  pi ) ) );
99  return( ret_val.second );
100  }
101 
108  template < class T >
109  bool addPorts( const std::size_t n_ports = 0 )
110  {
111  T *existing_buff_t( reinterpret_cast< T* >( alloc_ptr ) );
112  std::size_t length( alloc_ptr_length / sizeof( T ) );
113  const std::size_t inc( length / n_ports );
114  const std::size_t adder( length % n_ports );
115 
116  for( std::size_t index( 0 ); index < n_ports; index++ )
117  {
118  const std::size_t start_index( index * inc );
119  PortInfo pi( typeid( T ),
120  (void*)&( existing_buff_t[ start_index ] ) ,
121  inc + ( index == (n_ports - 1) ? adder : 0 ),
122  start_index );
123  pi.my_kernel = kernel;
124  const std::string name( std::to_string( index ) );
125  pi.my_name = name;
126  (this)->initializeConstMap< T >( pi );
127  portmap.map.insert( std::make_pair( name, pi ) );
128  }
129  return( true );
130  }
131 
142  const std::type_index& getPortType( const std::string port_name );
143 
144 
149  virtual FIFO& operator[]( const std::string port_name );
150 
151 
157  virtual bool hasPorts();
158 
163  virtual PortIterator begin();
164 
169  virtual PortIterator end();
170 
175  std::size_t count();
176 
177 protected:
186  template < class T > void initializeConstMap( PortInfo &pi )
187  {
188  pi.const_map.insert(
189  std::make_pair( Type::Heap , new instr_map_t() ) );
190 
191  pi.const_map[ Type::Heap ]->insert(
192  std::make_pair( false ,
193  RingBuffer< T, Type::Heap, false >::make_new_fifo ) );
194  pi.const_map[ Type::Heap ]->insert(
195  std::make_pair( true ,
196  RingBuffer< T, Type::Heap, true >::make_new_fifo ) );
197 
198  pi.const_map.insert( std::make_pair( Type::SharedMemory, new instr_map_t() ) );
199  pi.const_map[ Type::SharedMemory ]->insert(
200  std::make_pair( false ,
201  RingBuffer< T, Type::SharedMemory >::make_new_fifo ) );
203  return;
204  }
205 
213 
220  PortInfo& getPortInfoFor( const std::string port_name );
221 
227 
231  raft::kernel * kernel = nullptr;
232 
238  void * const alloc_ptr = nullptr;
239 
244  const std::size_t alloc_ptr_length = 0;
245 
247  friend class MapBase;
248  friend class GraphTools;
249 };
250 #endif /* END _PORT_HPP_ */
const std::size_t alloc_ptr_length
Definition: port.hpp:244
void initializeConstMap(PortInfo &pi)
Definition: port.hpp:186
virtual FIFO & operator[](const std::string port_name)
Definition: port.cpp:68
PortInfo & getPortInfoFor(const std::string port_name)
Definition: port.cpp:104
std::map< Type::RingBufferType, instr_map_t * > const_map
Definition: port_info.hpp:135
PortInfo & getPortInfo()
Definition: port.cpp:117
Port(raft::kernel *k)
Definition: port.cpp:33
Definition: mapbase.hpp:98
const std::type_index & getPortType(const std::string port_name)
Definition: port.cpp:57
raft::kernel * kernel
Definition: port.hpp:231
virtual PortIterator begin()
Definition: port.cpp:86
portmap_t portmap
Definition: port.hpp:226
Definition: graphtools.hpp:45
void *const alloc_ptr
Definition: port.hpp:238
Definition: portbase.hpp:28
virtual bool hasPorts()
Definition: port.cpp:80
virtual ~Port()
Definition: port.cpp:48
std::size_t count()
Definition: port.cpp:98
Definition: port.hpp:50
Definition: port_info.hpp:39
virtual PortIterator end()
Definition: port.cpp:92
Definition: kernel.hpp:48
bool addPort(const std::string port_name)
Definition: port.hpp:85
Definition: globalmap.cpp:3
Definition: fifo.hpp:41
Definition: portiterator.hpp:32
bool addPorts(const std::size_t n_ports=0)
Definition: port.hpp:109
Definition: portmap_t.hpp:28