File: edge-event-buffer.cpp

package info (click to toggle)
libgpiod 2.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,108 kB
  • sloc: ansic: 26,612; sh: 7,554; cpp: 4,944; python: 2,426; makefile: 811; xml: 49
file content (117 lines) | stat: -rw-r--r-- 2,927 bytes parent folder | download | duplicates (3)
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
// SPDX-License-Identifier: LGPL-2.1-or-later
// SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <brgl@bgdev.pl>

#include <iterator>
#include <ostream>
#include <utility>

#include "internal.hpp"

namespace gpiod {

namespace {

edge_event_buffer_ptr make_edge_event_buffer(unsigned int capacity)
{
	edge_event_buffer_ptr buffer(::gpiod_edge_event_buffer_new(capacity));
	if (!buffer)
		throw_from_errno("unable to allocate the edge event buffer");

	return buffer;
}

} /* namespace */

edge_event_buffer::impl::impl(unsigned int capacity)
	: buffer(make_edge_event_buffer(capacity)),
	  events()
{
	events.reserve(capacity);

	for (unsigned int i = 0; i < capacity; i++) {
		events.push_back(edge_event());
		events.back()._m_priv.reset(new edge_event::impl_external);
	}
}

int edge_event_buffer::impl::read_events(const line_request_ptr& request, unsigned int max_events)
{
	int ret = ::gpiod_line_request_read_edge_events(request.get(),
						       this->buffer.get(), max_events);
	if (ret < 0)
		throw_from_errno("error reading edge events from file descriptor");

	for (int i = 0; i < ret; i++) {
		::gpiod_edge_event* event = ::gpiod_edge_event_buffer_get_event(this->buffer.get(), i);

		dynamic_cast<edge_event::impl_external&>(*this->events[i]._m_priv).event = event;
	}

	return ret;
}

GPIOD_CXX_API edge_event_buffer::edge_event_buffer(::std::size_t capacity)
	: _m_priv(new impl(capacity))
{

}

GPIOD_CXX_API edge_event_buffer::edge_event_buffer(edge_event_buffer&& other) noexcept
	: _m_priv(::std::move(other._m_priv))
{

}

GPIOD_CXX_API edge_event_buffer::~edge_event_buffer()
{

}

GPIOD_CXX_API edge_event_buffer& edge_event_buffer::operator=(edge_event_buffer&& other) noexcept
{
	this->_m_priv = ::std::move(other._m_priv);

	return *this;
}

GPIOD_CXX_API const edge_event& edge_event_buffer::get_event(unsigned int index) const
{
	return this->_m_priv->events.at(index);
}

GPIOD_CXX_API ::std::size_t edge_event_buffer::num_events() const
{
	return ::gpiod_edge_event_buffer_get_num_events(this->_m_priv->buffer.get());
}

GPIOD_CXX_API ::std::size_t edge_event_buffer::capacity() const noexcept
{
	return ::gpiod_edge_event_buffer_get_capacity(this->_m_priv->buffer.get());
}

GPIOD_CXX_API edge_event_buffer::const_iterator edge_event_buffer::begin() const noexcept
{
	return this->_m_priv->events.begin();
}

GPIOD_CXX_API edge_event_buffer::const_iterator edge_event_buffer::end() const noexcept
{
	return this->_m_priv->events.begin() + this->num_events();
}

GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, const edge_event_buffer& buf)
{
	out << "gpiod::edge_event_buffer(num_events=" << buf.num_events() <<
	       ", capacity=" << buf.capacity() <<
	       ", events=[";

	::std::copy(buf.begin(), ::std::prev(buf.end()),
		    ::std::ostream_iterator<edge_event>(out, ", "));
	out << *(::std::prev(buf.end()));

	out << "])";

	return out;
}

} /* namespace gpiod */