Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Unified Diff: third_party/WebKit/Source/platform/inspector_protocol/TypeBuilder_h.template

Issue 1730383003: DevTools: consistently use Maybe for optional values in the protocol generator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments addressed Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..10ce09906af82d01c134971d7bc9e0ed659b84b8 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,94 @@ 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;
Nico 2016/02/26 19:31:48 also, i found it pretty surprising that Maybe<T> h
};
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; }
Nico 2016/02/26 19:14:06 isJust() isn't a great name for this. People who k
+ 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) { }
+ using MaybeBase::operator=;
+};
-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) { }
+ using MaybeBase::operator=;
+};
-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) { }
+ using MaybeBase::operator=;
+};
-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) { }
+ using MaybeBase::operator=;
+};
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) { }
+ 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);
+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(T* param) { return param->asValue(); }
+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 +354,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 +367,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 +399,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;
}

Powered by Google App Engine
This is Rietveld 408576698