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..38e4bed7723c5db4ebe6461011ce24397b75aba7 100644 |
--- a/tools/json_schema_compiler/cpp_util.py |
+++ b/tools/json_schema_compiler/cpp_util.py |
@@ -17,8 +17,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 +29,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 +41,64 @@ def CreateFundamentalValue(prop, var): |
PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', |
}[prop.type_] % var |
+def GetAsFundamentalValue(prop, src, dst): |
+ """Returns the C++ code for retrieving a fundamental type from a |
+ Value into a variable. |
-def GetFundamentalValue(prop, var): |
- """Returns the C++ code for retrieving a fundamental type from a Value |
- into a variable. |
+ src: Value* |
+ 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->GetAsString(%s)', |
+ PropertyType.BOOLEAN: '%s->GetAsBoolean(%s)', |
+ PropertyType.INTEGER: '%s->GetAsInteger(%s)', |
+ PropertyType.DOUBLE: '%s->GetAsDouble(%s)', |
+ }[prop.type_] % (src, dst) |
+ |
+def GetFundamentalValue(prop, src, name, dst): |
+ """Returns the C++ code for retrieving a fundamental type from a |
+ DictionaryValue into a variable. |
+ |
+ src: DictionaryValue* |
+ name: key |
+ dst: Property* |
+ """ |
+ return { |
+ 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 |
+ """ |
+ if prop.type_ == PropertyType.REF or prop.type_ == PropertyType.OBJECT: |
+ if prop.optional: |
+ return '%s->ToValue()' % var |
+ else: |
+ return '%s.ToValue()' % var |
+ elif prop.type_.is_fundamental: |
+ return CreateFundamentalValue(prop, var) |
+ else: |
+ raise NotImplementedError('Conversion of single %s to Value not implemented' |
+ % 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) |