Chromium Code Reviews| Index: third_party/WebKit/Source/platform/inspector_protocol/TypeBuilder_h.template |
| diff --git a/third_party/WebKit/Source/platform/inspector_protocol/TypeBuilder_h.template b/third_party/WebKit/Source/platform/inspector_protocol/TypeBuilder_h.template |
| index 4589aab80680c82d520919ac66e3ae8f943f46d3..cadfdb645984e0b53eaced60b7590937ed2a6f0f 100644 |
| --- a/third_party/WebKit/Source/platform/inspector_protocol/TypeBuilder_h.template |
| +++ b/third_party/WebKit/Source/platform/inspector_protocol/TypeBuilder_h.template |
| @@ -18,95 +18,93 @@ namespace blink { |
| namespace protocol { |
| template<typename T> |
| -class OptionalValue { |
| +class Maybe { |
| public: |
| - OptionalValue() : m_hasValue(false) { } |
| - OptionalValue(const T& value) : m_hasValue(true), m_value(value) { } |
| - |
| - void operator=(T value) |
| - { |
| - m_value = value; |
| - m_hasValue = true; |
| - } |
| - |
| - T get() const |
| - { |
| - ASSERT(m_hasValue); |
| - return m_value; |
| - } |
| - |
| - T get(const T& defaultValue) const |
| - { |
| - return m_hasValue ? m_value : defaultValue; |
| - } |
| - |
| - bool hasValue() const |
| - { |
| - return m_hasValue; |
| - } |
| - |
| + Maybe() { } |
| + Maybe(PassOwnPtr<T> value) : m_value(value) { } |
| + void operator=(PassOwnPtr<T> value) { m_value = value; } |
| + T* fromJust() const { ASSERT(m_value); return m_value.get(); } |
| + T* fromMaybe(PassOwnPtr<T> defaultValue) const { return m_value ? m_value.get() : defaultValue; } |
| + bool isJust() const { return !!m_value; } |
| + PassOwnPtr<T> takeJust() { ASSERT(m_value); return m_value.release(); } |
| private: |
| - bool m_hasValue; |
| - T m_value; |
| + OwnPtr<T> m_value; |
| }; |
| template<typename T> |
| -OptionalValue<T> optional(const T* value) |
| -{ |
| - return value ? OptionalValue<T>(*value) : OptionalValue<T>(); |
| -} |
| +class MaybeBase { |
| +public: |
| + MaybeBase() : m_isJust(false) { } |
| + MaybeBase(T value) : m_isJust(true), m_value(value) { } |
| + void operator=(T value) { m_value = value; m_isJust = true; } |
| + T fromJust() const { ASSERT(m_isJust); return m_value; } |
| + T fromMaybe(const T& defaultValue) const { return m_isJust ? m_value : defaultValue; } |
| + bool isJust() const { return m_isJust; } |
| + T takeJust() { ASSERT(m_isJust); return m_value; } |
| + |
| +protected: |
| + bool m_isJust; |
| + T m_value; |
| +}; |
| -PLATFORM_EXPORT OptionalValue<String> optional(const String& value); |
| +template<> class Maybe<bool> : public MaybeBase<bool> { |
| +public: |
| + Maybe() { } |
| + Maybe(bool value) : MaybeBase(value) { } |
|
dgozman
2016/02/24 23:17:44
Can we omit constructors so that ones from base cl
pfeldman
2016/02/24 23:38:39
Nope
|
| + void operator=(bool value) { m_value = value; } |
|
dgozman
2016/02/24 23:17:44
You don't need this. MaybeBase's version is better
pfeldman
2016/02/24 23:38:39
Converted to using.
|
| +}; |
| -template<typename T> |
| -OptionalValue<T> optional(const T& value) |
| +template<> class Maybe<int> : public MaybeBase<int> |
| { |
| - return OptionalValue<T>(value); |
| -} |
| - |
| -template<typename T> class Array; |
| - |
| -template<typename T> |
| -bool hasValue(const protocol::OptionalValue<T>& t) { return t.hasValue(); } |
| - |
| -template<typename T> |
| -bool hasValue(T* value) { return !!value; } |
| - |
| -template<typename T> |
| -bool hasValue(const OwnPtr<T>& value) { return !!value; } |
| - |
| -template<typename T> |
| -bool hasValue(const PassOwnPtr<T>& value) { return !!value; } |
| - |
| -template<typename T> |
| -bool hasValue(const RefPtr<T>& value) { return !!value; } |
| +public: |
| + Maybe() { } |
| + Maybe(int value) : MaybeBase(value) { } |
| + void operator=(int value) { m_value = value; } |
| +}; |
| -template<typename T> |
| -PassRefPtr<JSONValue> toValue(const T& param) |
| +template<> class Maybe<double> : public MaybeBase<double> |
| { |
| - return JSONBasicValue::create(param); |
| -} |
| - |
| -template<> |
| -PLATFORM_EXPORT PassRefPtr<JSONValue> toValue(const String& param); |
| +public: |
| + Maybe() { } |
| + Maybe(double value) : MaybeBase(value) { } |
| + void operator=(double value) { m_value = value; } |
| +}; |
| -template<typename T> |
| -PassRefPtr<JSONValue> toValue(PassRefPtr<T> param) |
| +template<> class Maybe<String> : public MaybeBase<String> |
| { |
| - return param; |
| -} |
| +public: |
| + Maybe() { } |
| + Maybe(const String& value) : MaybeBase(value) { } |
| + Maybe(const AtomicString& value) : MaybeBase(value) { } |
| + void operator=(const String value) { m_value = value; } |
| +}; |
| template<typename T> |
| -PassRefPtr<JSONValue> toValue(const PassOwnPtr<protocol::Array<T>> param) |
| -{ |
| - return param->asValue(); |
| -} |
| +class Maybe<RefPtr<T>> { |
| +public: |
| + Maybe() { } |
| + Maybe(RefPtr<T> value) : m_value(value) { } |
| + Maybe(PassRefPtr<T> value) : m_value(value) { } |
| + Maybe(T* value) : m_value(value) { } |
|
dgozman
2016/02/24 23:17:44
m_value(adoptRef(value))
pfeldman
2016/02/24 23:38:39
It is ref...
|
| + void operator=(PassRefPtr<T> value) { m_value = value; } |
| + PassRefPtr<T> fromJust() const { ASSERT(m_value); return m_value; } |
| + PassRefPtr<T> fromMaybe(const PassRefPtr<T> defaultValue) const { return m_value || defaultValue; } |
| + bool isJust() const { return !!m_value; } |
| + PassRefPtr<T> takeJust() { return m_value; } |
| + |
| +protected: |
| + RefPtr<T> m_value; |
| +}; |
| -template<typename T> |
| -PassRefPtr<JSONValue> toValue(PassOwnPtr<T> param) |
| -{ |
| - return param->asValue(); |
| -} |
| +template<typename T> class Array; |
| + |
| +PLATFORM_EXPORT PassRefPtr<JSONValue> toValue(int value); |
|
dgozman
2016/02/24 23:17:44
style: two spaces
pfeldman
2016/02/24 23:38:39
Done.
|
| +PLATFORM_EXPORT PassRefPtr<JSONValue> toValue(double value); |
| +PLATFORM_EXPORT PassRefPtr<JSONValue> toValue(bool value); |
| +PLATFORM_EXPORT PassRefPtr<JSONValue> toValue(const String& param); |
| +template<typename T> PassRefPtr<JSONValue> toValue(PassRefPtr<T> param) { return param; } |
| +template<typename T> PassRefPtr<JSONValue> toValue(PassOwnPtr<T> param) { return param->asValue(); } |
| +template<typename T> PassRefPtr<JSONValue> toValue(const Maybe<T>& param) { return toValue(param.fromJust()); } |
| template<typename T> |
| struct FromValue |
| @@ -355,6 +353,12 @@ public: |
| RefPtr<JSONValue> value = m_object->get("{{property.name}}"); |
| return value ? FromValue<{{resolve_type(property).raw_type}}>::convert(value) : defaultValue; |
| } |
| + |
| + void set{{property.name | to_title_case}}(const Maybe<{{resolve_type(property).raw_type}}>& value) |
| + { |
| + if (value.isJust()) |
| + m_object->setValue("{{property.name}}", toValue(value.fromJust())); |
| + } |
| {% else %} |
| {{resolve_type(property).return_type}} get{{property.name | to_title_case}}() |
| { |
| @@ -362,17 +366,12 @@ public: |
| RefPtr<JSONValue> value = m_object->get("{{property.name}}"); |
| return FromValue<{{resolve_type(property).raw_type}}>::convert(value); |
| } |
| - {% endif %} |
| void set{{property.name | to_title_case}}({{resolve_type(property).pass_type}} value) |
| { |
| - {% if property.optional and resolve_type(property).nullable %} |
| - if (value) |
| - m_object->setValue("{{property.name}}", toValue(value)); |
| - {% else %} |
| m_object->setValue("{{property.name}}", toValue(value)); |
| - {% endif %} |
| } |
| + {% endif %} |
| {% endfor %} |
| PassRefPtr<JSONObject> asValue() { return m_object; } |
| @@ -399,12 +398,8 @@ public: |
| {% for property in type.properties %} |
| {% if property.optional %} |
| - {{type.id}}Builder<STATE>& set{{property.name | to_title_case}}({{resolve_type(property).pass_type}} value) |
| + {{type.id}}Builder<STATE>& set{{property.name | to_title_case}}(const Maybe<{{resolve_type(property).raw_type}}>& value) |
| { |
| - {% if resolve_type(property).nullable%} |
| - if (!value) |
| - return *this; |
| - {% endif %} |
| m_result->set{{property.name | to_title_case}}(value); |
| return *this; |
| } |