Fix unused variable warning in non-threadsafe builds

Make lock/scoped_lock (dummy) RAII classes which avoids warnings in clang
Make lock and wrapper moveable as they should
Make wrapper return itself as reference not copy
This commit is contained in:
Flamefire 2018-11-21 22:09:56 +01:00 committed by Alexander Grund
parent 4a853f9952
commit 30d756d120
3 changed files with 65 additions and 25 deletions

View file

@ -74,7 +74,7 @@ script:
- cd $PROJECT_DIR/build - cd $PROJECT_DIR/build
- export BOOST_ROOT=$BOOST - export BOOST_ROOT=$BOOST
# `--coverage` flags required to generate coverage info for Coveralls # `--coverage` flags required to generate coverage info for Coveralls
- ./build.sh --toolset=$CC "cxxflags=-std=$CXX_STANDARD -Wno-unused-local-typedefs -Wno-unused-variable -Wno-unused-function -Wno-deprecated-declarations --coverage" "linkflags=--coverage" - ./build.sh --toolset=$CC "cxxflags=-std=$CXX_STANDARD -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations --coverage" "linkflags=--coverage"
after_success: after_success:
- COVERALS_DIR=$PROJECT_DIR/coverals - COVERALS_DIR=$PROJECT_DIR/coverals

View file

