From 30d756d120efa9692aef5124699284a877fc6143 Mon Sep 17 00:00:00 2001 From: Flamefire Date: Wed, 21 Nov 2018 22:09:56 +0100 Subject: [PATCH] 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 --- .travis.yml | 2 +- .../turtle/detail/function_impl_template.hpp | 38 +++++++++----- include/turtle/detail/mutex.hpp | 50 ++++++++++++++----- 3 files changed, 65 insertions(+), 25 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6ff977a..e775d7b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -74,7 +74,7 @@ script: - cd $PROJECT_DIR/build - export BOOST_ROOT=$BOOST # `--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: - COVERALS_DIR=$PROJECT_DIR/coverals diff --git a/include/turtle/detail/function_impl_template.hpp b/include/turtle/detail/function_impl_template.hpp index 5a29b3b..3a68971 100644 --- a/include/turtle/detail/function_impl_template.hpp +++ b/include/turtle/detail/function_impl_template.hpp @@ -95,42 +95,56 @@ namespace detail R( BOOST_PP_ENUM_PARAMS( MOCK_NUM_ARGS, T ) ) > 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_base< R, expectation_type >( e ) + : base_type( e ) , lock_( m ) {} - - wrapper once() + wrapper( BOOST_RV_REF( wrapper ) x ) + : 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 >() ); return *this; } - wrapper never() + wrapper& never() { this->e_->invoke( boost::make_shared< detail::never >() ); return *this; } - wrapper exactly( std::size_t count ) + wrapper& exactly( std::size_t count ) { this->e_->invoke( boost::make_shared< detail::exactly >( count ) ); return *this; } - wrapper at_least( std::size_t min ) + wrapper& at_least( std::size_t min ) { this->e_->invoke( boost::make_shared< detail::at_least >( min ) ); return *this; } - wrapper at_most( std::size_t max ) + wrapper& at_most( std::size_t max ) { this->e_->invoke( boost::make_shared< detail::at_most >( max ) ); 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( boost::make_shared< detail::between >( min, max ) ); @@ -141,7 +155,7 @@ namespace detail template< BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, typename Constraint_) > - wrapper with( + wrapper& with( BOOST_PP_ENUM_BINARY_PARAMS(MOCK_NUM_ARGS, Constraint_, c) ) { this->e_->with( @@ -151,7 +165,7 @@ namespace detail #if MOCK_NUM_ARGS > 1 template< typename Constraint > - wrapper with( const Constraint& c ) + wrapper& with( const Constraint& c ) { this->e_->with( c ); return *this; @@ -163,7 +177,7 @@ namespace detail this->e_->add( s##n ); #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, _) \ return *this; \ diff --git a/include/turtle/detail/mutex.hpp b/include/turtle/detail/mutex.hpp index 4884da9..18ae0a5 100644 --- a/include/turtle/detail/mutex.hpp +++ b/include/turtle/detail/mutex.hpp @@ -11,6 +11,8 @@ #include "../config.hpp" #include "singleton.hpp" +#include +#include #include #ifdef MOCK_THREAD_SAFE @@ -36,25 +38,35 @@ namespace detail struct lock { + private: + BOOST_MOVABLE_BUT_NOT_COPYABLE(lock) + + public: lock( const boost::shared_ptr< mutex >& m ) : m_( m ) { m_->lock(); } - lock( const lock& rhs ) - { - m_.swap( rhs.m_ ); - } ~lock() { if( m_ ) 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: - lock& operator=( const lock& rhs ); - - mutable boost::shared_ptr< mutex > m_; + boost::shared_ptr< mutex > m_; }; } } // mock @@ -65,24 +77,38 @@ namespace mock { namespace detail { - struct mutex + struct mutex : boost::noncopyable { - mutex() - {} void lock() {} 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() + {} }; - struct lock + class lock : boost::noncopyable { + private: + BOOST_MOVABLE_BUT_NOT_COPYABLE(lock) + + public: lock( const boost::shared_ptr< mutex >& ) {} + ~lock() + {} + lock( BOOST_RV_REF( lock ) x ) + {} + lock& operator=( BOOST_RV_REF( lock ) x ) + { + return *this; + } }; } } // mock