Index: content/browser/devtools/protocol/devtools_protocol_handler_generator.py |
diff --git a/content/browser/devtools/protocol/devtools_protocol_handler_generator.py b/content/browser/devtools/protocol/devtools_protocol_handler_generator.py |
index dd165faec5f34eb139e1db9ef62166ed3f250f8d..72219c2d2c0f611de1bc222281662ce61b82313f 100755 |
--- a/content/browser/devtools/protocol/devtools_protocol_handler_generator.py |
+++ b/content/browser/devtools/protocol/devtools_protocol_handler_generator.py |
@@ -34,6 +34,43 @@ namespace content { |
class DevToolsProtocolHandlerImpl; |
+class ListBuilderBase { |
dgozman
2014/11/06 14:20:42
Let's put ListBuilder in content::devtools.
vkuzkokov
2014/11/06 16:30:12
Done.
|
+ public: |
+ static scoped_ptr<base::ListValue> ToValue(ListBuilderBase src); |
+ |
+ protected: |
+ ListBuilderBase(); |
+ ListBuilderBase(scoped_ptr<base::ListValue> list_); |
+ |
+ scoped_ptr<base::ListValue> list_; |
+}; |
+ |
+template<class T> |
+class ListBuilder : public ListBuilderBase { |
+ MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(ListBuilder) |
+ public: |
+ ListBuilder() {} |
+ ListBuilder(ListBuilder&& other) : ListBuilderBase(other.list_.Pass()) { |
+ } |
+ |
+ void push_back(T item) { |
+ list_->Append(T::ToValue(item.Pass()).release()); |
+ } |
+}; |
+ |
+template<> |
+class ListBuilder<std::string> : public ListBuilderBase { |
+ MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(ListBuilder) |
+ public: |
+ ListBuilder() {} |
+ ListBuilder(ListBuilder&& other) : ListBuilderBase(other.list_.Pass()) { |
+ } |
+ |
+ void push_back(const std::string& item) { |
+ list_->AppendString(item); |
+ } |
+}; |
+ |
namespace devtools { |
${types}\ |
@@ -51,8 +88,6 @@ class DevToolsProtocolHandlerImpl : public DevToolsProtocol::Handler { |
${setters}\ |
private: |
-${friends}\ |
- |
${methods}\ |
${fields}\ |
@@ -71,27 +106,72 @@ typedef ${param_type} ${declared_name}; |
tmpl_struct = string.Template("""\ |
namespace ${domain} { |
-struct ${declared_name} { |
+template<int MASK> |
+struct ${declared_name}Builder { |
+ MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(${declared_name}Builder) |
public: |
- ${declared_name}(); |
+ enum { |
+ kAllSet = 0, |
+${fields_enum}\ |
+ }; |
${methods}\ |
+ static ${declared_name}Builder<kNoneSet> Create() { |
+ return ${declared_name}Builder<kNoneSet>(); |
+ } |
+ |
+ static scoped_ptr<base::DictionaryValue> ToValue( |
+ ${declared_name}Builder src) { |
+ return src.dict_.Pass(); |
+ } |
+ |
+ ${declared_name}Builder(${declared_name}Builder&& other) |
+ : dict_(other.dict_.Pass()) { |
+ } |
+ |
private: |
- friend class ::content::DevToolsProtocolHandlerImpl; |
+ friend struct ${declared_name}Builder<0>; |
-${fields}\ |
+ ${declared_name}Builder() : dict_(new base::DictionaryValue()) { |
+ } |
+ |
+ template<class T> T& ThisAs() { |
+ COMPILE_ASSERT(sizeof(*this) == sizeof(T), cannot_cast); |
+ return *reinterpret_cast<T*>(this); |
+ } |
+ |
+ scoped_ptr<base::DictionaryValue> dict_; |
}; |
+ |
+typedef ${declared_name}Builder<0> ${declared_name}; |
+ |
} // namespace ${domain} |
""") |
-tmpl_struct_setter = string.Template("""\ |
- void set_${param}(${pass_type} ${param}); |
+tmpl_builder_setter_req = string.Template("""\ |
+ ${declared_name}Builder<MASK & ~k${Param}>& |
+ set_${param}(${pass_type} ${param}) { |
+ COMPILE_ASSERT(MASK & k${Param}, already_set); |
+${builder_wrap}\ |
+ return ThisAs<${declared_name}Builder<MASK & ~k${Param}>>(); |
+ } |
""") |
-tmpl_struct_field = string.Template("""\ |
- ${param_type} ${param}_; |
- bool has_${param}_; |
+tmpl_builder_setter_opt = string.Template("""\ |
+ ${declared_name}Builder<MASK>& |
+ set_${param}(${pass_type} ${param}) { |
+${builder_wrap}\ |
+ return *this; |
+ } |
+""") |
+ |
+tmpl_builder_enum = string.Template("""\ |
+ k${Param} = 1 << ${ordinal}, |
+""") |
+ |
+tmpl_builder_none_set = string.Template("""\ |
+ kNoneSet = ${all_fields} |
""") |
tmpl_enum = string.Template("""\ |
@@ -130,13 +210,13 @@ ${methods}\ |
tmpl_event = string.Template("""\ |
void ${Command}( |
- const ${Command}Params& params); |
+ ${Command}Params params); |
""") |
tmpl_response = string.Template("""\ |
void Send${Command}Response( |
scoped_refptr<DevToolsProtocol::Command> command, |
- const ${Command}Response& params); |
+ ${Command}Response params); |
""") |
tmpl_setter = string.Template("""\ |
@@ -144,21 +224,12 @@ tmpl_setter = string.Template("""\ |
devtools::${domain}::${Domain}Handler* ${domain}_handler); |
""") |
-tmpl_friend = string.Template("""\ |
- friend class devtools::${domain}::Client; |
-""") |
- |
tmpl_callback = string.Template("""\ |
scoped_refptr<DevToolsProtocol::Response> |
On${Domain}${Command}( |
scoped_refptr<DevToolsProtocol::Command> command); |
""") |
-tmpl_to_value = string.Template("""\ |
- static base::DictionaryValue* ToValue( |
- const devtools::${domain}::${declared_name}& src); |
-""") |
- |
tmpl_field = string.Template("""\ |
devtools::${domain}::${Domain}Handler* ${domain}_handler_; |
""") |
@@ -172,6 +243,20 @@ ${includes}\ |
namespace content { |
+ListBuilderBase::ListBuilderBase() |
+ : list_(new base::ListValue) { |
+} |
+ |
+ListBuilderBase::ListBuilderBase(scoped_ptr<base::ListValue> list) |
+ : list_(list.Pass()) { |
+} |
+ |
+// static |
+scoped_ptr<base::ListValue> ListBuilderBase::ToValue( |
+ ListBuilderBase src) { |
+ return src.list_.Pass(); |
+} |
+ |
DevToolsProtocolHandlerImpl::DevToolsProtocolHandlerImpl() |
: ${fields_init} { |
} |
@@ -189,7 +274,7 @@ bool CreateCommonResponse( |
scoped_refptr<DevToolsProtocol::Response>* protocol_response) { |
switch (response.status()) { |
case ResponseStatus::RESPONSE_STATUS_FALLTHROUGH: |
- *protocol_response = NULL; |
+ *protocol_response = nullptr; |
break; |
case ResponseStatus::RESPONSE_STATUS_OK: |
return false; |
@@ -223,7 +308,7 @@ tmpl_include = string.Template("""\ |
#include "content/browser/devtools/protocol/${domain}_handler.h" |
""") |
-tmpl_field_init = string.Template("${domain}_handler_(NULL)") |
+tmpl_field_init = string.Template("${domain}_handler_(nullptr)") |
tmpl_setter_impl = string.Template("""\ |
void DevToolsProtocolHandlerImpl::Set${Domain}Handler( |
@@ -258,9 +343,9 @@ ${prep}\ |
scoped_refptr<DevToolsProtocol::Response> protocol_response; |
if (CreateCommonResponse(command, response, &protocol_response)) |
return protocol_response; |
- base::DictionaryValue* dict = new base::DictionaryValue(); |
+ base::DictionaryValue* result = new base::DictionaryValue(); |
${wrap}\ |
- return command->SuccessResponse(dict); |
+ return command->SuccessResponse(result); |
} |
""") |
@@ -279,16 +364,15 @@ params_prep = """\ |
tmpl_prep_req = string.Template("""\ |
${param_type} in_${param}${init}; |
- if (!params || |
- !params->Get${Type}("${proto_param}", &in_${param})) |
+ if (!params || !params->Get${Type}("${proto_param}", &in_${param})) |
return command->InvalidParamResponse("${proto_param}"); |
""") |
tmpl_prep_req_list = string.Template("""\ |
- base::ListValue* list_${param} = NULL; |
+ base::ListValue* list_${param} = nullptr; |
if (!params || !params->GetList("${proto_param}", &list_${param})) |
return command->InvalidParamResponse("${proto_param}"); |
- ${param_type} in_${param}; |
+ std::vector<${item_type}> in_${param}; |
for (base::ListValue::const_iterator it = |
list_${param}->begin(); it != list_${param}->end(); ++it) { |
${item_type} item${item_init}; |
@@ -312,45 +396,7 @@ tmpl_prep_output = string.Template("""\ |
tmpl_arg_req = string.Template("in_${param}") |
tmpl_arg_opt = string.Template( |
- "${param}_found ?\n &in_${param} : NULL") |
- |
-tmpl_arg_output = string.Template("&out_${param}") |
- |
-tmpl_to_value_impl = string.Template("""\ |
-// static |
-base::DictionaryValue* DevToolsProtocolHandlerImpl::ToValue( |
- const devtools::${domain}::${declared_name}& src) { |
- base::DictionaryValue* dict = new base::DictionaryValue(); |
-${dchecks}\ |
-${wrap}\ |
- return dict; |
-} |
-""") |
- |
-tmpl_dcheck = string.Template("""\ |
- DCHECK(${cond_name}); |
-""") |
- |
-tmpl_struct_impl = string.Template("""\ |
-namespace ${domain} { |
- |
-${declared_name}::${declared_name}()${fields} { |
-} |
- |
-${methods}\ |
- |
-} // namespace ${domain} |
-""") |
- |
-tmpl_struct_field_init = string.Template("has_${param}_(false)") |
- |
-tmpl_struct_setter_impl = string.Template("""\ |
-void ${declared_name}::set_${param}( |
- ${pass_type} ${param}) { |
- ${param}_ = ${param}; |
- has_${param}_ = true; |
-} |
-""") |
+ "${param}_found ? &in_${param} : nullptr") |
tmpl_client_impl = string.Template("""\ |
namespace ${domain} { |
@@ -369,68 +415,45 @@ ${methods}\ |
tmpl_event_impl = string.Template("""\ |
void Client::${Command}( |
- const ${Command}Params& params) { |
+ ${Command}Params params) { |
SendNotification("${Domain}.${command}", |
- DevToolsProtocolHandlerImpl::ToValue(params)); |
+ ${Command}Params::ToValue(params.Pass()).Pass()); |
} |
""") |
tmpl_response_impl = string.Template("""\ |
void Client::Send${Command}Response( |
scoped_refptr<DevToolsProtocol::Command> command, |
- const ${Command}Response& params) { |
+ ${Command}Response params) { |
SendAsyncResponse( |
- command->SuccessResponse(DevToolsProtocolHandlerImpl::ToValue(params))); |
+ command->SuccessResponse( |
+ ${Command}Response::ToValue(params.Pass()).release())); |
} |
""") |
tmpl_wrap = string.Template("""\ |
- dict->Set${Type}("${proto_param}", ${var_name}); |
+ ${dict_var}->Set${Type}("${proto_param}", ${var_name}); |
""") |
tmpl_wrap_dict = string.Template("""\ |
- dict->Set("${proto_param}", |
- DevToolsProtocolHandlerImpl::ToValue(${var_name})); |
+ ${dict_var}->Set("${proto_param}", |
+ ${param_type}::ToValue(${var_name}.Pass()).release()); |
""") |
-tmpl_wrap_obj = string.Template("""\ |
- dict->Set("${proto_param}", ${var_name}); |
-""") |
- |
-tmpl_wrap_list = string.Template("""\ |
- base::ListValue* list_${param} = new base::ListValue(); |
- for (${param_type}::const_iterator it = |
- ${var_name}.begin(); it != ${var_name}.end(); ++it) { |
-${append}\ |
- } |
- dict->Set("${proto_param}", list_${param}); |
-""") |
- |
-tmpl_append = string.Template("""\ |
- list_${param}->Append${Type}(*it); |
-""") |
+tmpl_wrap_list = tmpl_wrap_dict; |
-tmpl_append_dict = string.Template("""\ |
- list_${param}->Append(DevToolsProtocolHandlerImpl::ToValue(*it)); |
-""") |
- |
-tmpl_append_obj = string.Template("""\ |
- list_${param}->Append(*it); |
-""") |
- |
-tmpl_wrap_opt = string.Template("""\ |
- if (${cond_name}) |
- dict->Set${Type}("${proto_param}", ${var_name}); |
+tmpl_wrap_obj = string.Template("""\ |
+ ${dict_var}->Set("${proto_param}", ${var_name}.release()); |
""") |
tmpl_typename = string.Template("devtools::${domain}::${declared_name}") |
+def Shift(prefix, s): |
+ return "\n".join(map(lambda line: prefix + line, s.splitlines())) + "\n" |
+ |
def Capitalize(s): |
return s[:1].upper() + s[1:] |
-def Decapitalize(s): |
- return s.lower() |
- |
def Uncamelcase(s): |
result = "" |
for i, c in enumerate(s): |
@@ -456,48 +479,35 @@ for json_domain in json_api["domains"]: |
def DeclareStruct(json_properties, mapping): |
methods = [] |
- fields = [] |
- fields_init = [] |
- method_impls = [] |
- dchecks = [] |
- wrap = [] |
+ fields_enum = [] |
+ enum_items = [] |
+ req_fields_num = 0 |
for json_prop in json_properties: |
prop_map = mapping.copy() |
prop_map["proto_param"] = json_prop["name"] |
prop_map["param"] = Uncamelcase(json_prop["name"]) |
- prop_map["var_name"] = "src.%s_" % prop_map["param"] |
- prop_map["cond_name"] = "src.has_%s_" % prop_map["param"] |
+ prop_map["Param"] = Capitalize(json_prop["name"]) |
+ prop_map["var_name"] = "%s" % prop_map["param"] |
+ prop_map["dict_var"] = "dict_"; |
ResolveType(json_prop, prop_map) |
prop_map["declared_name"] = mapping["declared_name"] |
- methods.append(tmpl_struct_setter.substitute(prop_map)) |
- fields.append(tmpl_struct_field.substitute(prop_map)) |
- fields_init.append(tmpl_struct_field_init.substitute(prop_map)) |
- method_impls.append(tmpl_struct_setter_impl.substitute(prop_map)) |
if json_prop.get("optional"): |
- if param_map["Type"] in ["List", "Dictionary"]: |
- # TODO(vkuzkokov) Implement. |
- raise Exception( |
- "Optional array and object properties are not implemented") |
- wrap.append(tmpl_wrap_opt.substitute(prop_map)) |
+ methods.append(tmpl_builder_setter_opt.substitute(prop_map)) |
else: |
- dchecks.append(tmpl_dcheck.substitute(prop_map)); |
- if not "wrap" in prop_map: |
- raise Exception("Arrays of arrays are not implemented") |
- wrap.append(prop_map["wrap"]) |
- |
+ methods.append(tmpl_builder_setter_req.substitute(prop_map)) |
+ enum_items.append("k%s" % prop_map["Param"]); |
+ fields_enum.append(tmpl_builder_enum.substitute(prop_map, |
+ ordinal = req_fields_num)) |
+ req_fields_num += 1 |
+ |
+ all_fields = "kAllSet" |
+ if len(enum_items) > 0: |
+ all_fields = " | ".join(enum_items) |
+ fields_enum.append(tmpl_builder_none_set.substitute(mapping, |
+ all_fields = all_fields)) |
type_decls.append(tmpl_struct.substitute(mapping, |
- methods = "".join(methods), |
- fields = "".join(fields))) |
- fields_init_str = "" |
- if len(fields_init) > 0: |
- fields_init_str = "\n : " + (",\n ".join(fields_init)) |
- type_impls.append(tmpl_struct_impl.substitute(mapping, |
- fields = fields_init_str, |
- methods = "\n".join(method_impls))) |
- handler_methods.append(tmpl_to_value.substitute(mapping)) |
- handler_method_impls.append(tmpl_to_value_impl.substitute(mapping, |
- dchecks = "".join(dchecks), |
- wrap = "".join(wrap))) |
+ methods = "\n".join(methods), |
+ fields_enum = "".join(fields_enum))) |
def ResolveRef(json, mapping): |
dot_pos = json["$ref"].find(".") |
@@ -510,7 +520,7 @@ def ResolveRef(json, mapping): |
json_type = types["%s.%s" % (domain_name, type_name)] |
mapping["declared_name"] = Capitalize(type_name) |
mapping["Domain"] = domain_name |
- mapping["domain"] = Decapitalize(domain_name) |
+ mapping["domain"] = Uncamelcase(domain_name) |
mapping["param_type"] = tmpl_typename.substitute(mapping) |
if json_type.get("enum"): |
# TODO(vkuzkokov) Implement. Approximate template: |
@@ -528,17 +538,15 @@ def ResolveRef(json, mapping): |
def ResolveArray(json, mapping): |
items_map = mapping.copy() |
ResolveType(json["items"], items_map) |
- mapping["param_type"] = "std::vector<%s>" % items_map["param_type"] |
+ mapping["param_type"] = "ListBuilder<%s>" % items_map["param_type"] |
mapping["Type"] = "List" |
- if "append" in items_map: |
- mapping["wrap"] = tmpl_wrap_list.substitute(mapping, |
- append = items_map["append"]) |
- mapping["pass_type"] = "const %s&" % mapping["param_type"] |
+ mapping["wrap"] = tmpl_wrap_list.substitute(mapping) |
+ mapping["pass_type"] = mapping["param_type"] |
mapping["prep_req"] = tmpl_prep_req_list.substitute(mapping, |
item_type = items_map["param_type"], |
item_init = items_map["init"], |
ItemType = items_map["Type"]) |
- # TODO(vkuzkokov) mapping["append"]: template for array of arrays. |
+ mapping["arg_out"] = "&%s" % mapping["var_name"] |
def ResolveObject(json, mapping): |
mapping["Type"] = "Dictionary" |
@@ -548,14 +556,14 @@ def ResolveObject(json, mapping): |
(mapping["Command"], Capitalize(mapping["proto_param"]))) |
mapping["param_type"] = tmpl_typename.substitute(mapping) |
DeclareStruct(json["properties"], mapping) |
- mapping["append"] = tmpl_append_dict.substitute(mapping) |
mapping["wrap"] = tmpl_wrap_dict.substitute(mapping) |
- mapping["pass_type"] = "const %s&" % mapping["param_type"] |
+ mapping["pass_type"] = mapping["param_type"] |
+ mapping["arg_out"] = "&%s" % mapping["var_name"] |
else: |
- mapping["param_type"] = "base::DictionaryValue*" |
- mapping["append"] = tmpl_append_obj.substitute(mapping) |
+ mapping["param_type"] = "base::DictionaryValue" |
mapping["wrap"] = tmpl_wrap_obj.substitute(mapping) |
- mapping["pass_type"] = mapping["param_type"] |
+ mapping["pass_type"] = "scoped_ptr<base::DictionaryValue>" |
+ mapping["arg_out"] = "%s.get()" % mapping["var_name"] |
def ResolvePrimitive(json, mapping): |
jsonrpc_type = json["type"] |
@@ -596,10 +604,10 @@ def ResolvePrimitive(json, mapping): |
else: |
raise Exception("Unknown type: %s" % json_type) |
mapping["wrap"] = tmpl_wrap.substitute(mapping) |
- mapping["append"] = tmpl_append.substitute(mapping) |
mapping["prep_req"] = tmpl_prep_req.substitute(mapping) |
if jsonrpc_type != "string": |
mapping["pass_type"] = mapping["param_type"] |
+ mapping["arg_out"] = "&%s" % mapping["var_name"] |
def ResolveType(json, mapping): |
mapping["init"] = "" |
@@ -616,9 +624,9 @@ def ResolveType(json, mapping): |
else: |
raise Exception("Unknown type at %s.%s %s" % |
(mapping["Domain"], mapping["command"], mapping["proto_param"])) |
+ mapping["builder_wrap"] = Shift(" ", mapping["wrap"]) |
setters = [] |
-friends = [] |
fields = [] |
includes = [] |
@@ -627,7 +635,7 @@ fields_init = [] |
for json_domain in json_api["domains"]: |
domain_map = {} |
domain_map["Domain"] = json_domain["domain"] |
- domain_map["domain"] = Decapitalize(json_domain["domain"]) |
+ domain_map["domain"] = Uncamelcase(json_domain["domain"]) |
initializations = [] |
client_methods = [] |
@@ -655,6 +663,7 @@ for json_domain in json_api["domains"]: |
param_map["proto_param"] = json_param["name"] |
param_map["param"] = Uncamelcase(json_param["name"]) |
param_map["var_name"] = "in_%s" % param_map["param"] |
+ param_map["dict_var"] = "result" |
ResolveType(json_param, param_map) |
if len(prep) == 0: |
@@ -696,13 +705,14 @@ for json_domain in json_api["domains"]: |
param_map["proto_param"] = json_param["name"] |
param_map["param"] = Uncamelcase(json_param["name"]) |
param_map["var_name"] = "out_%s" % param_map["param"] |
+ param_map["dict_var"] = "result" |
if json_param.get("optional"): |
# TODO(vkuzkokov) Implement Optional<T> for value types. |
raise Exception("Optional return values are not implemented") |
ResolveType(json_param, param_map) |
prep.append(tmpl_prep_output.substitute(param_map)) |
- args.append(tmpl_arg_output.substitute(param_map)) |
+ args.append(param_map["arg_out"]) |
if not "wrap" in param_map: |
raise Exception("Arrays of arrays are not implemented") |
wrap.append(param_map["wrap"]) |
@@ -749,7 +759,6 @@ for json_domain in json_api["domains"]: |
if domain_needs_client: |
type_decls.append(tmpl_client.substitute(domain_map, |
methods = "".join(client_methods))) |
- friends.append(tmpl_friend.substitute(domain_map)) |
initializations.append(tmpl_init_client.substitute(domain_map)) |
type_impls.append(tmpl_client_impl.substitute(domain_map, |
methods = "\n".join(client_method_impls))) |
@@ -763,7 +772,6 @@ output_cc_file = open(output_cc_path, "w") |
output_h_file.write(template_h.substitute({}, |
types = "\n".join(type_decls), |
setters = "".join(setters), |
- friends = "".join(friends), |
methods = "".join(handler_methods), |
fields = "".join(fields))) |
output_h_file.close() |