A parser that emits either a ``BitPackedRun`` or a ``RleRun``.
| 193 | |
| 194 | /// A parser that emits either a ``BitPackedRun`` or a ``RleRun``. |
| 195 | class RleBitPackedParser { |
| 196 | public: |
| 197 | /// The different types of runs emitted by the parser |
| 198 | using dynamic_run_type = std::variant<RleRun, BitPackedRun>; |
| 199 | |
| 200 | constexpr RleBitPackedParser() noexcept = default; |
| 201 | |
| 202 | constexpr RleBitPackedParser(const uint8_t* data, rle_size_t data_size, |
| 203 | rle_size_t value_bit_width) noexcept |
| 204 | : data_(data), data_size_(data_size), value_bit_width_(value_bit_width) {} |
| 205 | |
| 206 | constexpr void Reset(const uint8_t* data, rle_size_t data_size, |
| 207 | rle_size_t value_bit_width) noexcept { |
| 208 | *this = {data, data_size, value_bit_width}; |
| 209 | } |
| 210 | |
| 211 | /// Whether there is still runs to iterate over. |
| 212 | /// |
| 213 | /// WARN: Due to simplistic error handling, iteration with Next and Peek could |
| 214 | /// fail to return data while the parser is not exhausted. |
| 215 | /// This is how one can check for errors. |
| 216 | bool exhausted() const { return data_size_ == 0; } |
| 217 | |
| 218 | /// Enum to return from an ``Parse`` handler. |
| 219 | /// |
| 220 | /// Since a callback has no way to know when to stop, the handler must return |
| 221 | /// a value indicating to the ``Parse`` function whether to stop or continue. |
| 222 | enum class ControlFlow { |
| 223 | Continue, |
| 224 | Break, |
| 225 | }; |
| 226 | |
| 227 | /// A callback approach to parsing. |
| 228 | /// |
| 229 | /// This approach is used to reduce the number of dynamic lookups involved with using a |
| 230 | /// variant. |
| 231 | /// |
| 232 | /// The handler must be of the form |
| 233 | /// ```cpp |
| 234 | /// struct Handler { |
| 235 | /// ControlFlow OnBitPackedRun(BitPackedRun run); |
| 236 | /// |
| 237 | /// ControlFlow OnRleRun(RleRun run); |
| 238 | /// }; |
| 239 | /// ``` |
| 240 | template <typename Handler> |
| 241 | void Parse(Handler&& handler); |
| 242 | |
| 243 | private: |
| 244 | /// The pointer to the beginning of the run |
| 245 | const uint8_t* data_ = nullptr; |
| 246 | /// Size in bytes of the run. |
| 247 | rle_size_t data_size_ = 0; |
| 248 | /// The size in bit of a packed value in the run |
| 249 | rle_size_t value_bit_width_ = 0; |
| 250 | |
| 251 | /// Run the handler on the run read and return the number of values read. |
| 252 | /// Does not advance the parser. |
no outgoing calls