From 6702d68940735a6e507d34c1c55071449e41fd91 Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Sat, 11 Jul 2020 14:45:10 +0200 Subject: [PATCH] Use range-based for-loops --- .../turtle/detail/expectation_template.hpp | 24 +++----- .../turtle/detail/function_impl_template.hpp | 55 ++++++++++--------- include/turtle/detail/group.hpp | 20 +++---- include/turtle/detail/object_impl.hpp | 7 +-- include/turtle/detail/root.hpp | 9 +-- include/turtle/detail/sequence_impl.hpp | 3 +- 6 files changed, 51 insertions(+), 67 deletions(-) diff --git a/include/turtle/detail/expectation_template.hpp b/include/turtle/detail/expectation_template.hpp index 1791e0f..4424847 100644 --- a/include/turtle/detail/expectation_template.hpp +++ b/include/turtle/detail/expectation_template.hpp @@ -162,9 +162,8 @@ namespace detail ~expectation() { - for( sequences_cit it = sequences_.begin(); - it != sequences_.end(); ++it ) - (*it)->remove( this ); + for( auto& sequence: sequences_) + sequence->remove( this ); } void invoke( const std::shared_ptr< invocation >& i ) @@ -220,14 +219,14 @@ namespace detail bool invoke() const { - for( sequences_cit it = sequences_.begin(); - it != sequences_.end(); ++it ) - if( ! (*it)->is_valid( this ) ) + for( auto& sequence: sequences_) + { + if( ! sequence->is_valid( this ) ) return false; + } bool result = invocation_->invoke(); - for( sequences_cit it = sequences_.begin(); - it != sequences_.end(); ++it ) - (*it)->invalidate( this ); + for( auto& sequence: sequences_) + sequence->invalidate( this ); return result; } @@ -251,18 +250,13 @@ namespace detail } private: - typedef std::vector< - std::shared_ptr< sequence_impl > - > sequences_type; - typedef sequences_type::const_iterator sequences_cit; - std::shared_ptr< invocation > invocation_; std::shared_ptr< matcher_base< void( BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, T) ) > > matcher_; - sequences_type sequences_; + std::vector< std::shared_ptr > sequences_; const char* file_; int line_; }; diff --git a/include/turtle/detail/function_impl_template.hpp b/include/turtle/detail/function_impl_template.hpp index 708e122..727632d 100644 --- a/include/turtle/detail/function_impl_template.hpp +++ b/include/turtle/detail/function_impl_template.hpp @@ -52,14 +52,19 @@ namespace detail virtual ~function_impl() { if( valid_ && exceptions_ >= exceptions() ) - for( expectations_cit it = expectations_.begin(); - it != expectations_.end(); ++it ) - if( ! it->verify() ) + { + for( const auto& expectation: expectations_ ) + { + if( ! expectation.verify() ) + { error_type::fail( "untriggered expectation", boost::unit_test::lazy_ostream::instance() << lazy_context( this ) << lazy_expectations( this ), - it->file(), it->line() ); + expectation.file(), expectation.line() ); + } + } + } if( context_ ) context_->remove( *this ); } @@ -67,17 +72,18 @@ namespace detail virtual bool verify() const { lock _( mutex_ ); - for( expectations_cit it = expectations_.begin(); - it != expectations_.end(); ++it ) - if( ! it->verify() ) + for( const auto& expectation: expectations_ ) + { + if( ! expectation.verify() ) { valid_ = false; error_type::fail( "verification failed", boost::unit_test::lazy_ostream::instance() << lazy_context( this ) << lazy_expectations( this ), - it->file(), it->line() ); + expectation.file(), expectation.line() ); } + } return valid_; } @@ -228,30 +234,31 @@ namespace detail { lock _( mutex_ ); valid_ = false; - for( expectations_cit it = expectations_.begin(); - it != expectations_.end(); ++it ) - if( it->is_valid( + for( const auto& expectation: expectations_ ) + { + if( expectation.is_valid( BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_MOVE, _) ) ) { - if( ! it->invoke() ) + if( ! expectation.invoke() ) { error_type::fail( "sequence failed", - MOCK_FUNCTION_CONTEXT, it->file(), it->line() ); + MOCK_FUNCTION_CONTEXT, expectation.file(), expectation.line() ); return error_type::abort(); } - if( ! it->valid() ) + if( ! expectation.valid() ) { error_type::fail( "missing action", - MOCK_FUNCTION_CONTEXT, it->file(), it->line() ); + MOCK_FUNCTION_CONTEXT, expectation.file(), expectation.line() ); return error_type::abort(); } valid_ = true; - error_type::call( MOCK_FUNCTION_CONTEXT, it->file(), it->line() ); - if( it->functor() ) - return it->functor()( + error_type::call( MOCK_FUNCTION_CONTEXT, expectation.file(), expectation.line() ); + if( expectation.functor() ) + return expectation.functor()( BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_MOVE, _) ); - return it->trigger(); + return expectation.trigger(); } + } error_type::fail( "unexpected call", MOCK_FUNCTION_CONTEXT ); return error_type::abort(); } @@ -297,18 +304,14 @@ namespace detail {} friend std::ostream& operator<<( std::ostream& s, const lazy_expectations& e ) { - for( expectations_cit it = e.impl_->expectations_.begin(); - it != e.impl_->expectations_.end(); ++it ) - s << std::endl << *it; + for( const auto& expectation: e.impl_->expectations_ ) + s << std::endl << expectation; return s; } const function_impl* impl_; }; - typedef std::list< expectation_type > expectations_type; - typedef typename expectations_type::const_iterator expectations_cit; - - expectations_type expectations_; + std::list< expectation_type > expectations_; context* context_; mutable bool valid_; const int exceptions_; diff --git a/include/turtle/detail/group.hpp b/include/turtle/detail/group.hpp index d0668cc..16ce5a3 100644 --- a/include/turtle/detail/group.hpp +++ b/include/turtle/detail/group.hpp @@ -36,27 +36,21 @@ namespace detail bool verify() const { bool valid = true; - for( verifiables_cit it = verifiables_.begin(); - it != verifiables_.end(); ++it ) - if( ! (*it)->verify() ) + for( const auto* verifiable: verifiables_ ) + if( ! verifiable->verify() ) valid = false; return valid; } void reset() { - const verifiables_t verifiables = verifiables_; - for( verifiables_cit it = verifiables.begin(); - it != verifiables.end(); ++it ) - if( std::find( verifiables_.begin(), verifiables_.end(), *it ) - != verifiables_.end() ) - (*it)->reset(); + const auto verifiables = verifiables_; + for( auto* verifiable: verifiables ) + if( std::find( verifiables_.begin(), verifiables_.end(), verifiable ) != verifiables_.end() ) + verifiable->reset(); } private: - typedef std::vector< verifiable* > verifiables_t; - typedef verifiables_t::const_iterator verifiables_cit; - - verifiables_t verifiables_; + std::vector< verifiable* > verifiables_; }; } } // mock diff --git a/include/turtle/detail/object_impl.hpp b/include/turtle/detail/object_impl.hpp index c411ef6..6f5d4eb 100644 --- a/include/turtle/detail/object_impl.hpp +++ b/include/turtle/detail/object_impl.hpp @@ -58,7 +58,7 @@ namespace detail virtual void serialize( std::ostream& s, const verifiable& v ) const { lock _( mutex_ ); - children_cit it = children_.find( &v ); + const auto it = children_.find( &v ); if( it != children_.end() ) s << it->second; else @@ -78,12 +78,9 @@ namespace detail } private: - typedef std::map< const verifiable*, child > children_t; - typedef children_t::const_iterator children_cit; - group group_; parent parent_; - children_t children_; + std::map< const verifiable*, child > children_; const std::shared_ptr< mutex > mutex_; }; } diff --git a/include/turtle/detail/root.hpp b/include/turtle/detail/root.hpp index dae3790..09c1f95 100644 --- a/include/turtle/detail/root.hpp +++ b/include/turtle/detail/root.hpp @@ -33,7 +33,7 @@ namespace detail boost::unit_test::const_string name ) { scoped_lock _( mutex_ ); - children_t::iterator it = children_.lower_bound( &v ); + auto it = children_.lower_bound( &v ); if( it == children_.end() || children_.key_comp()( &v, it->first ) ) it = children_.insert( it, @@ -67,7 +67,7 @@ namespace detail virtual void serialize( std::ostream& s, const verifiable& v ) const { scoped_lock _( mutex_ ); - children_cit it = children_.find( &v ); + const auto it = children_.find( &v ); if( it != children_.end() ) s << it->second; else @@ -120,11 +120,8 @@ namespace detail child child_; }; - typedef std::map< const verifiable*, counter_child > children_t; - typedef children_t::const_iterator children_cit; - parents_t parents_; - children_t children_; + std::map< const verifiable*, counter_child > children_; group group_; mutable mutex mutex_; diff --git a/include/turtle/detail/sequence_impl.hpp b/include/turtle/detail/sequence_impl.hpp index 84087c4..d3f9f90 100644 --- a/include/turtle/detail/sequence_impl.hpp +++ b/include/turtle/detail/sequence_impl.hpp @@ -48,8 +48,7 @@ namespace detail void invalidate( const void* e ) { lock _( mutex_ ); - elements_type::iterator it = - std::find( elements_.begin(), elements_.end(), e ); + const auto it = std::find( elements_.begin(), elements_.end(), e ); if( it != elements_.end() ) elements_.erase( elements_.begin(), it ); }