ipcq 0.12.0
Loading...
Searching...
No Matches
basicExample.cpp
1/**
2 * @file
3 * @copyright
4 * SPDX-FileCopyrightText: 2023-2023 European Southern Observatory (ESO)
5 *
6 * SPDX-License-Identifier: LGPL-3.0-only
7 */
8#include <array>
9#include <cstdint>
10
11#include <ipcq/reader.hpp>
12#include <ipcq/writer.hpp>
13
14struct MyElement {
15 enum class Status {
18 };
20 std::size_t size;
21 std::array<float, 64 * 64> data;
22};
23
24int main() {
25 auto capacity = 64u;
26 ipcq::Writer<MyElement> writer("topic", capacity);
27 ipcq::Reader<MyElement> reader("topic");
28
29 // Write
30 {
32 auto ec = writer.Write(data, ipcq::Notify::All);
33 if (ec) {
34 std::cerr << "Failed to write to queue: " << ec.message() << std::endl;
35 return 1;
36 }
37 }
38
39 // Read
40 {
41 using namespace std::chrono_literals;
42 MyElement out;
43 auto [ec, count] =
44 reader.Read([&out](MyElement const& in) noexcept { out = in; }, 1u, 10ms);
45 if (ec) {
46 std::cerr << "Failed to read from queue: " << ec.message() << std::endl;
47 return 1;
48 } else {
49 // out is OK
50 // ...
51 }
52 }
53}
std::pair< std::error_code, size_t > Read(Operation &&op, size_t count, std::chrono::duration< Rep, Period > timeout) noexcept(std::is_nothrow_invocable_v< Operation, T const & >)
Reads available data if any, or waits for notification from writer, and then reads available data fro...
Definition reader.hpp:321
std::error_code Write(T const &data, Notify notify_readers) noexcept
Append single element to queue, possibly overwriting the front element.
Definition writer.hpp:165
BasicReader< T, detail::BoostConditionPolicy > Reader
Convenience alias with reasonable default condition policy.
Definition reader.hpp:489
BasicWriter< T, BoostConditionPolicy > Writer
Convenience alias with reasonable default condition policy.
Definition writer.hpp:230
@ All
Notify all waiting readers.
Definition writer.hpp:31
Contains definition of ipcq::BasicReader.
Status status
std::array< float, 64 *64 > data
std::size_t size
Contains declarations for ipcq::BasicWriter.