ipcq 0.12.0
Loading...
Searching...
No Matches
batchExample.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 <chrono>
9
10#include <ipcq/adapter.hpp>
11#include <ipcq/reader.hpp>
12#include <ipcq/writer.hpp>
13
14struct MyElement {
15 std::array<uint8_t, 128> data;
16};
17
18/** Writer side */
19void Writer() {
20 auto capacity = 64u;
21 ipcq::Writer<MyElement> writer("myqueue", capacity);
22 MyElement element = {};
23 // Prepare element
24 auto err = writer.Write(element, ipcq::Notify::All);
25 if (err) {
26 // handle error
27 }
28}
29
30/** Reader side */
31void Reader() {
32 using namespace std::chrono_literals;
33 // Create reader, waiting up to 30s for the topic to be created if it does not exist.
34 // If the topic is not created after that it will throw `std::system_error` with error
35 // code `ipcq::Error::Timeout`
36 auto reader = ipcq::Reader<MyElement>::MakeReader("myqueue", 30s);
37
38 std::array<MyElement, 40> elements;
39
40 // Read *up to* 40 elements, using the adapter utility that performs assignment
41 // for each element into the array.
42 // If there is no data at all yet we wait 2s before giving up (returning `ipcq::Error::Timeout`)
43 auto [err, count] =
44 reader.Read(ipcq::OutputIteratorAdapter(std::begin(elements)), elements.size(), 2s);
45
46 if (err) {
47 // Handle errors.
48 // Particularly ipcq::Error::InconsistentState indicates that writer has broken the
49 // sequential reads by `reader`. This is recovered by resetting the internal state to read
50 // the next written element by invoking `ipcq::BasicReader::Reset()`.
51 } else {
52 // Success
53 // `count` elements have been copied.
54 // process result ...
55 }
56}
Provides iterator adapter for ipcq::BasicReader.
static BasicReader MakeReader(char const *topic_name, std::chrono::duration< Rep, Period > timeout)
Definition reader.hpp:190
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
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.
std::array< float, 64 *64 > data
Adapts either LegacyOutputIterator or Iterator type (e.g.
Definition adapter.hpp:52
Contains declarations for ipcq::BasicWriter.