inspect.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*************************************************************************
  2. *
  3. * Copyright 2016 Realm Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. **************************************************************************/
  18. #ifndef REALM_UTIL_INSPECT_HPP
  19. #define REALM_UTIL_INSPECT_HPP
  20. #include <string>
  21. namespace realm {
  22. namespace util {
  23. // LCOV_EXCL_START
  24. //
  25. // Because these are templated functions, every combination of output stream
  26. // type and value(s) type(s) generates a new function. This makes LCOV/GCOVR
  27. // report over 70 functions in this file, with only 6.6% function coverage,
  28. // even though line coverage is at 100%.
  29. template <class OS, class T>
  30. void inspect_value(OS& os, const T& value)
  31. {
  32. os << value;
  33. }
  34. template <class OS>
  35. void inspect_value(OS& os, const std::string& value)
  36. {
  37. // FIXME: Escape the string.
  38. os << "\"" << value << "\"";
  39. }
  40. template <class OS>
  41. void inspect_value(OS& os, const char* value)
  42. {
  43. // FIXME: Escape the string.
  44. os << "\"" << value << "\"";
  45. }
  46. template <class OS>
  47. void inspect_all(OS&)
  48. {
  49. // No-op
  50. }
  51. /// Convert all arguments to strings, and quote string arguments.
  52. template <class OS, class First, class... Args>
  53. void inspect_all(OS& os, First&& first, Args&&... args)
  54. {
  55. inspect_value(os, std::forward<First>(first));
  56. if (sizeof...(Args) != 0) {
  57. os << ", ";
  58. }
  59. inspect_all(os, std::forward<Args>(args)...);
  60. }
  61. // LCOV_EXCL_STOP
  62. } // namespace util
  63. } // namespace realm
  64. #endif // REALM_UTIL_INSPECT_HPP