Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Dec 17, 2023
1 parent f017853 commit 0474cb9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 37 deletions.
43 changes: 7 additions & 36 deletions include/tao/pq/parameter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace tao::pq
}

template< typename A >
void bind_rvalue_reference( A&& a )
void bind_impl( A&& a )
{
using D = std::decay_t< A&& >;

Expand All @@ -88,47 +88,19 @@ namespace tao::pq
throw std::length_error( "too many parameters!" );
}

auto bptr = std::make_unique< holder< D > >( std::forward< A >( a ) );
parameter::fill( bptr->m_traits, std::make_index_sequence< columns >() );

m_params[ m_pos++ ] = bptr.release();
m_size += columns;
}

template< typename A >
void bind_const_lvalue_reference( const A& a )
{
using D = std::decay_t< const A& >;
constexpr auto hold = std::is_rvalue_reference_v< A&& > && !parameter_traits< D >::self_contained;
using container_t = std::conditional_t< hold, holder< D >, binder< D > >;

constexpr auto columns = parameter_traits< D >::columns;
if( ( static_cast< std::size_t >( m_size ) + columns ) > Max ) {
throw std::length_error( "too many parameters!" );
}

auto bptr = std::make_unique< binder< D > >( a );
auto bptr = std::make_unique< container_t >( std::forward< A >( a ) );
parameter::fill( bptr->m_traits, std::make_index_sequence< columns >() );

m_params[ m_pos++ ] = bptr.release();
m_size += columns;
}

template< typename A >
void bind_impl( A&& a )
{
using D = std::decay_t< A&& >;
if constexpr( std::is_rvalue_reference_v< A&& > || parameter_traits< D >::self_contained ) {
parameter::bind_rvalue_reference( std::forward< A >( a ) );
}
else {
parameter::bind_const_lvalue_reference( std::forward< A >( a ) );
}
}

public:
parameter() noexcept = default;

template< typename... As >
explicit parameter( As&&... as )
explicit parameter( As&&... as ) noexcept( noexcept( parameter::bind( std::forward< As >( as )... ) ) )
{
parameter::bind( std::forward< As >( as )... );
}
Expand All @@ -146,15 +118,14 @@ namespace tao::pq
void operator=( const parameter& ) = delete;
void operator=( parameter&& ) = delete;

// NOTE: arguments must remain VALID and UNMODIFIED until this object is destroyed or reset.
template< typename... As >
void bind( As&&... as )
void bind( As&&... as ) noexcept( sizeof...( As ) == 0 )
{
( parameter::bind_impl( std::forward< As >( as ) ), ... );
}

template< typename... As >
void reset( As&&... as ) noexcept( sizeof...( As ) == 0 )
void reset( As&&... as ) noexcept( noexcept( parameter::bind( std::forward< As >( as )... ) ) )
{
for( std::size_t i = 0; i != m_pos; ++i ) {
delete m_params[ i ];
Expand Down
3 changes: 2 additions & 1 deletion src/test/pq/parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ void run()

{
tao::pq::parameter< 2 > p;
std::string s = "Jerry";
std::string s = "Alice";
p.bind( s );
p.bind( std::move( s ) );
p.reset( std::string( "Jerry" ), 42 + 7 );
p.bind();
tr->execute( "insert_user", p );
Expand Down

0 comments on commit 0474cb9

Please sign in to comment.