forked from heremaps/flatdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reader.h
130 lines (111 loc) · 3.85 KB
/
Reader.h
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
/**
* 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 "functional/Tagged.h"
#include <boost/endian/conversion.hpp>
#include <boost/optional.hpp>
#include <boost/optional/optional_io.hpp>
#include <cstring>
#include <utility>
namespace flatdata
{
/**
* This class allows reading 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 read from [data + offset, data + offset + num_bits) bit positions
*
* @note The class expects data streams with 8 byte padding in the end when reading
*/
template < typename T, int offset = 0, int num_bits = sizeof( T ) * 8, int struct_size_bytes = 0 >
struct Reader
{
using StreamType = const 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;
operator T( ) const
{
/* Does the following:
* - takes the smallest data type available that can read the necessary amount of bytes
* (including offset in the beginning and empty space in the end)
* - uses that data type to read 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
* - Previous data has to be erased with a mask
* - Surrounding data of non-aligned integers is preserved
*/
static const int BYTE_OFFSET = offset / 8;
static const int BIT_OFFSET = offset % 8;
if ( num_bits == 1 )
{
return static_cast< T >( ( data[ BYTE_OFFSET ] & ( 1 << BIT_OFFSET ) ) != 0 );
}
UnsignedType result;
std::memcpy( &result, data + BYTE_OFFSET, sizeof( result ) );
boost::endian::little_to_native_inplace( result );
result >>= BIT_OFFSET;
if ( sizeof( UnsignedType ) * 8 - BIT_OFFSET < num_bits )
{
UnsignedType temp = data[ BYTE_OFFSET + sizeof( UnsignedType ) ];
result |= temp << ( ( sizeof( UnsignedType ) * 8 - BIT_OFFSET )
% ( sizeof( UnsignedType ) * 8 ) );
}
result = masked< num_bits >( result );
return extend_sign< T, num_bits >( result );
}
template < typename U >
U
as( ) const
{
return static_cast< U >( this->operator T( ) );
}
};
template < typename T, int offset, int num_bits, int struct_size_bytes >
struct Reader< std::pair< T, T >, offset, num_bits, struct_size_bytes >
{
using StreamType = const unsigned char*;
StreamType data;
template < typename U >
operator std::pair< U, U >( ) const
{
Reader< T, offset, num_bits > start{data};
Reader< T, offset, num_bits > end{data + struct_size_bytes};
return std::pair< T, T >( start, end );
}
};
template < typename T, T INVALID_VALUE, int offset, int num_bits, int struct_size_bytes >
struct Reader< Tagged< T, INVALID_VALUE >, offset, num_bits, struct_size_bytes >
{
using StreamType = const unsigned char*;
StreamType data;
explicit operator bool( ) const
{
return Reader< T, offset, num_bits >{data} != INVALID_VALUE;
}
T operator*( ) const
{
return Reader< T, offset, num_bits >{data};
}
template < typename U >
operator boost::optional< U >( ) const
{
if ( Reader< T, offset, num_bits >{data} == INVALID_VALUE )
{
return {};
}
else
{
return boost::optional< U >(Reader< T, offset, num_bits >{data});
}
}
};
} // namespace flatdata