Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions flatdata-cpp/benchmark/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,14 @@ main( int argc, char** argv )
}
else if ( args.at( "lookup" ).asBool( ) )
{
size_t num_runs = args.at( "--num-runs" ).asLong( );
call_for_graph< DoLookup >( argv[ 2 ], num_nodes, num_runs );
}
else if ( args.at( "bfs" ).asBool( ) )
{
size_t num_runs = args.at( "--num-runs" ).asLong( );
call_for_graph< DoBFS >( argv[ 2 ], num_nodes, num_runs );
}
else if ( args.at( "dijkstra" ).asBool( ) )
{
size_t num_runs = args.at( "--num-runs" ).asLong( );
call_for_graph< DoDijkstra >( argv[ 2 ], num_nodes, num_runs );
}
}
Expand Down
15 changes: 15 additions & 0 deletions flatdata-cpp/include/flatdata/internal/Reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <boost/optional/optional_io.hpp>

#include <cstring>
#include <optional>
#include <utility>

#include <flatdata/MemoryDescriptor.h>
Expand Down Expand Up @@ -154,6 +155,20 @@ struct Reader< Tagged< T, INVALID_VALUE >, offset, num_bits, struct_size_bytes >
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
26 changes: 26 additions & 0 deletions flatdata-cpp/include/flatdata/internal/Writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <cassert>
#include <type_traits>
#include <optional>

namespace flatdata
{
Expand Down Expand Up @@ -125,6 +126,19 @@ struct Writer< Tagged< T, INVALID_VALUE >, offset, num_bits, struct_size_bytes >
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 )
{
Expand All @@ -143,6 +157,18 @@ struct Writer< Tagged< T, INVALID_VALUE >, offset, num_bits, struct_size_bytes >
{
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;
}
};

Expand Down
20 changes: 19 additions & 1 deletion flatdata-cpp/test/StructTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,54 +43,72 @@ TEST_CASE( "Invalid values are handled", "[Struct]" )
REQUIRE( static_cast< bool >( writer.invalid_zero ) == true );
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_zero )
== boost::optional< int8_t >( 10 ) );
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_zero )
== std::optional< int8_t >( 10 ) );
REQUIRE( *reader.invalid_zero == 10 );
REQUIRE( static_cast< bool >( reader.invalid_zero ) == true );
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_zero )
== boost::optional< int8_t >( 10 ) );
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_zero )
== std::optional< int8_t >( 10 ) );

writer.invalid_zero = 0;
REQUIRE( *writer.invalid_zero == 0 );
REQUIRE( static_cast< bool >( writer.invalid_zero ) == false );
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_zero ) == boost::none );
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_zero ) == std::nullopt );
REQUIRE( *reader.invalid_zero == 0 );
REQUIRE( static_cast< bool >( reader.invalid_zero ) == false );
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_zero ) == boost::none );
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_zero ) == std::nullopt );

writer.invalid_min_int = 10;
REQUIRE( *writer.invalid_min_int == 10 );
REQUIRE( static_cast< bool >( writer.invalid_min_int ) == true );
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_min_int )
== boost::optional< int8_t >( 10 ) );
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_min_int )
== std::optional< int8_t >( 10 ) );
REQUIRE( *reader.invalid_min_int == 10 );
REQUIRE( static_cast< bool >( reader.invalid_min_int ) == true );
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_min_int )
== boost::optional< int8_t >( 10 ) );
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_min_int )
== std::optional< int8_t >( 10 ) );

writer.invalid_min_int = -128;
REQUIRE( *writer.invalid_min_int == -128 );
REQUIRE( static_cast< bool >( writer.invalid_min_int ) == false );
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_min_int ) == boost::none );
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_min_int ) == std::nullopt );
REQUIRE( *reader.invalid_min_int == -128 );
REQUIRE( static_cast< bool >( reader.invalid_min_int ) == false );
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_min_int ) == boost::none );
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_min_int ) == std::nullopt );

writer.invalid_max_int = 10;
REQUIRE( *writer.invalid_max_int == 10 );
REQUIRE( static_cast< bool >( writer.invalid_max_int ) == true );
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_max_int )
== boost::optional< int8_t >( 10 ) );
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_max_int )
== std::optional< int8_t >( 10 ) );
REQUIRE( *reader.invalid_max_int == 10 );
REQUIRE( static_cast< bool >( reader.invalid_max_int ) == true );
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_max_int )
== boost::optional< int8_t >( 10 ) );
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_max_int )
== std::optional< int8_t >( 10 ) );

writer.invalid_max_int = 127;
REQUIRE( *writer.invalid_max_int == 127 );
REQUIRE( static_cast< bool >( writer.invalid_max_int ) == false );
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_max_int ) == boost::none );
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_max_int ) == std::nullopt );
REQUIRE( *reader.invalid_max_int == 127 );
REQUIRE( static_cast< bool >( reader.invalid_max_int ) == false );
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_max_int ) == boost::none );
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_max_int ) == std::nullopt );
}

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