Skip to content

Commit d1b4387

Browse files
committed
Revamp reader/writer using concepts and get rid of all the typedef cruft
1 parent 0ccacf6 commit d1b4387

File tree

5 files changed

+35
-93
lines changed

5 files changed

+35
-93
lines changed

bitreader/include/bitreader/bitreader-utils.hpp

Lines changed: 14 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -25,98 +25,39 @@ namespace brcpp
2525
template<typename Enum>
2626
using fitting_integral = typename integral_by_size<sizeof(Enum)>::type;
2727

28-
struct binary_codec {};
29-
30-
template<bool cond, typename R>
31-
using cond_if_t = std::enable_if_t<cond, R>;
32-
33-
template<typename T>
34-
using check_unsigned_integral = std::bool_constant<std::is_unsigned_v<T> && std::is_integral_v<T>>;
35-
36-
template<typename T>
37-
using check_signed_integral = std::bool_constant<std::is_signed_v<T> && std::is_integral_v<T>>;
38-
39-
template<typename T>
40-
using check_integral = std::bool_constant<std::is_integral_v<T>>;
41-
42-
template<typename T>
43-
using check_floating_point = std::bool_constant<std::is_floating_point_v<T>>;
44-
45-
template<typename T>
46-
using check_bit_readable = std::bool_constant<std::is_floating_point_v<T> || std::is_integral_v<T>>;
47-
48-
template<typename T>
49-
using check_binary_codec = std::bool_constant<std::is_base_of_v<binary_codec, T>>;
50-
51-
template<typename T>
52-
using check_enum = std::bool_constant<std::is_enum_v<T>>;
53-
54-
55-
template<typename T>
56-
using if_unsigned_integral = std::enable_if_t<check_unsigned_integral<T>::value, T>;
57-
58-
template<typename T>
59-
using if_signed_integral = std::enable_if_t<check_signed_integral<T>::value, T>;
60-
61-
template<typename T>
62-
using if_integral = std::enable_if_t<check_integral<T>::value, T>;
28+
struct binary_codec_base {};
6329

6430
template<typename T>
65-
using if_floating_point = std::enable_if_t<check_floating_point<T>::value, T>;
31+
concept binary_codec =
32+
std::derived_from<T, binary_codec_base> &&
33+
requires { typename T::value_type; };
6634

6735
template<typename T>
68-
using if_bit_readable = std::enable_if_t<check_bit_readable<T>::value, T>;
36+
concept enumeration = std::is_enum_v<T>;
6937

70-
template<typename T>
71-
using if_binary_codec = std::enable_if_t<check_binary_codec<T>::value, typename T::value_type>;
72-
73-
template<typename T>
74-
using if_enum = std::enable_if_t<check_enum<T>::value, T>;
75-
76-
77-
template<typename T>
78-
using if_unsigned_integral_void = std::enable_if_t<check_unsigned_integral<T>::value, void>;
79-
80-
template<typename T>
81-
using if_signed_integral_void = std::enable_if_t<check_signed_integral<T>::value, void>;
82-
83-
template<typename T>
84-
using if_integral_void = std::enable_if_t<check_integral<T>::value, void>;
85-
86-
template<typename T>
87-
using if_floating_point_void = std::enable_if_t<check_floating_point<T>::value, void>;
88-
89-
template<typename T>
90-
using if_bit_readable_void = std::enable_if_t<check_bit_readable<T>::value, void>;
38+
using std::integral;
39+
using std::unsigned_integral;
40+
using std::signed_integral;
41+
using std::floating_point;
9142

9243
template<typename T>
93-
using if_binary_codec_void = std::enable_if_t<check_binary_codec<T>::value, void>;
94-
95-
template<typename T>
96-
using if_enum_void = std::enable_if_t<check_enum<T>::value, void>;
44+
struct bit_read_helper {};
9745

9846
//--------------------------------------------------------------------------
99-
template<typename T>
100-
struct bit_read_helper
47+
template<integral T>
48+
struct bit_read_helper<T>
10149
{
10250
static constexpr const size_t max_bits = 8 * sizeof(T);
10351
static constexpr const size_t min_bits = 0;
10452
static constexpr const bool is_signed = std::is_signed_v<T>;
10553
};
10654

