Utils.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #ifndef UTILS_H_KEPMRPBH
  18. #define UTILS_H_KEPMRPBH
  19. #include <string>
  20. #include <vector>
  21. #include <boost/filesystem.hpp>
  22. namespace fs = boost::filesystem;
  23. namespace YouCompleteMe {
  24. bool AlmostEqual( double a, double b );
  25. // Reads the entire contents of the specified file. If the file does not exist,
  26. // an exception is thrown.
  27. std::string ReadUtf8File( const fs::path &filepath );
  28. // Writes the entire contents of the specified file. If the file does not exist,
  29. // an exception is thrown.
  30. void WriteUtf8File( const fs::path &filepath, const std::string &contents );
  31. template <class Container, class Key>
  32. typename Container::mapped_type &
  33. GetValueElseInsert( Container &container,
  34. const Key &key,
  35. const typename Container::mapped_type &value ) {
  36. return container.insert( typename Container::value_type( key, value ) )
  37. .first->second;
  38. }
  39. template <class Container, class Key>
  40. bool ContainsKey( Container &container, const Key &key ) {
  41. return container.find( key ) != container.end();
  42. }
  43. template <class Container, class Key>
  44. typename Container::mapped_type
  45. FindWithDefault( Container &container,
  46. const Key &key,
  47. const typename Container::mapped_type &value ) {
  48. typename Container::const_iterator it = container.find( key );
  49. return it != container.end() ? it->second : value;
  50. }
  51. template <class Container, class Key>
  52. bool Erase( Container &container, const Key &key ) {
  53. typename Container::iterator it = container.find( key );
  54. if ( it != container.end() ) {
  55. container.erase( it );
  56. return true;
  57. }
  58. return false;
  59. }
  60. } // namespace YouCompleteMe
  61. #endif /* end of include guard: UTILS_H_KEPMRPBH */