debugStringConvertibleUtils.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <limits>
  9. #include <memory>
  10. #include <string>
  11. #include <vector>
  12. #include <react/debug/DebugStringConvertible.h>
  13. #include <react/debug/DebugStringConvertibleItem.h>
  14. namespace facebook {
  15. namespace react {
  16. #if RN_DEBUG_STRING_CONVERTIBLE
  17. template <typename T>
  18. inline SharedDebugStringConvertible
  19. debugStringConvertibleItem(std::string name, T value, T defaultValue = {}) {
  20. if (value == defaultValue) {
  21. return nullptr;
  22. }
  23. return std::make_shared<DebugStringConvertibleItem>(name, toString(value));
  24. }
  25. template <typename T>
  26. inline SharedDebugStringConvertible debugStringConvertibleItem(
  27. std::string name,
  28. folly::Optional<T> value,
  29. T defaultValue = {}) {
  30. if (!value.hasValue()) {
  31. return nullptr;
  32. }
  33. return debugStringConvertibleItem(
  34. name, value.value_or(defaultValue), defaultValue);
  35. }
  36. inline SharedDebugStringConvertibleList operator+(
  37. const SharedDebugStringConvertibleList &lhs,
  38. const SharedDebugStringConvertibleList &rhs) {
  39. auto result = SharedDebugStringConvertibleList{};
  40. std::move(lhs.begin(), lhs.end(), std::back_inserter(result));
  41. std::move(rhs.begin(), rhs.end(), std::back_inserter(result));
  42. return result;
  43. }
  44. inline SharedDebugStringConvertible debugStringConvertibleItem(
  45. std::string name,
  46. DebugStringConvertible value,
  47. std::string defaultValue) {
  48. return debugStringConvertibleItem(
  49. name, value.getDebugDescription(), defaultValue);
  50. }
  51. #endif
  52. } // namespace react
  53. } // namespace facebook