10755
//--------------------------------------------------------------------------
108-
template<typename T>
109-
struct floating_point_bit_read_helper
56+
template<floating_point T>
57+
struct bit_read_helper<T>
11058
{
11159
static constexpr const size_t max_bits = 8 * sizeof(T);
11260
static constexpr const size_t min_bits = 8 * sizeof(T);
11361
static constexpr const bool is_signed = std::is_signed_v<T>;
11462
};
115-
116-
//--------------------------------------------------------------------------
117-
template<> struct bit_read_helper<float>: floating_point_bit_read_helper<float> {};
118-
template<> struct bit_read_helper<double>: floating_point_bit_read_helper<double> {};
119-
template<> struct bit_read_helper<long double>: floating_point_bit_read_helper<long double> {};
120-
121-
12263
}

bitreader/include/bitreader/bitreader.hpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <cstdint>
33
#include <cstddef>
44
#include <cassert>
5+
#include <concepts>
56
#include <stdexcept>
67
#include <memory>
78

@@ -79,23 +80,23 @@ namespace brcpp {
7980
* @param bits Number of bits to read
8081
* @return The data read from the stream
8182
*/
82-
template<typename T>
83-
if_integral<T> read(size_t bits)
83+
template<integral T>
84+
T read(size_t bits)
8485
{
8586
_validate_read_dynamic<T>(bits);
8687
T ret = T(0);
8788
_read(_state, bits, ret);
8889
return _sign_extend(ret, bits);
8990
}
9091

91-
template<typename T>
92-
if_binary_codec<T> read()
92+
template<binary_codec T>
93+
T::value_type read()
9394
{
9495
return T::read(*this);
9596
}
9697

97-
template<typename T>
98-
if_enum<T> read(size_t bits)
98+
template<enumeration T>
99+
T read(size_t bits)
99100
{
100101
using value_type = std::underlying_type_t<T>;
101102
auto val = static_cast<T>(read<value_type>(bits));
@@ -116,8 +117,8 @@ namespace brcpp {
116117
* @param bits Number of bits to read
117118
* @return The data read from the stream
118119
*/
119-
template<typename T>
120-
if_integral<T> peek(size_t bits)
120+
template<integral T>
121+
T peek(size_t bits)
121122
{
122123
_validate_read_dynamic<T>(bits);
123124
T ret = T(0);
@@ -143,16 +144,16 @@ namespace brcpp {
143144
};
144145

145146
//----------------------------------------------------------------------
146-
template<typename T>
147-
if_signed_integral<T> _sign_extend(T raw, size_t bits)
147+
template<signed_integral T>
148+
T _sign_extend(T raw, size_t bits)
148149
{
149150
const auto m = static_cast<T>(one<T> << (bits - 1));
150151
return (raw ^ m) - m;
151152
}
152153

153154
//----------------------------------------------------------------------
154-
template<typename T>
155-
if_unsigned_integral<T> _sign_extend(T raw, size_t bits)
155+
template<unsigned_integral T>
156+
T _sign_extend(T raw, size_t bits)
156157
{
157158
return raw;
158159
}

bitreader/include/bitreader/bitwriter.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ namespace brcpp {
5252
}
5353

5454
//----------------------------------------------------------------------
55-
template<typename T>
56-
if_integral_void<T> write(T data, size_t bits)
55+
template<integral T>
56+
void write(T data, size_t bits)
5757
{
5858
size_t written = 0;
5959
size_t to_write = bits;
@@ -75,17 +75,17 @@ namespace brcpp {
7575
}
7676

7777
//----------------------------------------------------------------------
78-
template<typename T>
79-
if_enum_void<T> write(T data, size_t bits)
78+
template<enumeration T>
79+
void write(T data, size_t bits)
8080
{
8181
using data_t = std::underlying_type_t<T>;
8282
auto value = static_cast<data_t>(data);
8383
this->write(value, bits);
8484
}
8585

8686
//----------------------------------------------------------------------
87-
template<typename T>
88-
if_binary_codec_void<T> write(typename T::value_type data)
87+
template<binary_codec T>
88+
void write(T::value_type data)
8989
{
9090
T::write(*this, data);
9191
}

bitreader/include/bitreader/codings/exp-golomb-k0.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace brcpp::ext
66
{
77
template<typename T>
8-
struct exp_golomb_k0: public brcpp::binary_codec
8+
struct exp_golomb_k0: public brcpp::binary_codec_base
99
{
1010
using value_type = T;
1111

bitreader/include/bitreader/codings/string-nullterm.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace brcpp::ext
77
{
8-
struct string_nullterm: public brcpp::binary_codec
8+
struct string_nullterm: public brcpp::binary_codec_base
99
{
1010
using value_type = std::string;
1111

0 commit comments

Comments
 (0)