Skip to content

Commit 6f88151

Browse files
author
me
committed
- Added check_byte
- Added final keyword everywhere. In theory this can help with optimization and LTO. I'm really not liking the use of inheritance. That's such a code smell in 2025
1 parent 7f4a0fe commit 6f88151

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

src/msgpack.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,6 @@ namespace msgpackcpp
136136
void unpack(source_base& in);
137137
static value unpack_static(source_base& in);
138138
};
139-
140-
//----------------------------------------------------------------------------------------------------------------
141-
142-
template<class Byte>
143-
constexpr bool is_byte = std::is_same_v<Byte, char> ||
144-
std::is_same_v<Byte, uint8_t> ||
145-
std::is_same_v<Byte, int8_t>;
146139

147140
//----------------------------------------------------------------------------------------------------------------
148141

@@ -293,6 +286,16 @@ namespace msgpackcpp
293286
}
294287
#endif
295288

289+
//----------------------------------------------------------------------------------------------------------------
290+
291+
template<class Byte>
292+
constexpr bool is_byte = std::is_same_v<Byte, char> ||
293+
std::is_same_v<Byte, uint8_t> ||
294+
std::is_same_v<Byte, int8_t>;
295+
296+
template<class Byte>
297+
using check_byte = std::enable_if_t<is_byte<Byte>, bool>;
298+
296299
//----------------------------------------------------------------------------------------------------------------
297300

298301
constexpr uint16_t byte_swap16(uint16_t v)
@@ -501,7 +504,7 @@ namespace msgpackcpp
501504
}
502505
else
503506
{
504-
// negative - int64_T
507+
// negative - int64_t
505508
constexpr uint8_t format = MSGPACK_I64;
506509
const uint64_t v64 = host_to_b64(bit_cast<uint64_t>(static_cast<int64_t>(v)));
507510
out.write((const char*)&format, 1);

src/msgpack_sinks.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace msgpackcpp
1212
//----------------------------------------------------------------------------------------------------------------
1313

1414
template<class Byte, class Alloc>
15-
struct sink_vector : sink_base
15+
struct sink_vector final : sink_base
1616
{
1717
static_assert(is_byte<Byte>, "Byte needs to be a byte");
1818
std::vector<Byte,Alloc>& buf;
@@ -26,77 +26,77 @@ namespace msgpackcpp
2626
};
2727

2828
template<class Byte, class Alloc>
29-
struct source_vector : source_base
29+
struct source_vector final : source_base
3030
{
3131
static_assert(is_byte<Byte>, "Byte needs to be a byte");
3232
const std::vector<Byte,Alloc>& data;
3333
size_t offset{0};
3434

3535
source_vector(const std::vector<Byte,Alloc>& data_) : data{data_} {}
3636

37-
void read(char* buf, size_t nbytes) override
37+
void read(char* buf, size_t nbytes) override final
3838
{
3939
if ((data.size() - offset) < nbytes)
4040
throw std::system_error(OUT_OF_DATA);
4141
std::memcpy(buf, data.data() + offset, nbytes);
4242
offset += nbytes;
4343
}
4444

45-
uint8_t peak() override
45+
uint8_t peak() override final
4646
{
4747
return static_cast<uint8_t>(data[offset]);
4848
}
4949

50-
size_t remaining() const override
50+
size_t remaining() const override final
5151
{
5252
return data.size() - offset;
5353
}
5454
};
5555

56-
template<class Byte, class Alloc, std::enable_if_t<is_byte<Byte>, bool> = true>
56+
template<class Byte, class Alloc, check_byte<Byte> = true>
5757
auto sink(std::vector<Byte, Alloc>& buf)
5858
{
5959
return sink_vector<Byte,Alloc>{buf};
6060
}
6161

62-
template<class Byte, class Alloc, std::enable_if_t<is_byte<Byte>, bool> = true>
62+
template<class Byte, class Alloc, check_byte<Byte> = true>
6363
auto source(const std::vector<Byte, Alloc>& buf)
6464
{
6565
return source_vector<Byte,Alloc>{buf};
6666
}
6767

6868
//----------------------------------------------------------------------------------------------------------------
6969

70-
struct ostream_sink : sink_base
70+
struct ostream_sink final : sink_base
7171
{
7272
std::ostream& out;
7373

7474
ostream_sink(std::ostream& out_) : out{out_}{}
75-
void write(const char* data, size_t nbytes) override {out.write(data, nbytes);}
75+
void write(const char* data, size_t nbytes) override final {out.write(data, nbytes);}
7676
};
7777

78-
struct istream_source : source_base
78+
struct istream_source final : source_base
7979
{
8080
std::istream& in;
8181

8282
istream_source(std::istream& in_) : in{in_}{}
8383

84-
void read(char* buf, size_t nbytes) override
84+
void read(char* buf, size_t nbytes) override final
8585
{
8686
in.read(buf, nbytes);
8787
if (in.gcount() != (long)nbytes)
8888
throw std::system_error(OUT_OF_DATA);
8989
}
9090

91-
uint8_t peak() override
91+
uint8_t peak() override final
9292
{
9393
uint8_t b = static_cast<uint8_t>(in.peek());
9494
if (!in || !in.good() || in.eof())
9595
throw std::system_error(OUT_OF_DATA);
9696
return b;
9797
}
9898

99-
size_t remaining() const override
99+
size_t remaining() const override final
100100
{
101101
const auto pos = in.tellg();
102102
in.seekg(0, std::ios::end );

0 commit comments

Comments
 (0)