Skip to content

Commit 8d54467

Browse files
committed
refactor: Simplify Packer and Unpacker usage by removing unnecessary converting iterator
1 parent cfb2518 commit 8d54467

2 files changed

Lines changed: 3 additions & 39 deletions

File tree

examples/first_example.cpp

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,15 @@
99
#include <map>
1010
#include <msgpack23/msgpack23.h>
1111

12-
class converting_insert_iterator final {
13-
public:
14-
using difference_type = std::ptrdiff_t;
15-
16-
constexpr explicit converting_insert_iterator(std::back_insert_iterator<std::vector<unsigned char>> &&iterator) : store_{std::move(iterator)} {}
17-
18-
constexpr converting_insert_iterator &operator=(const std::byte &value) {
19-
store_ = std::to_underlying(value);
20-
return *this;
21-
}
22-
23-
constexpr converting_insert_iterator &operator=(std::byte &&value) {
24-
store_ = std::to_underlying(value);
25-
return *this;
26-
}
27-
28-
[[nodiscard]] constexpr converting_insert_iterator &operator*() {
29-
return *this;
30-
}
31-
32-
constexpr converting_insert_iterator &operator++() {
33-
return *this;
34-
}
35-
36-
constexpr converting_insert_iterator operator++(int) {
37-
return *this;
38-
}
39-
private:
40-
std::back_insert_iterator<std::vector<unsigned char>> store_;
41-
};
42-
4312
int main() {
4413
std::map<std::string, int> const original {{"apple", 1}, {"banana", 2}};
4514

4615
std::vector<unsigned char> data{};
47-
msgpack23::Packer packer { converting_insert_iterator{ std::back_inserter(data) } };
16+
msgpack23::Packer packer { std::back_inserter(data) };
4817
packer(original);
4918

5019
std::map<std::string, int> unpacked;
51-
msgpack23::Unpacker unpacker {
52-
std::span<std::byte>{
53-
reinterpret_cast<std::byte *>(data.data()),
54-
data.size()
55-
}
56-
};
20+
msgpack23::Unpacker unpacker { data };
5721
unpacker(unpacked);
5822

5923
for (auto const& [key, value] : unpacked) {

include/msgpack23/msgpack23.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ namespace msgpack23 {
518518
}
519519

520520
[[nodiscard]] bool check_constant(FormatConstants const &value) const {
521-
return current() == static_cast<B>(std::to_underlying(value));
521+
return static_cast<B>(current()) == static_cast<B>(std::to_underlying(value));
522522
}
523523

524524
[[nodiscard]] FormatConstants current_constant() const {

0 commit comments

Comments
 (0)