Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Oct 24, 2018
1 parent 261b4d8 commit ba6ab34
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 43 deletions.
4 changes: 2 additions & 2 deletions include/tao/postgres/field.hpp
Expand Up @@ -37,14 +37,14 @@ namespace tao
const char* get() const;

template< typename T >
typename std::enable_if_t< result_traits_size< T >::value != 1, T > as() const
typename std::enable_if_t< result_traits_size< T > != 1, T > as() const
{
static_assert( !std::is_same_v< T, T >, "tao::postgres::result_traits<T>::size does not yield exactly one column for T, which is required for field access" );
__builtin_unreachable();
}

template< typename T >
typename std::enable_if_t< result_traits_size< T >::value == 1, T > as() const; // implemented in row.hpp
typename std::enable_if_t< result_traits_size< T > == 1, T > as() const; // implemented in row.hpp

template< typename T >
std::optional< T > optional() const
Expand Down
14 changes: 4 additions & 10 deletions include/tao/postgres/result.hpp
Expand Up @@ -34,16 +34,10 @@ namespace tao
namespace result_impl
{
template< typename T, typename = void >
struct has_reserve
: std::false_type
{
};
inline constexpr bool has_reserve = false;

template< typename T >
struct has_reserve< T, std::enable_if_t< std::is_void_v< decltype( T::reserve( std::declval< typename T::size_type >() ) ) > > >
: std::true_type
{
};
inline constexpr bool has_reserve< T, std::void_t< decltype( T::reserve( std::declval< typename T::size_type >() ) ) > > = true;

} // namespace result_impl

Expand Down Expand Up @@ -155,7 +149,7 @@ namespace tao
}

template< typename T >
std::enable_if_t< result_impl::has_reserve< T >::value, T > as_container() const
std::enable_if_t< result_impl::has_reserve< T >, T > as_container() const
{
T nrv;
nrv.reserve( size() );
Expand All @@ -167,7 +161,7 @@ namespace tao
}

template< typename T >
std::enable_if_t< !result_impl::has_reserve< T >::value, T > as_container() const
std::enable_if_t< !result_impl::has_reserve< T >, T > as_container() const
{
T nrv;
check_has_result_set();
Expand Down
20 changes: 4 additions & 16 deletions include/tao/postgres/result_traits.hpp
Expand Up @@ -23,28 +23,16 @@ namespace tao
};

template< typename T, typename = void >
struct result_traits_size
: std::integral_constant< std::size_t, 1 >
{
};
inline constexpr std::size_t result_traits_size = 1;

template< typename T >
struct result_traits_size< T, std::enable_if_t< std::is_same_v< decltype( result_traits< T >::size ), const std::size_t > > >
: std::integral_constant< std::size_t, result_traits< T >::size >
{
};
inline constexpr std::size_t result_traits_size< T, std::enable_if_t< std::is_same_v< decltype( result_traits< T >::size ), const std::size_t > > > = result_traits< T >::size;

template< typename T, typename = void >
struct result_traits_has_null
: std::false_type
{
};
inline constexpr bool result_traits_has_null = false;

template< typename T >
struct result_traits_has_null< T, decltype( result_traits< T >::null(), void() ) >
: std::true_type
{
};
inline constexpr bool result_traits_has_null< T, decltype( result_traits< T >::null(), void() ) > = true;

