Skip to content

Commit 0a6cb89

Browse files
authored
MINOR: adding Reader/Writer std::optional variants, same as boost::op… (#258)
* MINOR: adding Reader/Writer std::optional variants, same as boost::optional ones Signed-off-by: Esteban Pavese <esteban.pavese@here.com>
1 parent 987c2e2 commit 0a6cb89

4 files changed

Lines changed: 60 additions & 4 deletions

File tree

flatdata-cpp/benchmark/main.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,14 @@ main( int argc, char** argv )
215215
}
216216
else if ( args.at( "lookup" ).asBool( ) )
217217
{
218-
size_t num_runs = args.at( "--num-runs" ).asLong( );
219218
call_for_graph< DoLookup >( argv[ 2 ], num_nodes, num_runs );
220219
}
221220
else if ( args.at( "bfs" ).asBool( ) )
222221
{
223-
size_t num_runs = args.at( "--num-runs" ).asLong( );
224222
call_for_graph< DoBFS >( argv[ 2 ], num_nodes, num_runs );
225223
}
226224
else if ( args.at( "dijkstra" ).asBool( ) )
227225
{
228-
size_t num_runs = args.at( "--num-runs" ).asLong( );
229226
call_for_graph< DoDijkstra >( argv[ 2 ], num_nodes, num_runs );
230227
}
231228
}

flatdata-cpp/include/flatdata/internal/Reader.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <boost/optional/optional_io.hpp>
1414

1515
#include <cstring>
16+
#include <optional>
1617
#include <utility>
1718

1819
#include <flatdata/MemoryDescriptor.h>
@@ -154,6 +155,20 @@ struct Reader< Tagged< T, INVALID_VALUE >, offset, num_bits, struct_size_bytes >
154155
return boost::optional< U >( Reader< T, offset, num_bits, struct_size_bytes >{ data } );
155156
}
156157
}
158+
159+
template < typename U >
160+
operator std::optional< U >( ) const
161+
{
162+
boost::optional< U > bopt = static_cast< boost::optional< U > >( *this );
163+
if ( bopt )
164+
{
165+
return *bopt;
166+
}
167+
else
168+
{
169+
return std::nullopt;
170+
}
171+
}
157172
};
158173

159174
} // namespace flatdata

flatdata-cpp/include/flatdata/internal/Writer.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <cassert>
1414
#include <type_traits>
15+
#include <optional>
1516

1617
namespace flatdata
1718
{
@@ -125,6 +126,19 @@ struct Writer< Tagged< T, INVALID_VALUE >, offset, num_bits, struct_size_bytes >
125126
return Reader< Tagged< T, INVALID_VALUE >, offset, num_bits >{ data };
126127
}
127128

129+
operator std::optional< T >( ) const
130+
{
131+
boost::optional< T > bopt = static_cast< boost::optional< T > >( *this );
132+
if ( bopt )
133+
{
134+
return *bopt;
135+
}
136+
else
137+
{
138+
return std::nullopt;
139+
}
140+
}
141+
128142
Writer&
129143
operator=( T t )
130144
{
@@ -143,6 +157,18 @@ struct Writer< Tagged< T, INVALID_VALUE >, offset, num_bits, struct_size_bytes >
143157
{
144158
Writer< T, offset, num_bits >{ data } = INVALID_VALUE;
145159
}
160+
return *this;
161+
}
162+
163+
Writer&
164+
operator=( std::optional< T > t )
165+
{
166+
boost::optional< T > bopt;
167+
if ( t )
168+
{
169+
bopt = *t;
170+
}
171+
return *this = bopt;
146172
}
147173
};
148174

flatdata-cpp/test/StructTest.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,54 +43,72 @@ TEST_CASE( "Invalid values are handled", "[Struct]" )
4343
REQUIRE( static_cast< bool >( writer.invalid_zero ) == true );
4444
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_zero )
4545
== boost::optional< int8_t >( 10 ) );
46+
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_zero )
47+
== std::optional< int8_t >( 10 ) );
4648
REQUIRE( *reader.invalid_zero == 10 );
4749
REQUIRE( static_cast< bool >( reader.invalid_zero ) == true );
4850
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_zero )
4951
== boost::optional< int8_t >( 10 ) );
52+
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_zero )
53+
== std::optional< int8_t >( 10 ) );
5054

5155
writer.invalid_zero = 0;
5256
REQUIRE( *writer.invalid_zero == 0 );
5357
REQUIRE( static_cast< bool >( writer.invalid_zero ) == false );
5458
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_zero ) == boost::none );
59+
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_zero ) == std::nullopt );
5560
REQUIRE( *reader.invalid_zero == 0 );
5661
REQUIRE( static_cast< bool >( reader.invalid_zero ) == false );
5762
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_zero ) == boost::none );
63+
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_zero ) == std::nullopt );
5864

5965
writer.invalid_min_int = 10;
6066
REQUIRE( *writer.invalid_min_int == 10 );
6167
REQUIRE( static_cast< bool >( writer.invalid_min_int ) == true );
6268
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_min_int )
6369
== boost::optional< int8_t >( 10 ) );
70+
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_min_int )
71+
== std::optional< int8_t >( 10 ) );
6472
REQUIRE( *reader.invalid_min_int == 10 );
6573
REQUIRE( static_cast< bool >( reader.invalid_min_int ) == true );
6674
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_min_int )
6775
== boost::optional< int8_t >( 10 ) );
76+
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_min_int )
77+
== std::optional< int8_t >( 10 ) );
6878

6979
writer.invalid_min_int = -128;
7080
REQUIRE( *writer.invalid_min_int == -128 );
7181
REQUIRE( static_cast< bool >( writer.invalid_min_int ) == false );
7282
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_min_int ) == boost::none );
83+
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_min_int ) == std::nullopt );
7384
REQUIRE( *reader.invalid_min_int == -128 );
7485
REQUIRE( static_cast< bool >( reader.invalid_min_int ) == false );
7586
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_min_int ) == boost::none );
87+
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_min_int ) == std::nullopt );
7688

7789
writer.invalid_max_int = 10;
7890
REQUIRE( *writer.invalid_max_int == 10 );
7991
REQUIRE( static_cast< bool >( writer.invalid_max_int ) == true );
8092
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_max_int )
8193
== boost::optional< int8_t >( 10 ) );
94+
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_max_int )
95+
== std::optional< int8_t >( 10 ) );
8296
REQUIRE( *reader.invalid_max_int == 10 );
8397
REQUIRE( static_cast< bool >( reader.invalid_max_int ) == true );
8498
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_max_int )
8599
== boost::optional< int8_t >( 10 ) );
100+
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_max_int )
101+
== std::optional< int8_t >( 10 ) );
86102

87103
writer.invalid_max_int = 127;
88104
REQUIRE( *writer.invalid_max_int == 127 );
89105
REQUIRE( static_cast< bool >( writer.invalid_max_int ) == false );
90106
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_max_int ) == boost::none );
107+
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_max_int ) == std::nullopt );
91108
REQUIRE( *reader.invalid_max_int == 127 );
92109
REQUIRE( static_cast< bool >( reader.invalid_max_int ) == false );
93110
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_max_int ) == boost::none );
111+
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_max_int ) == std::nullopt );
94112
}
95113

96114
TEST_CASE( "Invalid values can be converted to string", "[Struct]" )
@@ -102,4 +120,4 @@ TEST_CASE( "Invalid values can be converted to string", "[Struct]" )
102120
invalid_max_int : 0,
103121
})";
104122
REQUIRE( ( *data ).to_string( ) == EXPECTED );
105-
}
123+
}

0 commit comments

Comments
 (0)