-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathWriter.h
More file actions
175 lines (153 loc) · 5.27 KB
/
Copy pathWriter.h
File metadata and controls
175 lines (153 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Copyright (c) 2017 HERE Europe B.V.
* See the LICENSE file in the root of this project for license details.
*/
#pragma once
#include "BitHelpers.h"
#include "Reader.h"
#include <boost/endian/conversion.hpp>
#include <cassert>
#include <type_traits>
#include <optional>
namespace flatdata
{
/**
* This class allows reading/writing integers/booleans/enumeration to a bitstream
* Its data member is shared with other instances within the same structure by being part of the
* same union.
* Data is encoded in little-endian
* Data is written to [data + offset, data + offset + num_bits) bit positions
*
* @note The class expects data streams with 8 byte padding in the end when reading/writing
*/
template < typename T, int offset = 0, int num_bits = sizeof( T ) * 8, int struct_size_bytes = 0 >
struct Writer
{
using StreamType = unsigned char*;
using UnsignedType = typename BitsToUnsigned<
int_choice< num_bits, num_bits + offset % 8, num_bits + offset % 8 <= 64 >::value >::type;
enum
{
bit_width = num_bits
};
StreamType data;
Writer&
operator=( T t )
{
/* Does the following:
* - takes the smallest data type available that can write the necessary amount of bytes
* (including offset in the beginning and empty space in the end)
* - uses that data type to read/write the main part of the data
* - In case of 64 bit numbers one byte could be missing in the data (e.g. unaligned 64 bit
* integer), and one more byte is read/written
* - Previous data has to be erased with a mask
* - Surrounding data of non-aligned integers is preserved
*/
auto destination = data + offset / 8;
static const int BIT_OFFSET = offset % 8;
auto value_to_store = static_cast< UnsignedType >( t );
if ( std::is_signed< T >::value )
{
// negative numbers are stored as 2-complement, and we need to remove upper bits
value_to_store = masked< num_bits >( value_to_store );
}
UnsignedType value_mask = masked< num_bits >( static_cast< UnsignedType >( -1 ) );
UnsignedType value;
// read previous data
std::memcpy( &value, destination, sizeof( UnsignedType ) );
boost::endian::little_to_native_inplace( value );
// remove previous value, but keep surrounding data
value &= ~( value_mask << BIT_OFFSET );
// add new value, and keep surrounding data
value |= value_to_store << BIT_OFFSET;
// write back new data
boost::endian::native_to_little_inplace( value );
std::memcpy( destination, &value, sizeof( UnsignedType ) );
static const int BATCHED_BITS_WRITTEN = sizeof( UnsignedType ) * 8 - BIT_OFFSET;
// one byte might be missing
if ( BATCHED_BITS_WRITTEN < num_bits )
{
destination += sizeof( UnsignedType );
value_to_store >>= BATCHED_BITS_WRITTEN % ( sizeof( UnsignedType ) * 8 );
value_mask >>= BATCHED_BITS_WRITTEN % ( sizeof( UnsignedType ) * 8 );
// remove previous value, but keep surrounding data
unsigned char byte_value = static_cast< unsigned char >( *destination & ~value_mask );
// add new value, and keep surrounding data
*destination = byte_value | static_cast< unsigned char >( value_to_store );
}
return *this;
}
operator T( ) const
{
return Reader< T, offset, num_bits >{ data };
}
};
template < typename T, int offset, int num_bits, int struct_size_bytes >
struct Writer< std::pair< T, T >, offset, num_bits, struct_size_bytes >
{
using StreamType = unsigned char*;
StreamType data;
// We do not support writing to a range, just reading it after the fact
};
template < typename T, T INVALID_VALUE, int offset, int num_bits, int struct_size_bytes >
struct Writer< Tagged< T, INVALID_VALUE >, offset, num_bits, struct_size_bytes >
{
using StreamType = unsigned char*;
StreamType data;
explicit operator bool( ) const
{
return static_cast< bool >(
Reader< Tagged< T, INVALID_VALUE >, offset, num_bits >{ data } );
}
T
operator*( ) const
{
return *Reader< Tagged< T, INVALID_VALUE >, offset, num_bits >{ data };
}
operator boost::optional< T >( ) const
{
return Reader< Tagged< T, INVALID_VALUE >, offset, num_bits >{ data };
}
operator std::optional< T >( ) const
{
boost::optional< T > bopt = static_cast< boost::optional< T > >( *this );
if ( bopt )
{
return *bopt;
}
else
{
return std::nullopt;
}
}
Writer&
operator=( T t )
{
Writer< T, offset, num_bits >{ data } = t;
return *this;
}
Writer&
operator=( boost::optional< T > t )
{
if ( t )
{
Writer< T, offset, num_bits >{ data } = t;
}
else
{
Writer< T, offset, num_bits >{ data } = INVALID_VALUE;
}
return *this;
}
Writer&
operator=( std::optional< T > t )
{
boost::optional< T > bopt;
if ( t )
{
bopt = *t;
}
return *this = bopt;
}
};
} // namespace flatdata