File tree Expand file tree Collapse file tree 1 file changed +16
-4
lines changed Expand file tree Collapse file tree 1 file changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -55,11 +55,10 @@ class BitWriter {
55
55
int offset = 0 ;
56
56
unsigned char * out;
57
57
58
- public:
59
- BitWriter (unsigned char * output) : out(output) {}
60
-
61
58
template <int BITS, typename I>
62
- inline void Write (I val) {
59
+ inline void WriteInner (I val) {
60
+ // We right shift by up to 8 bits below. Verify that's well defined for the type I.
61
+ static_assert (std::numeric_limits<I>::digits > 8 , " BitWriter::WriteInner needs I > 8 bits" );
63
62
int bits = BITS;
64
63
if (bits + offset >= 8 ) {
65
64
state |= ((val & ((I (1 ) << (8 - offset)) - 1 )) << offset);
@@ -78,6 +77,19 @@ class BitWriter {
78
77
offset += bits;
79
78
}
80
79
80
+
81
+ public:
82
+ BitWriter (unsigned char * output) : out(output) {}
83
+
84
+ template <int BITS, typename I>
85
+ inline void Write (I val) {
86
+ // If I is smaller than an unsigned int, invoke WriteInner with argument converted to unsigned.
87
+ using compute_type = typename std::conditional<
88
+ (std::numeric_limits<I>::digits < std::numeric_limits<unsigned >::digits),
89
+ unsigned , I>::type;
90
+ return WriteInner<BITS, compute_type>(val);
91
+ }
92
+
81
93
inline void Flush () {
82
94
if (offset) {
83
95
*(out++) = state;
You can’t perform that action at this time.
0 commit comments