Commit eee3f681 authored by Allen Bauer's avatar Allen Bauer Committed by Chromium LUCI CQ

Add a base type converter for simpler definition of serializable types.

Bug: 938501
Change-Id: Idcc0c7970542266d5b4b53b938238309f063e27f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2618487
Commit-Queue: Allen Bauer <kylixrd@chromium.org>
Commit-Queue: Wei Li <weili@chromium.org>
Reviewed-by: default avatarWei Li <weili@chromium.org>
Auto-Submit: Allen Bauer <kylixrd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#841609}
parent 8c9cf37c
...@@ -45,10 +45,14 @@ using ArgType = ...@@ -45,10 +45,14 @@ using ArgType =
const T&>::type; const T&>::type;
// General Type Conversion Template Functions --------------------------------- // General Type Conversion Template Functions ---------------------------------
template <typename T> template <bool serializable>
struct TypeConverter { struct BaseTypeConverter {
static constexpr bool is_serializable = std::is_enum<T>::value; static constexpr bool is_serializable = serializable;
static bool IsSerializable() { return is_serializable; } static bool IsSerializable() { return is_serializable; }
};
template <typename T>
struct TypeConverter : BaseTypeConverter<std::is_enum<T>::value> {
static base::string16 ToString(ArgType<T> source_value); static base::string16 ToString(ArgType<T> source_value);
static base::Optional<T> FromString(const base::string16& source_value); static base::Optional<T> FromString(const base::string16& source_value);
}; };
...@@ -116,9 +120,7 @@ VIEWS_EXPORT base::Optional<SkColor> RgbaPiecesToSkColor( ...@@ -116,9 +120,7 @@ VIEWS_EXPORT base::Optional<SkColor> RgbaPiecesToSkColor(
#define DECLARE_CONVERSIONS(T) \ #define DECLARE_CONVERSIONS(T) \
template <> \ template <> \
struct VIEWS_EXPORT TypeConverter<T> { \ struct VIEWS_EXPORT TypeConverter<T> : BaseTypeConverter<true> { \
static constexpr bool is_serializable = true; \
static bool IsSerializable() { return is_serializable; } \
static base::string16 ToString(ArgType<T> source_value); \ static base::string16 ToString(ArgType<T> source_value); \
static base::Optional<T> FromString(const base::string16& source_value); \ static base::Optional<T> FromString(const base::string16& source_value); \
}; };
...@@ -149,9 +151,8 @@ DECLARE_CONVERSIONS(gfx::Insets) ...@@ -149,9 +151,8 @@ DECLARE_CONVERSIONS(gfx::Insets)
VIEWS_EXPORT const base::string16& GetNullOptStr(); VIEWS_EXPORT const base::string16& GetNullOptStr();
template <typename T> template <typename T>
struct TypeConverter<base::Optional<T>> { struct TypeConverter<base::Optional<T>>
static constexpr bool is_serializable = TypeConverter<T>::is_serializable; : BaseTypeConverter<TypeConverter<T>::is_serializable> {
static bool IsSerializable() { return is_serializable; }
static base::string16 ToString(ArgType<base::Optional<T>> source_value) { static base::string16 ToString(ArgType<base::Optional<T>> source_value) {
if (!source_value) if (!source_value)
return GetNullOptStr(); return GetNullOptStr();
...@@ -168,9 +169,7 @@ struct TypeConverter<base::Optional<T>> { ...@@ -168,9 +169,7 @@ struct TypeConverter<base::Optional<T>> {
}; };
template <typename T> template <typename T>
struct TypeConverter<std::unique_ptr<T>> { struct TypeConverter<std::unique_ptr<T>> : BaseTypeConverter<false> {
static constexpr bool is_serializable = false;
static bool IsSerializable() { return is_serializable; }
static base::string16 ToString(const std::unique_ptr<T>& source_value); static base::string16 ToString(const std::unique_ptr<T>& source_value);
static base::Optional<std::unique_ptr<T>> FromString( static base::Optional<std::unique_ptr<T>> FromString(
const base::string16& source_value); const base::string16& source_value);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment