From eb7bec4fddb5c944f309410ce457a0032791dc0d Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sat, 23 Feb 2013 17:54:04 -0800 Subject: [PATCH] Refactoring ClangCompleter to use ResultAnd<> --- cpp/ycm/ClangCompleter/ClangCompleter.cpp | 29 ++++------------------- cpp/ycm/Result.h | 13 ++++++++++ 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.cpp b/cpp/ycm/ClangCompleter/ClangCompleter.cpp index 7590f592..f73be368 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.cpp +++ b/cpp/ycm/ClangCompleter/ClangCompleter.cpp @@ -53,26 +53,6 @@ namespace YouCompleteMe { extern const unsigned int MAX_ASYNC_THREADS; extern const unsigned int MIN_ASYNC_THREADS; -namespace { - -// TODO: replace this with ResultAnd from Result.h -struct CompletionDataAndResult { - CompletionDataAndResult( const CompletionData *completion_data, - const Result &result ) - : completion_data_( completion_data ), result_( result ) {} - - bool operator< ( const CompletionDataAndResult &other ) const { - return result_ < other.result_; - } - - const CompletionData *completion_data_; - Result result_; -}; - - -} // unnamed namespace - - ClangCompleter::ClangCompleter() : candidate_repository_( CandidateRepository::Instance() ), threading_enabled_( false ), @@ -411,7 +391,7 @@ std::vector< CompletionData > ClangCompleter::SortCandidatesForQuery( std::vector< const Candidate * > repository_candidates = candidate_repository_.GetCandidatesForStrings( completion_datas ); - std::vector< CompletionDataAndResult > data_and_results; + std::vector< ResultAnd< CompletionData* > > data_and_results; for ( uint i = 0; i < repository_candidates.size(); ++i ) { const Candidate *candidate = repository_candidates[ i ]; @@ -422,7 +402,8 @@ std::vector< CompletionData > ClangCompleter::SortCandidatesForQuery( Result result = candidate->QueryMatchResult( query ); if ( result.IsSubsequence() ) { - CompletionDataAndResult data_and_result( &completion_datas[ i ], result ); + ResultAnd< CompletionData* > data_and_result( &completion_datas[ i ], + result ); data_and_results.push_back( boost::move( data_and_result ) ); } } @@ -432,9 +413,9 @@ std::vector< CompletionData > ClangCompleter::SortCandidatesForQuery( std::vector< CompletionData > sorted_completion_datas; sorted_completion_datas.reserve( data_and_results.size() ); - foreach ( const CompletionDataAndResult & data_and_result, + foreach ( const ResultAnd< CompletionData* > & data_and_result, data_and_results ) { - sorted_completion_datas.push_back( *data_and_result.completion_data_ ); + sorted_completion_datas.push_back( *data_and_result.extra_object_ ); } return sorted_completion_datas; diff --git a/cpp/ycm/Result.h b/cpp/ycm/Result.h index 97b93ec8..262182f2 100644 --- a/cpp/ycm/Result.h +++ b/cpp/ycm/Result.h @@ -99,6 +99,19 @@ struct ResultAnd { Result result_; }; +template< class T > +struct ResultAnd { + ResultAnd( const T* extra_object, const Result &result ) + : extra_object_( extra_object ), result_( result ) {} + + bool operator< ( const ResultAnd &other ) const { + return result_ < other.result_; + } + + const T* extra_object_; + Result result_; +}; + } // namespace YouCompleteMe #endif /* end of include guard: RESULT_H_CZYD2SGN */