| 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | /* SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <[email protected]> */ |
| 3 | |
| 4 | /** |
| 5 | * @file edge-event-buffer.hpp |
| 6 | */ |
| 7 | |
| 8 | #ifndef __LIBGPIOD_CXX_EDGE_EVENT_BUFFER_HPP__ |
| 9 | #define __LIBGPIOD_CXX_EDGE_EVENT_BUFFER_HPP__ |
| 10 | |
| 11 | #if !defined(__LIBGPIOD_GPIOD_CXX_INSIDE__) |
| 12 | #error "Only gpiod.hpp can be included directly." |
| 13 | #endif |
| 14 | |
| 15 | #include <cstddef> |
| 16 | #include <iostream> |
| 17 | #include <memory> |
| 18 | #include <vector> |
| 19 | |
| 20 | namespace gpiod { |
| 21 | |
| 22 | class edge_event; |
| 23 | class line_request; |
| 24 | |
| 25 | /** |
| 26 | * @brief Object into which edge events are read for better performance. |
| 27 | * |
| 28 | * The edge_event_buffer allows reading edge_event objects into an existing |
| 29 | * buffer which improves the performance by avoiding needless memory |
| 30 | * allocations. |
| 31 | */ |
| 32 | class edge_event_buffer final |
| 33 | { |
| 34 | public: |
| 35 | |
| 36 | /** |
| 37 | * @brief Constant iterator for iterating over edge events stored in |
| 38 | * the buffer. |
| 39 | */ |
| 40 | using const_iterator = ::std::vector<edge_event>::const_iterator; |
| 41 | |
| 42 | /** |
| 43 | * @brief Constructor. Creates a new edge event buffer with given |
| 44 | * capacity. |
| 45 | * @param capacity Capacity of the new buffer. |
| 46 | */ |
| 47 | explicit edge_event_buffer(::std::size_t capacity = 64); |
| 48 | |
| 49 | edge_event_buffer(const edge_event_buffer& other) = delete; |
| 50 | |
| 51 | /** |
| 52 | * @brief Move constructor. |
| 53 | * @param other Object to move. |
| 54 | */ |
| 55 | edge_event_buffer(edge_event_buffer&& other) noexcept; |
| 56 | |
| 57 | ~edge_event_buffer(); |
| 58 | |
| 59 | edge_event_buffer& operator=(const edge_event_buffer& other) = delete; |
| 60 | |
| 61 | /** |
| 62 | * @brief Move assignment operator. |
| 63 | * @param other Object to move. |
| 64 | * @return Reference to self. |
| 65 | */ |
| 66 | edge_event_buffer& operator=(edge_event_buffer&& other) noexcept; |
| 67 | |
| 68 | /** |
| 69 | * @brief Get the constant reference to the edge event at given index. |
| 70 | * @param index Index of the event in the buffer. |
| 71 | * @return Constant reference to the edge event. |
| 72 | */ |
| 73 | const edge_event& get_event(unsigned int index) const; |
| 74 | |
| 75 | /** |
| 76 | * @brief Get the number of edge events currently stored in the buffer. |
| 77 | * @return Number of edge events in the buffer. |
| 78 | */ |
| 79 | ::std::size_t num_events() const; |
| 80 | |
| 81 | /** |
| 82 | * @brief Maximum capacity of the buffer. |
| 83 | * @return Buffer capacity. |
| 84 | */ |
| 85 | ::std::size_t capacity() const noexcept; |
| 86 | |
| 87 | /** |
| 88 | * @brief Get a constant iterator to the first edge event currently |
| 89 | * stored in the buffer. |
| 90 | * @return Constant iterator to the first element. |
| 91 | */ |
| 92 | const_iterator begin() const noexcept; |
| 93 | |
| 94 | /** |
| 95 | * @brief Get a constant iterator to the element after the last edge |
| 96 | * event in the buffer. |
| 97 | * @return Constant iterator to the element after the last edge event. |
| 98 | */ |
| 99 | const_iterator end() const noexcept; |
| 100 | |
| 101 | private: |
| 102 | |
| 103 | struct impl; |
| 104 | |
| 105 | ::std::unique_ptr<impl> _m_priv; |
| 106 | |
| 107 | friend line_request; |
| 108 | }; |
| 109 | |
| 110 | /** |
| 111 | * @brief Stream insertion operator for GPIO edge event buffer objects. |
| 112 | * @param out Output stream to write to. |
| 113 | * @param buf GPIO edge event buffer object to insert into the output stream. |
| 114 | * @return Reference to out. |
| 115 | */ |
| 116 | ::std::ostream& operator<<(::std::ostream& out, const edge_event_buffer& buf); |
| 117 | |
| 118 | } /* namespace gpiod */ |
| 119 | |
| 120 | #endif /* __LIBGPIOD_CXX_EDGE_EVENT_BUFFER_HPP__ */ |