RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
CoalescingOutputIterator.h
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2024 Roman Lebedev
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20
21#pragma once
22
23#include "adt/Bit.h"
24#include "adt/Invariant.h"
25#include "io/Endianness.h"
26#include <concepts>
27#include <cstddef>
28#include <cstdint>
29#include <iterator>
30#include <type_traits>
31
32namespace rawspeed {
33
34template <typename UnderlyingOutputIterator, typename PartType = uint8_t,
35 typename CoalescedType =
36 typename UnderlyingOutputIterator::container_type::value_type>
37 requires(std::output_iterator<UnderlyingOutputIterator, CoalescedType> &&
38 std::unsigned_integral<CoalescedType> &&
39 std::unsigned_integral<PartType> &&
40 sizeof(PartType) <= sizeof(CoalescedType) &&
41 sizeof(CoalescedType) % sizeof(PartType) == 0)
43 UnderlyingOutputIterator it;
44
45 CoalescedType cache = 0;
46 int occupancy = 0;
47
48 static constexpr int MaxOccupancy = bitwidth<CoalescedType>();
49
55
60 return;
62 ++it;
63 cache = 0;
64 occupancy = 0;
65 }
66
68 using value_type = PartType;
69 };
70
71public:
72 using iterator_concept = std::output_iterator_tag;
73 using value_type = void;
74 using difference_type = ptrdiff_t;
75 using pointer = void;
76 using reference = void;
77
79
81
82 template <typename U>
83 requires std::same_as<UnderlyingOutputIterator, std::remove_reference_t<U>>
84 explicit CoalescingOutputIterator(U&& it_) : it(std::forward<U>(it_)) {}
85
87 : it(other.it) {
88 invariant(occupancy == 0);
89 invariant(other.occupancy == 0);
90 }
91
92 // NOLINTBEGIN(performance-move-constructor-init)
96 // NOLINTEND(performance-move-constructor-init)
97
99 invariant(occupancy == 0);
100 invariant(other.occupancy == 0);
101 CoalescingOutputIterator tmp(other);
102 std::swap(*this, tmp);
103 return *this;
104 }
107 *this = static_cast<const CoalescingOutputIterator&>(other);
108 return *this;
109 }
110
113 if (occupancy == 0)
114 return;
115 int numPaddingBits = MaxOccupancy - occupancy;
116 invariant(numPaddingBits > 0);
117 invariant(numPaddingBits < MaxOccupancy);
118 occupancy += numPaddingBits;
121 invariant(occupancy == 0);
122 }
123
126 return *this;
127 }
128
133
134 // NOLINTNEXTLINE(cert-dcl21-cpp)
137 return *this;
138 }
139
140 template <typename U>
141 requires std::same_as<U, PartType>
146 static_assert(bitwidth<U>() <= MaxOccupancy);
147 part = getLE<U>(&part);
148 cache |= static_cast<CoalescedType>(part) << occupancy;
152 return *this;
153 }
154};
155
156// CTAD deduction guide
157template <typename T>
159
160} // namespace rawspeed
#define invariant(expr)
Definition Invariant.h:27
CoalescingOutputIterator & operator=(const CoalescingOutputIterator &other)
CoalescingOutputIterator & operator=(CoalescingOutputIterator &&other) noexcept
CoalescingOutputIterator(const CoalescingOutputIterator &other)
CoalescingOutputIterator(CoalescingOutputIterator &&other) noexcept
CoalescingOutputIterator & operator=(U part)
CoalescingOutputIterator operator++(int)
throw T(buf.data())
CoalescingOutputIterator(T) -> CoalescingOutputIterator< T >
constexpr unsigned RAWSPEED_READNONE bitwidth(T unused={})
Definition Bit.h:43
T getLE(const void *data)
Definition Endianness.h:120