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 | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..31e991fd1c48190fdfb258a85237d1c39ec6122b | 
| --- /dev/null | 
| +++ b/tools/json_schema_compiler/cpp_util.py | 
| @@ -0,0 +1,50 @@ | 
| +# Copyright (c) 2012 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. | 
| +"""Utilies and constants specific to Chromium C++ code. | 
| +""" | 
| + | 
| +from datetime import datetime | 
| +from model import PropertyType | 
| + | 
| +CHROMIUM_LICENSE = ( | 
| +"""// Copyright (c) %d 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.""" % datetime.now().year | 
| +) | 
| +GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN | 
| +// %s | 
| +// DO NOT EDIT. | 
| +""" | 
| + | 
| +def cpp_name(s): | 
| 
 
Yoyo Zhou
2012/01/19 02:19:40
Separate top-level function and class definitions
 
calamity
2012/01/20 01:10:25
Done.
 
 | 
| + """Translates a namespace name or function name into something more | 
| + suited to C++. | 
| + | 
| + eg experimental.downloads -> Experimental_Downloads | 
| + updateAll -> UpdateAll. | 
| + """ | 
| + return '_'.join([x[0].upper() + x[1:] for x in s.split('.')]) | 
| 
 
Yoyo Zhou
2012/01/19 02:19:40
x.capitalize()
 
calamity
2012/01/20 01:10:25
Capitalize inconveniently lowercases everything af
 
Yoyo Zhou
2012/01/23 23:16:42
Oh, sadness.
 
 | 
| + | 
| +def create_fundamental_value(prop, var): | 
| + """Returns the C++ code for creating a value of the given property type | 
| + using the given variable. | 
| + """ | 
| + return { | 
| + PropertyType.STRING: 'Value::CreateStringValue(%s)', | 
| + PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', | 
| + PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', | 
| + PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', | 
| + }[prop.type] % var | 
| + | 
| + | 
| +def get_fundamental_value(prop, var): | 
| + """Returns the C++ code for retrieving a fundamental type from a Value | 
| + into a variable. | 
| + """ | 
| + return { | 
| + PropertyType.STRING: 'GetAsString(%s)', | 
| + PropertyType.BOOLEAN: 'GetAsBoolean(%s)', | 
| + PropertyType.INTEGER: 'GetAsInteger(%s)', | 
| + PropertyType.DOUBLE: 'GetAsDouble(%s)', | 
| + }[prop.type] % var |