ipcq 0.12.0
Loading...
Searching...
No Matches
adapter.hpp
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup ipcq
4 * @brief Provides iterator adapter for `ipcq::BasicReader`.
5 * @copyright
6 * SPDX-FileCopyrightText: 2020-2020 European Southern Observatory (ESO)
7 *
8 * SPDX-License-Identifier: LGPL-3.0-only
9 */
10#ifndef IPCQ_ADAPTER_HPP_
11#define IPCQ_ADAPTER_HPP_
12#include <iterator>
13
14namespace ipcq {
15
16/**
17 * Primary template for @a InputIterators.
18 * This IteratorTraits::value_type is the container type even for @a OutputIterators.
19 *
20 * @ingroup ipcq
21 */
22template <class Iterator, class=void>
23struct IteratorTraits : std::iterator_traits<Iterator>{
24};
25
26/**
27 * Template specialization for @a OutputIterators for which normal std::iterator_traits::value_type
28 * is void.
29 *
30 * @ingroup ipcq
31 */
32template <class Iterator>
33struct IteratorTraits<Iterator, std::void_t<typename Iterator::container_type>>
34 : std::iterator_traits<typename Iterator::container_type::iterator> {
35};
36
37/**
38 * Adapts either @a @c LegacyOutputIterator or @a @c Iterator type (e.g. @c
39 * std::back_insert_iterator or @c std::iterator) to use in @c BasicReader::Read and @c
40 * BasicReader::Read.
41 *
42 * Example that does in-place assignment:
43 *
44 * std::vector<Topic> buffer(5);
45 * reader.Read(ipcq::OutputIteratorAdapter(std::begin(buffer)), 5, 5s);
46 *
47 * @see ipcq::BackInserter for the variant that does insertion with @c Container::push_back.
48 *
49 * @ingroup ipcq
50 */
51template <class Iterator>
54
55 constexpr OutputIteratorAdapter(Iterator iter) : iterator(std::move(iter)) {
56 }
57
58 /**
59 * Assigns @c value to *iterator and then increments the iterator.
60 *
61 * @code *iterator++ = value; @endcode
62 *
63 */
64 constexpr void operator()(ValueType const& value) {
65 *iterator++ = value;
66 }
67 Iterator iterator;
68};
69
70/**
71 * Convenience function that creates an OutputIteratorAdapter with a `std::back_insert_iterator` for
72 * a container.
73 *
74 * Equivalent to:
75 *
76 * OutputIteratorAdapter(std::back_insert_iterator(container));
77 *
78 * Example:
79 *
80 * std::vector<Topic> buffer;
81 * buffer.reserve(BATCH_SIZE);
82 * reader.Read(ipcq::BackInserter(buffer), BATCH_SIZE, 5s);
83 *
84 * @sa ipcq::OutputIteratorAdapter
85 *
86 * @ingroup ipcq
87 */
88template <class Container>
89constexpr auto
91 using Iterator = std::back_insert_iterator<Container>;
92 return OutputIteratorAdapter<Iterator>(Iterator{c});
93}
94
95} // namespace ipcq
96
97#endif // #ifndef IPCQ_ADAPTER_HPP_
constexpr auto BackInserter(Container &c) -> OutputIteratorAdapter< std::back_insert_iterator< Container > >
Convenience function that creates an OutputIteratorAdapter with a std::back_insert_iterator for a con...
Definition adapter.hpp:90
Definition error.hpp:135
Primary template for InputIterators.
Definition adapter.hpp:23
Adapts either LegacyOutputIterator or Iterator type (e.g.
Definition adapter.hpp:52
typename IteratorTraits< Iterator >::value_type ValueType
Definition adapter.hpp:53
constexpr OutputIteratorAdapter(Iterator iter)
Definition adapter.hpp:55
constexpr void operator()(ValueType const &value)
Assigns value to *iterator and then increments the iterator.
Definition adapter.hpp:64