-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathReader.h
More file actions
174 lines (149 loc) · 5.44 KB
/
Copy pathReader.h
File metadata and controls
174 lines (149 loc) · 5.44 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
/**
* 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 <optional>
#include <utility>
#include <flatdata/MemoryDescriptor.h>
#include <flatdata/DebugDataAccessStatistics.h>
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*;
enum
{
bit_width = num_bits
};
enum : typename UnderlyingType< T >::type
{
max = bit_width == 0 ? 0
: ( bit_width - std::is_signed< T >::value
== sizeof( typename BitsToUnsigned< num_bits >::type ) * 8
? typename BitsToUnsigned< num_bits >::type( -1 )
: ( typename BitsToUnsigned< num_bits >::type( 1 )
<< ( bit_width - std::is_signed< T >::value ) )
- 1 ),
min = bit_width == 0 || !std::is_signed< T >::value
? static_cast< typename UnderlyingType< T >::type >( 0 )
: static_cast< typename UnderlyingType< T >::type >( -max - 1 )
};
StreamType data;
operator T( ) const
{
using UnsignedType =
typename BitsToUnsigned< int_choice< num_bits, num_bits + offset % 8,
num_bits + offset % 8 <= 64 >::value >::type;
/* 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;
#ifdef DEBUG_DATA_ACCESS_STATISTICS
DebugDataAccessStatistics::read(
{ data + BYTE_OFFSET,
sizeof( UnsignedType )
+ ( sizeof( UnsignedType ) * 8 - BIT_OFFSET < num_bits ? 1 : 0 ) },
BYTE_OFFSET, struct_size_bytes );
#endif
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, struct_size_bytes > start{ data };
Reader< T, offset, num_bits, struct_size_bytes > 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, struct_size_bytes >{ data } );
}
}
template < typename U >
operator std::optional< U >( ) const
{
boost::optional< U > bopt = static_cast< boost::optional< U > >( *this );
if ( bopt )
{
return *bopt;
}
else
{
return std::nullopt;
}
}
};
} // namespace flatdata