@ -95,42 +95,56 @@ namespace detail
R( BOOST_PP_ENUM_PARAMS( MOCK_NUM_ARGS, T ) ) R( BOOST_PP_ENUM_PARAMS( MOCK_NUM_ARGS, T ) )
> expectation_type; > expectation_type;
struct wrapper : wrapper_base< R, expectation_type > class wrapper : public wrapper_base< R, expectation_type >
{ {
private:
typedef wrapper_base< R, expectation_type > base_type;
BOOST_MOVABLE_BUT_NOT_COPYABLE(wrapper)
public:
wrapper( const boost::shared_ptr< mutex >& m, expectation_type& e ) wrapper( const boost::shared_ptr< mutex >& m, expectation_type& e )
: wrapper_base< R, expectation_type >( e ) : base_type( e )
, lock_( m ) , lock_( m )
{} {}
wrapper( BOOST_RV_REF( wrapper ) x )
wrapper once() : base_type( x )
, lock_( boost::move( x.lock_) )
{}
wrapper& operator=( BOOST_RV_REF( wrapper ) x )
{
static_cast< base_type& >( *this ) = x;
lock_ = boost::move( x.lock_ );
return *this;
}
wrapper& once()
{ {
this->e_->invoke( boost::make_shared< detail::once >() ); this->e_->invoke( boost::make_shared< detail::once >() );
return *this; return *this;
} }
wrapper never() wrapper& never()
{ {
this->e_->invoke( boost::make_shared< detail::never >() ); this->e_->invoke( boost::make_shared< detail::never >() );
return *this; return *this;
} }
wrapper exactly( std::size_t count ) wrapper& exactly( std::size_t count )
{ {
this->e_->invoke( this->e_->invoke(
boost::make_shared< detail::exactly >( count ) ); boost::make_shared< detail::exactly >( count ) );
return *this; return *this;
} }
wrapper at_least( std::size_t min ) wrapper& at_least( std::size_t min )
{ {
this->e_->invoke( this->e_->invoke(
boost::make_shared< detail::at_least >( min ) ); boost::make_shared< detail::at_least >( min ) );
return *this; return *this;
} }
wrapper at_most( std::size_t max ) wrapper& at_most( std::size_t max )
{ {
this->e_->invoke( this->e_->invoke(
boost::make_shared< detail::at_most >( max ) ); boost::make_shared< detail::at_most >( max ) );
return *this; return *this;
} }
wrapper between( std::size_t min, std::size_t max ) wrapper& between( std::size_t min, std::size_t max )
{ {
this->e_->invoke( this->e_->invoke(
boost::make_shared< detail::between >( min, max ) ); boost::make_shared< detail::between >( min, max ) );
@ -141,7 +155,7 @@ namespace detail
template< template<
BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, typename Constraint_) BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, typename Constraint_)
> >
wrapper with( wrapper& with(
BOOST_PP_ENUM_BINARY_PARAMS(MOCK_NUM_ARGS, Constraint_, c) ) BOOST_PP_ENUM_BINARY_PARAMS(MOCK_NUM_ARGS, Constraint_, c) )
{ {
this->e_->with( this->e_->with(
@ -151,7 +165,7 @@ namespace detail
#if MOCK_NUM_ARGS > 1 #if MOCK_NUM_ARGS > 1
template< typename Constraint > template< typename Constraint >
wrapper with( const Constraint& c ) wrapper& with( const Constraint& c )
{ {
this->e_->with( c ); this->e_->with( c );
return *this; return *this;
@ -163,7 +177,7 @@ namespace detail
this->e_->add( s##n ); this->e_->add( s##n );
#define MOCK_FUNCTION_IN(z, n, d) \ #define MOCK_FUNCTION_IN(z, n, d) \
wrapper in( BOOST_PP_ENUM_PARAMS(n, sequence& s) ) \ wrapper& in( BOOST_PP_ENUM_PARAMS(n, sequence& s) ) \
{ \ { \
BOOST_PP_REPEAT(n, MOCK_FUNCTION_IN_ADD, _) \ BOOST_PP_REPEAT(n, MOCK_FUNCTION_IN_ADD, _) \
return *this; \ return *this; \

View file

@ -11,6 +11,8 @@
#include "../config.hpp" #include "../config.hpp"
#include "singleton.hpp" #include "singleton.hpp"
#include <boost/move/move.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#ifdef MOCK_THREAD_SAFE #ifdef MOCK_THREAD_SAFE
@ -36,25 +38,35 @@ namespace detail
struct lock struct lock
{ {
private:
BOOST_MOVABLE_BUT_NOT_COPYABLE(lock)
public:
lock( const boost::shared_ptr< mutex >& m ) lock( const boost::shared_ptr< mutex >& m )
: m_( m ) : m_( m )
{ {
m_->lock(); m_->lock();
} }
lock( const lock& rhs )
{
m_.swap( rhs.m_ );
}
~lock() ~lock()
{ {
if( m_ ) if( m_ )
m_->unlock(); m_->unlock();
} }
lock( BOOST_RV_REF( lock ) x )
: m_( x.m_ )
{
// Explicit reset to avoid unlock in destructor
x.m_.reset();
}
lock& operator=( BOOST_RV_REF( lock ) x )
{
m_ = x.m_;
x.m_.reset();
return *this;
}
private: private:
lock& operator=( const lock& rhs ); boost::shared_ptr< mutex > m_;
mutable boost::shared_ptr< mutex > m_;
}; };
} }
} // mock } // mock
@ -65,24 +77,38 @@ namespace mock
{ {
namespace detail namespace detail
{ {
struct mutex struct mutex : boost::noncopyable
{ {
mutex()
{}
void lock() void lock()
{} {}
void unlock() void unlock()
{} {}
}; };
struct scoped_lock // Dummy lock classes.
// Constructor + Destructor make it RAII classes for compilers and avoid unused variable warnings
struct scoped_lock : boost::noncopyable
{ {
scoped_lock( mutex& ) scoped_lock( mutex& )
{} {}
~scoped_lock()
{}
}; };
struct lock class lock : boost::noncopyable
{ {
private:
BOOST_MOVABLE_BUT_NOT_COPYABLE(lock)
public:
lock( const boost::shared_ptr< mutex >& ) lock( const boost::shared_ptr< mutex >& )
{} {}
~lock()
{}
lock( BOOST_RV_REF( lock ) x )
{}
lock& operator=( BOOST_RV_REF( lock ) x )
{
return *this;
}
}; };
} }
} // mock } // mock