values_util.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_UTIL_VALUES_VALUES_UTIL_H_
  5. #define BASE_UTIL_VALUES_VALUES_UTIL_H_
  6. #include "base/optional.h"
  7. #include "base/values.h"
  8. namespace base {
  9. class FilePath;
  10. class Time;
  11. class TimeDelta;
  12. class UnguessableToken;
  13. } // namespace base
  14. namespace util {
  15. // Simple helper functions for converting between base::Value and other types.
  16. // The base::Value representation is stable, suitable for persistent storage
  17. // e.g. as JSON on disk.
  18. //
  19. // It is valid to pass nullptr to the ValueToEtc functions. They will just
  20. // return base::nullopt.
  21. // Converts between an int64_t and a string-flavored base::Value (a human
  22. // readable string of that number).
  23. base::Value Int64ToValue(int64_t integer);
  24. base::Optional<int64_t> ValueToInt64(const base::Value* value);
  25. base::Optional<int64_t> ValueToInt64(const base::Value& value);
  26. // Converts between a base::TimeDelta (an int64_t number of microseconds) and a
  27. // string-flavored base::Value (a human readable string of that number).
  28. base::Value TimeDeltaToValue(base::TimeDelta time_delta);
  29. base::Optional<base::TimeDelta> ValueToTimeDelta(const base::Value* value);
  30. base::Optional<base::TimeDelta> ValueToTimeDelta(const base::Value& value);
  31. // Converts between a base::Time (an int64_t number of microseconds since the
  32. // Windows epoch) and a string-flavored base::Value (a human readable string of
  33. // that number).
  34. base::Value TimeToValue(base::Time time);
  35. base::Optional<base::Time> ValueToTime(const base::Value* value);
  36. base::Optional<base::Time> ValueToTime(const base::Value& value);
  37. // Converts between a base::FilePath (a std::string or base::string16) and a
  38. // string-flavored base::Value (the UTF-8 representation).
  39. base::Value FilePathToValue(base::FilePath file_path);
  40. base::Optional<base::FilePath> ValueToFilePath(const base::Value* value);
  41. base::Optional<base::FilePath> ValueToFilePath(const base::Value& value);
  42. // Converts between a base::UnguessableToken (128 bits) and a string-flavored
  43. // base::Value (32 hexadecimal digits).
  44. base::Value UnguessableTokenToValue(base::UnguessableToken token);
  45. base::Optional<base::UnguessableToken> ValueToUnguessableToken(
  46. const base::Value* value);
  47. base::Optional<base::UnguessableToken> ValueToUnguessableToken(
  48. const base::Value& value);
  49. } // namespace util
  50. #endif // BASE_UTIL_VALUES_VALUES_UTIL_H_