IdentifierDatabase.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (C) 2013 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. #ifndef IDENTIFIERDATABASE_H_ZESX3CVR
  18. #define IDENTIFIERDATABASE_H_ZESX3CVR
  19. #include <boost/utility.hpp>
  20. #include <boost/unordered_map.hpp>
  21. #include <boost/thread/mutex.hpp>
  22. #include <boost/shared_ptr.hpp>
  23. #include <vector>
  24. #include <string>
  25. #include <map>
  26. #include <set>
  27. namespace YouCompleteMe {
  28. class Candidate;
  29. class Result;
  30. class CandidateRepository;
  31. // filepath -> identifiers
  32. typedef std::map< std::string, std::vector< std::string > >
  33. FilepathToIdentifiers;
  34. // filetype -> (filepath -> identifiers)
  35. typedef std::map< std::string, FilepathToIdentifiers >
  36. FiletypeIdentifierMap;
  37. // This class stores the database of identifiers the identifier completer has
  38. // seen. It stores them in a data structure that makes it easy to tell which
  39. // identifier came from which file and what files have which filetypes.
  40. //
  41. // The main point of this class is to isolate the parts of the code that need
  42. // access to this internal data structure so that it's easier to confirm that
  43. // mutexes are used correctly to protect concurrent access.
  44. //
  45. // This class is thread-safe.
  46. class IdentifierDatabase : boost::noncopyable {
  47. public:
  48. IdentifierDatabase();
  49. void AddIdentifiers( const FiletypeIdentifierMap &filetype_identifier_map );
  50. void AddIdentifiers(
  51. const std::vector< std::string > &new_candidates,
  52. const std::string &filetype,
  53. const std::string &filepath );
  54. void ClearCandidatesStoredForFile( const std::string &filetype,
  55. const std::string &filepath );
  56. void ResultsForQueryAndType( const std::string &query,
  57. const std::string &filetype,
  58. std::vector< Result > &results ) const;
  59. private:
  60. std::set< const Candidate * > &GetCandidateSet(
  61. const std::string &filetype,
  62. const std::string &filepath );
  63. void AddIdentifiersNoLock(
  64. const std::vector< std::string > &new_candidates,
  65. const std::string &filetype,
  66. const std::string &filepath );
  67. // filepath -> *( *candidate )
  68. typedef boost::unordered_map < std::string,
  69. boost::shared_ptr< std::set< const Candidate * > > >
  70. FilepathToCandidates;
  71. // filetype -> *( filepath -> *( *candidate ) )
  72. typedef boost::unordered_map < std::string,
  73. boost::shared_ptr< FilepathToCandidates > > FiletypeCandidateMap;
  74. CandidateRepository &candidate_repository_;
  75. FiletypeCandidateMap filetype_candidate_map_;
  76. mutable boost::mutex filetype_candidate_map_mutex_;
  77. };
  78. } // namespace YouCompleteMe
  79. #endif /* end of include guard: IDENTIFIERDATABASE_H_ZESX3CVR */