Chromium Code Reviews| Index: tools/json_schema_compiler/cpp_util.py |
| diff --git a/tools/json_schema_compiler/cpp_util.py b/tools/json_schema_compiler/cpp_util.py |
| index 760f01da194fc19d30ea2090075a7524f22643d8..40758489342bf8ff84338405cb5a9c260f8851e7 100644 |
| --- a/tools/json_schema_compiler/cpp_util.py |
| +++ b/tools/json_schema_compiler/cpp_util.py |
| @@ -6,6 +6,7 @@ |
| from datetime import datetime |
| from model import PropertyType |
| +import re |
| CHROMIUM_LICENSE = ( |
| """// Copyright (c) %d The Chromium Authors. All rights reserved. |
| @@ -17,8 +18,7 @@ GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN |
| // DO NOT EDIT. |
| """ |
| - |
| -def CppName(s): |
| +def Classname(s): |
| """Translates a namespace name or function name into something more |
| suited to C++. |
| @@ -30,7 +30,11 @@ def CppName(s): |
| def CreateFundamentalValue(prop, var): |
| """Returns the C++ code for creating a value of the given property type |
| using the given variable. |
| + |
| + var: Fundamental or Fundamental* |
| """ |
| + if prop.optional: |
| + var = '*' + var |
| return { |
| PropertyType.STRING: 'Value::CreateStringValue(%s)', |
| PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', |
| @@ -38,14 +42,47 @@ def CreateFundamentalValue(prop, var): |
| PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', |
| }[prop.type_] % var |
| +def GetFundamentalValue(prop, src, name, dst): |
| + """Returns the C++ code for retrieving a fundamental type from a |
| + DictionaryValue into a variable. |
| -def GetFundamentalValue(prop, var): |
| - """Returns the C++ code for retrieving a fundamental type from a Value |
| - into a variable. |
| + src: DictionaryValue* |
| + name: key |
| + dst: Property* |
| """ |
| return { |
| - PropertyType.STRING: 'GetAsString(%s)', |
| - PropertyType.BOOLEAN: 'GetAsBoolean(%s)', |
| - PropertyType.INTEGER: 'GetAsInteger(%s)', |
| - PropertyType.DOUBLE: 'GetAsDouble(%s)', |
| - }[prop.type_] % var |
| + PropertyType.STRING: '%s->GetString("%s", %s)', |
| + PropertyType.BOOLEAN: '%s->GetBoolean("%s", %s)', |
| + PropertyType.INTEGER: '%s->GetInteger("%s", %s)', |
| + PropertyType.DOUBLE: '%s->GetDouble("%s", %s)', |
| + }[prop.type_] % (src, name, dst) |
| + |
| +def CreateValueFromSingleProperty(prop, var): |
| + """Creates a Value given a single property. Use for everything except |
| + PropertyType.ARRAY. |
| + |
| + var: raw value |
|
not at google - send to devlin
2012/02/05 23:42:12
"raw value"?
|
| + """ |
| + if prop.type_ == PropertyType.REF or prop.type_ == PropertyType.OBJECT: |
| + return '%s.ToValue()' % var |
| + elif prop.type_.is_fundamental: |
| + return CreateFundamentalValue(prop, var) |
| + else: |
| + raise NotImplementedError('Conversion of single %s to Value not implemented' % |
|
not at google - send to devlin
2012/02/05 23:42:12
line wrap
calamity
2012/02/06 11:51:18
Done.
|
| + repr(prop.type_)) |
| + |
| +def GetValueFromList(prop, src, index, dst): |
| + """Returns the C++ code for retrieving a fundamental type from a |
| + DictionaryValue into a variable. |
| + |
| + src: ListValue& |
| + index: int |
| + dst: Property* |
| + """ |
| + return { |
| + PropertyType.REF: '%s.GetDictionary(%d, %s)', |
| + PropertyType.STRING: '%s.GetString(%d, %s)', |
| + PropertyType.BOOLEAN: '%s.GetBoolean(%d, %s)', |
| + PropertyType.INTEGER: '%s.GetInteger(%d, %s)', |
| + PropertyType.DOUBLE: '%s.GetDouble(%d, %s)', |
| + }[prop.type_] % (src, index, dst) |