File: buffered_lane.h

package info (click to toggle)
wsclean 2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,196 kB
  • sloc: cpp: 34,504; ansic: 234; python: 174; makefile: 10
file content (122 lines) | stat: -rw-r--r-- 2,165 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef BUFFER_LANE_H
#define BUFFER_LANE_H

#include <vector>

#include "lane.h"

template<typename Tp>
class lane_write_buffer 
{
public:
	typedef typename ao::lane<Tp>::size_type size_type;
	typedef typename ao::lane<Tp>::value_type value_type;
	
	lane_write_buffer() : _buffer_size(0), _lane(0)
	{ }
	
	lane_write_buffer(ao::lane<Tp>* lane, size_type buffer_size) : _buffer_size(buffer_size), _lane(lane)
	{
		_buffer.reserve(buffer_size);
	}
	
	~lane_write_buffer()
	{
		flush();
	}
	
	void reset(ao::lane<Tp>* lane, size_type buffer_size)
	{
		_buffer.clear();
		_buffer.reserve(buffer_size);
		_buffer_size = buffer_size;
		_lane = lane;
	}
	
	void clear()
	{
		_lane->clear();
		_buffer.clear();
	}
		
	void write(const value_type& element)
	{
		_buffer.push_back(element);
		if(_buffer.size() == _buffer_size)
			flush();
	}
	
	void write(value_type&& element)
	{
		_buffer.push_back(std::move(element));
		if(_buffer.size() == _buffer_size)
			flush();
	}
	
	template<typename... Args>
	void emplace(Args&&... args)
	{
		_buffer.emplace_back(args...);
		if(_buffer.size() == _buffer_size)
			flush();
	}
	
	void write_end()
	{
		flush();
		_lane->write_end();
	}
		
	void flush()
	{
		_lane->move_write(&_buffer[0], _buffer.size());
		_buffer.clear();
	}
private:
	size_type _buffer_size;
	std::vector<value_type> _buffer;
	ao::lane<Tp>* _lane;
};

template<typename Tp>
class lane_read_buffer 
{
public:
	lane_read_buffer(ao::lane<Tp>* lane, size_t buffer_size) :
		_buffer(new Tp[buffer_size]),
		_buffer_size(buffer_size),
		_buffer_pos(0),
		_buffer_fill_count(0),
		_lane(lane)
	{
	}
	
	~lane_read_buffer()
	{
		delete[] _buffer;
	}
	
	bool read(Tp& element)
	{
		if(_buffer_pos == _buffer_fill_count)
		{
			_buffer_fill_count = _lane->read(_buffer, _buffer_size);
			_buffer_pos = 0;
			if(_buffer_fill_count == 0)
				return false;
		}
		element = std::move(_buffer[_buffer_pos]);
		++_buffer_pos;
		return true;
	}
	
private:
	lane_read_buffer(const lane_read_buffer&) = delete;
	lane_read_buffer& operator=(const lane_read_buffer&) = delete;

	Tp* _buffer;
	size_t _buffer_size, _buffer_pos, _buffer_fill_count;
	ao::lane<Tp>* _lane;
};

#endif