| // This file is generated |
| |
| // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef {{class_name}}_h |
| #define {{class_name}}_h |
| |
| #include "platform/JSONValues.h" |
| #include "platform/PlatformExport.h" |
| #include "wtf/Assertions.h" |
| #include "wtf/PassOwnPtr.h" |
| #include "wtf/PassRefPtr.h" |
| #include "wtf/text/WTFString.h" |
| |
| namespace blink { |
| namespace protocol { |
| |
| template<typename T> |
| class Maybe { |
| public: |
| 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: |
| OwnPtr<T> m_value; |
| }; |
| |
| template<typename 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; |
| }; |
| |
| template<> class Maybe<bool> : public MaybeBase<bool> { |
| public: |
| Maybe() { } |
| Maybe(bool value) : MaybeBase(value) { } |
| using MaybeBase::operator=; |
| }; |
| |
| template<> class Maybe<int> : public MaybeBase<int> |
| { |
| public: |
| Maybe() { } |
| Maybe(int value) : MaybeBase(value) { } |
| using MaybeBase::operator=; |
| }; |
| |
| template<> class Maybe<double> : public MaybeBase<double> |
| { |
| public: |
| Maybe() { } |
| Maybe(double value) : MaybeBase(value) { } |
| using MaybeBase::operator=; |
| }; |
| |
| template<> class Maybe<String> : public MaybeBase<String> |
| { |
| public: |
| Maybe() { } |
| Maybe(const String& value) : MaybeBase(value) { } |
| Maybe(const AtomicString& value) : MaybeBase(value) { } |
| using MaybeBase::operator=; |
| }; |
| |
| template<typename T> |
| 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> 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 |
| { |
| static PassOwnPtr<T> convert(RefPtr<JSONValue> value) |
| { |
| if (!value) |
| return nullptr; |
| RefPtr<JSONObject> object; |
| bool success = value->asObject(&object); |
| ASSERT_UNUSED(success, success); |
| return T::runtimeCast(object.release()); |
| } |
| }; |
| |
| template<> |
| struct FromValue<bool> |
| { |
| static bool convert(RefPtr<JSONValue> value) |
| { |
| bool result; |
| bool success = value->asBoolean(&result); |
| ASSERT_UNUSED(success, success); |
| return result; |
| } |
| }; |
| |
| template<> |
| struct FromValue<int> |
| { |
| static int convert(RefPtr<JSONValue> value) |
| { |
| int result; |
| bool success = value->asNumber(&result); |
| ASSERT_UNUSED(success, success); |
| return result; |
| } |
| }; |
| |
| template<> |
| struct FromValue<double> |
| { |
| static double convert(RefPtr<JSONValue> value) |
| { |
| double result; |
| bool success = value->asNumber(&result); |
| ASSERT_UNUSED(success, success); |
| return result; |
| } |
| }; |
| |
| template<> |
| struct FromValue<String> |
| { |
| static String convert(RefPtr<JSONValue> value) |
| { |
| String result; |
| bool success = value->asString(&result); |
| ASSERT_UNUSED(success, success); |
| return result; |
| } |
| }; |
| |
| template<typename T> |
| struct FromValue<RefPtr<T>> |
| { |
| static PassRefPtr<T> convert(RefPtr<T> value) |
| { |
| return value.release(); |
| } |
| }; |
| |
| template<typename T> |
| struct FromValue<protocol::Array<T>> |
| { |
| static PassOwnPtr<protocol::Array<T>> convert(RefPtr<JSONValue> value) |
| { |
| RefPtr<JSONArray> array = value->asArray(); |
| ASSERT_UNUSED(array, array); |
| return protocol::Array<T>::runtimeCast(array); |
| } |
| }; |
| |
| template<typename T> |
| class ArrayBase { |
| public: |
| static PassOwnPtr<Array<T>> create() |
| { |
| OwnPtr<Array<T>> result = adoptPtr(new Array<T>()); |
| result->m_array = JSONArray::create(); |
| return result.release(); |
| } |
| |
| static PassOwnPtr<Array<T>> runtimeCast(PassRefPtr<JSONArray> array) |
| { |
| if (!array) |
| return nullptr; |
| OwnPtr<Array<T>> result = adoptPtr(new Array<T>()); |
| result->m_array = array; |
| return result.release(); |
| } |
| |
| void addItem(const T& value) |
| { |
| m_array->pushValue(toValue(value)); |
| } |
| |
| size_t length() { return m_array->length(); } |
| |
| T get(size_t index) { return FromValue<T>::convert(m_array->get(index)); } |
| PassRefPtr<JSONArray> asValue() { return m_array; } |
| |
| private: |
| RefPtr<JSONArray> m_array; |
| }; |
| |
| template<> class Array<String> : public ArrayBase<String> {}; |
| template<> class Array<int> : public ArrayBase<int> {}; |
| template<> class Array<double> : public ArrayBase<double> {}; |
| template<> class Array<bool> : public ArrayBase<bool> {}; |
| template<typename T> class Array<RefPtr<T>> : public ArrayBase<RefPtr<T>> {}; |
| |
| template<typename T> |
| class Array { |
| public: |
| static PassOwnPtr<Array<T>> create() |
| { |
| OwnPtr<Array<T>> result = adoptPtr(new Array<T>()); |
| result->m_array = JSONArray::create(); |
| return result.release(); |
| } |
| |
| static PassOwnPtr<Array<T>> runtimeCast(PassRefPtr<JSONArray> array) |
| { |
| if (!array) |
| return nullptr; |
| OwnPtr<Array<T>> result = adoptPtr(new Array<T>()); |
| result->m_array = array; |
| return result.release(); |
| } |
| |
| void addItem(PassOwnPtr<T> value) |
| { |
| m_array->pushValue(toValue(value)); |
| } |
| |
| size_t length() { return m_array->length(); } |
| |
| PassOwnPtr<T> get(size_t index) { return FromValue<T>::convert(m_array->get(index)); } |
| PassRefPtr<JSONArray> asValue() { return m_array; } |
| |
| private: |
| RefPtr<JSONArray> m_array; |
| }; |
| |
| {% for domain in api.domains %} |
| |
| // ------------- Forward declarations and typedefs. |
| |
| namespace {{domain.domain}} { |
| {% for type in domain.types %} |
| {% if type.type == "object" %} |
| // {{type.description}} |
| class {{type.id}}; |
| {% elif type.type != "array" %} |
| // {{type.description}} |
| using {{type.id}} = {{resolve_type(type).type}}; |
| {% endif %} |
| {% endfor %} |
| } // {{domain.domain}} |
| {% endfor %} |
| |
| // ------------- Enum values from types. |
| {% for domain in api.domains %} |
| {% for type in domain.types %} |
| {% if "enum" in type %} |
| |
| namespace {{domain.domain}} { |
| namespace {{type.id}}Enum { |
| {% for literal in type.enum %} |
| PLATFORM_EXPORT extern const char* {{ literal | dash_to_camelcase}}; |
| {% endfor %} |
| } // {{type.id}}Enum |
| } // {{domain.domain}} |
| {% endif %} |
| {% endfor %} |
| {% endfor %} |
| |
| // ------------- Enum values from params. |
| {% for domain in api.domains %} |
| {% for command in join_arrays(domain, ["commands", "events"]) %} |
| {% for param in join_arrays(command, ["parameters", "returns"]) %} |
| {% if "enum" in param %} |
| |
| namespace {{domain.domain}} { |
| namespace {{command.name | to_title_case}} { |
| namespace {{param.name | to_title_case}}Enum { |
| {% for literal in param.enum %} |
| PLATFORM_EXPORT extern const char* {{ literal | dash_to_camelcase}}; |
| {% endfor %} |
| } // {{param.name | to_title_case}}Enum |
| } // {{command.name | to_title_case }} |
| } // {{domain.domain}} |
| {% endif %} |
| {% endfor %} |
| {% endfor %} |
| {% endfor %} |
| |
| // ------------- Type and builder declarations. |
| {% for domain in api.domains %} |
| |
| namespace {{domain.domain}} { |
| {% for type in domain.types %} |
| {% if type.type == "object" %} |
| {% set type_def = type_definition(domain.domain + "." + type.id)%} |
| |
| // {{type.description}} |
| class PLATFORM_EXPORT {{type.id}} { |
| public: |
| static PassOwnPtr<{{type.id}}> runtimeCast(PassRefPtr<JSONObject> object) |
| { |
| return adoptPtr(new {{type.id}}(object)); |
| } |
| |
| {{type.id}}() : m_object(JSONObject::create()) { } |
| |
| ~{{type.id}}() { } |
| {% for property in type.properties %} |
| |
| {% if "enum" in property %} |
| struct PLATFORM_EXPORT {{property.name | to_title_case}}Enum { |
| {% for literal in property.enum %} |
| static const char* {{ literal | dash_to_camelcase}}; |
| {% endfor %} |
| }; // {{property.name | to_title_case}}Enum |
| {% endif %} |
| |
| bool has{{property.name | to_title_case}}() |
| { |
| RefPtr<JSONValue> value = m_object->get("{{property.name}}"); |
| {% if resolve_type(property).json_type %} |
| return value && value->type() == JSONValue::{{resolve_type(property).json_type}}; |
| {% else %} |
| return !!value; |
| {% endif %} |
| } |
| |
| {% if property.optional %} |
| {{resolve_type(property).return_type}} get{{property.name | to_title_case}}({{resolve_type(property).return_type}} defaultValue) |
| { |
| 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}}() |
| { |
| ASSERT(has{{property.name | to_title_case}}()); |
| RefPtr<JSONValue> value = m_object->get("{{property.name}}"); |
| return FromValue<{{resolve_type(property).raw_type}}>::convert(value); |
| } |
| |
| void set{{property.name | to_title_case}}({{resolve_type(property).pass_type}} value) |
| { |
| m_object->setValue("{{property.name}}", toValue(value)); |
| } |
| {% endif %} |
| {% endfor %} |
| |
| PassRefPtr<JSONObject> asValue() { return m_object; } |
| |
| PassOwnPtr<{{type.id}}> clone() { return adoptPtr(new {{type.id}}(m_object)); } |
| |
| template<int STATE> |
| class {{type.id}}Builder { |
| public: |
| enum { |
| NoFieldsSet = 0, |
| {% set count = 0 %} |
| {% for property in type.properties %} |
| {% if not(property.optional) %} |
| {% set count = count + 1 %} |
| {{property.name | to_title_case}}Set = 1 << {{count}}, |
| {% endif %} |
| {% endfor %} |
| AllFieldsSet = ( |
| {%- for property in type.properties %} |
| {% if not(property.optional) %}{{property.name | to_title_case}}Set | {%endif %} |
| {% endfor %}0)}; |
| |
| {% for property in type.properties %} |
| |
| {% if property.optional %} |
| {{type.id}}Builder<STATE>& set{{property.name | to_title_case}}(const Maybe<{{resolve_type(property).raw_type}}>& value) |
| { |
| m_result->set{{property.name | to_title_case}}(value); |
| return *this; |
| } |
| {% else %} |
| {{type.id}}Builder<STATE | {{property.name | to_title_case}}Set>& set{{property.name | to_title_case}}({{resolve_type(property).pass_type}} value) |
| { |
| static_assert(!(STATE & {{property.name | to_title_case}}Set), "property {{property.name}} should not be set yet"); |
| m_result->set{{property.name | to_title_case}}(value); |
| return castState<{{property.name | to_title_case}}Set>(); |
| } |
| {% endif %} |
| {% endfor %} |
| |
| PassOwnPtr<{{type.id}}> build() |
| { |
| static_assert(STATE == AllFieldsSet, "state should be AllFieldsSet"); |
| return m_result.release(); |
| } |
| |
| private: |
| friend class {{type.id}}; |
| {{type.id}}Builder() : m_result({{type_def.create_type}}) { } |
| |
| template<int STEP> {{type.id}}Builder<STATE | STEP>& castState() |
| { |
| return *reinterpret_cast<{{type.id}}Builder<STATE | STEP>*>(this); |
| } |
| |
| {{type_def.type}} m_result; |
| }; |
| |
| static {{type.id}}Builder<0> create() |
| { |
| return {{type.id}}Builder<0>(); |
| } |
| |
| private: |
| explicit {{type.id}}(PassRefPtr<JSONObject> object) : m_object(object) { } |
| RefPtr<JSONObject> m_object; |
| }; |
| |
| {% endif %} |
| {% endfor %} |
| |
| } // {{domain.domain}} |
| {% endfor %} |
| |
| } // namespace protocol |
| } // namespace blink |
| |
| #endif // !defined({{class_name}}_h) |