values_util.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_JSON_VALUES_UTIL_H_
  5. #define BASE_JSON_VALUES_UTIL_H_
  6. #include "base/base_export.h"
  7. #include "base/values.h"
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. namespace base {
  10. class FilePath;
  11. class Time;
  12. class TimeDelta;
  13. class UnguessableToken;
  14. // Simple helper functions for converting between Value and other types.
  15. // The Value representation is stable, suitable for persistent storage
  16. // e.g. as JSON on disk.
  17. //
  18. // It is valid to pass nullptr to the ValueToEtc functions. They will just
  19. // return absl::nullopt.
  20. // Converts between an int64_t and a string-flavored Value (a human
  21. // readable string of that number).
  22. BASE_EXPORT Value Int64ToValue(int64_t integer);
  23. BASE_EXPORT absl::optional<int64_t> ValueToInt64(const Value* value);
  24. BASE_EXPORT absl::optional<int64_t> ValueToInt64(const Value& value);
  25. // Converts between a TimeDelta (an int64_t number of microseconds) and a
  26. // string-flavored Value (a human readable string of that number).
  27. BASE_EXPORT Value TimeDeltaToValue(TimeDelta time_delta);
  28. BASE_EXPORT absl::optional<TimeDelta> ValueToTimeDelta(const Value* value);
  29. BASE_EXPORT absl::optional<TimeDelta> ValueToTimeDelta(const Value& value);
  30. // Converts between a Time (an int64_t number of microseconds since the
  31. // Windows epoch) and a string-flavored Value (a human readable string of
  32. // that number).
  33. BASE_EXPORT Value TimeToValue(Time time);
  34. BASE_EXPORT absl::optional<Time> ValueToTime(const Value* value);
  35. BASE_EXPORT absl::optional<Time> ValueToTime(const Value& value);
  36. // Converts between a FilePath (a std::string or std::u16string) and a
  37. // string-flavored Value (the UTF-8 representation).
  38. BASE_EXPORT Value FilePathToValue(FilePath file_path);
  39. BASE_EXPORT absl::optional<FilePath> ValueToFilePath(const Value* value);
  40. BASE_EXPORT absl::optional<FilePath> ValueToFilePath(const Value& value);
  41. // Converts between a UnguessableToken (128 bits) and a string-flavored
  42. // Value (32 hexadecimal digits).
  43. BASE_EXPORT Value UnguessableTokenToValue(UnguessableToken token);
  44. BASE_EXPORT absl::optional<UnguessableToken> ValueToUnguessableToken(
  45. const Value* value);
  46. BASE_EXPORT absl::optional<UnguessableToken> ValueToUnguessableToken(
  47. const Value& value);
  48. } // namespace base
  49. #endif // BASE_JSON_VALUES_UTIL_H_