template<>
struct result_traits< const char* >
Expand Down
2 changes: 1 addition & 1 deletion include/tao/postgres/result_traits_optional.hpp
Expand Up @@ -16,7 +16,7 @@ namespace tao
template< typename T >
struct result_traits< std::optional< T > >
{
static constexpr std::size_t size = result_traits_size< T >::value;
static constexpr std::size_t size = result_traits_size< T >;

static std::optional< T > null()
{
Expand Down
4 changes: 2 additions & 2 deletions include/tao/postgres/result_traits_pair.hpp
Expand Up @@ -19,11 +19,11 @@ namespace tao
using DT = std::decay_t< T >;
using DU = std::decay_t< U >;

static constexpr std::size_t size = result_traits_size< DT >::value + result_traits_size< DU >::value;
static constexpr std::size_t size = result_traits_size< DT > + result_traits_size< DU >;

static std::pair< T, U > from( const row& row )
{
return std::pair< T, U >( row.get< DT >( 0 ), row.get< DU >( result_traits_size< DT >::value ) );
return std::pair< T, U >( row.get< DT >( 0 ), row.get< DU >( result_traits_size< DT > ) );
}
};

Expand Down
8 changes: 4 additions & 4 deletions include/tao/postgres/result_traits_tuple.hpp
Expand Up @@ -38,9 +38,9 @@ namespace tao
struct result_traits< std::tuple< Ts... >, std::enable_if_t< sizeof...( Ts ) == 1 > >
{
using T = std::tuple_element_t< 0, std::tuple< Ts... > >;
static constexpr std::size_t size = result_traits_size< T >::value;
static constexpr std::size_t size = result_traits_size< T >;

static std::enable_if_t< result_traits_has_null< T >::value, std::tuple< T > > null()
static std::enable_if_t< result_traits_has_null< T >, std::tuple< T > > null()
{
return std::tuple< T >( result_traits< T >::null() );
}
Expand All @@ -54,7 +54,7 @@ namespace tao
template< typename... Ts >
struct result_traits< std::tuple< Ts... >, std::enable_if_t< ( sizeof...( Ts ) > 1 ) > >
{
static constexpr std::size_t size = ( result_traits_size< Ts >::value + ... );
static constexpr std::size_t size = ( result_traits_size< Ts > + ... );

template< std::size_t... Ns >
static std::tuple< Ts... > from( const row& row, const std::index_sequence< Ns... >& )
Expand All @@ -64,7 +64,7 @@ namespace tao

static std::tuple< Ts... > from( const row& row )
{
return from( row, impl::exclusive_scan_t< result_traits_size< Ts >::value... >() );
return from( row, impl::exclusive_scan_t< result_traits_size< Ts >... >() );
}
};

Expand Down
16 changes: 8 additions & 8 deletions include/tao/postgres/row.hpp
Expand Up @@ -56,14 +56,14 @@ namespace tao
const char* get( const std::size_t column ) const;

template< typename T >
std::enable_if_t< result_traits_size< T >::value == 0, T > get( const std::size_t ) const
std::enable_if_t< result_traits_size< T > == 0, T > get( const std::size_t ) const
{
static_assert( !std::is_same< T, T >::value, "tao::postgres::result_traits<T>::size yields zero" );
__builtin_unreachable();
}

template< typename T >
std::enable_if_t< result_traits_size< T >::value == 1 && result_traits_has_null< T >::value, T > get( const std::size_t column ) const
std::enable_if_t< result_traits_size< T > == 1 && result_traits_has_null< T >, T > get( const std::size_t column ) const
{
if( is_null( column ) ) {
return result_traits< T >::null();
Expand All @@ -72,16 +72,16 @@ namespace tao
}

template< typename T >
std::enable_if_t< result_traits_size< T >::value == 1 && !result_traits_has_null< T >::value, T > get( const std::size_t column ) const
std::enable_if_t< result_traits_size< T > == 1 && !result_traits_has_null< T >, T > get( const std::size_t column ) const
{
ensure_column( column );
return result_traits< T >::from( get( column ) );
}

template< typename T >
std::enable_if_t< ( result_traits_size< T >::value > 1 ), T > get( const std::size_t column ) const
std::enable_if_t< ( result_traits_size< T >> 1 ), T > get( const std::size_t column ) const
{
return result_traits< T >::from( slice( column, result_traits_size< T >::value ) );
return result_traits< T >::from( slice( column, result_traits_size< T > ) );
}

template< typename T >
Expand All @@ -93,8 +93,8 @@ namespace tao
template< typename T >
T as() const
{
if( result_traits_size< T >::value != columns_ ) {
throw std::runtime_error( utility::printf( "datatype (%s) requires %zu columns, but row/slice has %zu columns", utility::demangle< T >().c_str(), result_traits_size< T >::value, columns_ ) );
if( result_traits_size< T > != columns_ ) {
throw std::runtime_error( utility::printf( "datatype (%s) requires %zu columns, but row/slice has %zu columns", utility::demangle< T >().c_str(), result_traits_size< T >, columns_ ) );
}
return get< T >( 0 );
}
Expand Down Expand Up @@ -133,7 +133,7 @@ namespace tao
};

template< typename T >
std::enable_if_t< result_traits_size< T >::value == 1, T > field::as() const
std::enable_if_t< result_traits_size< T > == 1, T > field::as() const
{
return row_.get< T >( column_ );
}
Expand Down

0 comments on commit ba6ab34

Please sign in to comment.