CandidateRepository.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (C) 2011, 2012 Google Inc.
  2. //
  3. // This file is part of YouCompleteMe.
  4. //
  5. // YouCompleteMe is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // YouCompleteMe is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. #include "CandidateRepository.h"
  18. #include "Candidate.h"
  19. #include "standard.h"
  20. #include "Utils.h"
  21. #include <boost/thread/locks.hpp>
  22. #include <boost/algorithm/string.hpp>
  23. #include <locale>
  24. #ifdef USE_CLANG_COMPLETER
  25. # include "ClangCompleter/CompletionData.h"
  26. #endif // USE_CLANG_COMPLETER
  27. namespace YouCompleteMe {
  28. using boost::all;
  29. using boost::is_print;
  30. boost::mutex CandidateRepository::singleton_mutex_;
  31. CandidateRepository *CandidateRepository::instance_ = NULL;
  32. CandidateRepository &CandidateRepository::Instance() {
  33. boost::lock_guard< boost::mutex > locker( singleton_mutex_ );
  34. if ( !instance_ ) {
  35. static CandidateRepository repo;
  36. instance_ = &repo;
  37. }
  38. return *instance_;
  39. }
  40. int CandidateRepository::NumStoredCandidates() {
  41. boost::lock_guard< boost::mutex > locker( holder_mutex_ );
  42. return candidate_holder_.size();
  43. }
  44. std::vector< const Candidate * > CandidateRepository::GetCandidatesForStrings(
  45. const std::vector< std::string > &strings ) {
  46. std::vector< const Candidate * > candidates;
  47. candidates.reserve( strings.size() );
  48. {
  49. boost::lock_guard< boost::mutex > locker( holder_mutex_ );
  50. foreach ( const std::string & candidate_text, strings ) {
  51. const std::string &validated_candidate_text =
  52. all( candidate_text, is_print( std::locale::classic() ) ) ?
  53. candidate_text :
  54. empty_;
  55. const Candidate *&candidate = GetValueElseInsert(
  56. candidate_holder_,
  57. validated_candidate_text,
  58. NULL );
  59. if ( !candidate )
  60. candidate = new Candidate( validated_candidate_text );
  61. candidates.push_back( candidate );
  62. }
  63. }
  64. return candidates;
  65. }
  66. #ifdef USE_CLANG_COMPLETER
  67. std::vector< const Candidate * > CandidateRepository::GetCandidatesForStrings(
  68. const std::vector< CompletionData > &datas ) {
  69. std::vector< const Candidate * > candidates;
  70. candidates.reserve( datas.size() );
  71. {
  72. boost::lock_guard< boost::mutex > locker( holder_mutex_ );
  73. foreach ( const CompletionData & data, datas ) {
  74. const Candidate *&candidate = GetValueElseInsert( candidate_holder_,
  75. data.original_string_,
  76. NULL );
  77. if ( !candidate )
  78. candidate = new Candidate( data.original_string_ );
  79. candidates.push_back( candidate );
  80. }
  81. }
  82. return candidates;
  83. }
  84. #endif // USE_CLANG_COMPLETER
  85. CandidateRepository::~CandidateRepository() {
  86. foreach ( const CandidateHolder::value_type & pair,
  87. candidate_holder_ ) {
  88. delete pair.second;
  89. }
  90. }
  91. } // namespace YouCompleteMe